mirror of
https://github.com/flutter/samples.git
synced 2025-11-08 13:58:47 +00:00
116 lines
2.8 KiB
Dart
116 lines
2.8 KiB
Dart
// Copyright 2020 The Flutter team. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
class AnimatedListDemo extends StatefulWidget {
|
|
static String routeName = '/misc/animated_list';
|
|
|
|
@override
|
|
_AnimatedListDemoState createState() => _AnimatedListDemoState();
|
|
}
|
|
|
|
class _AnimatedListDemoState extends State<AnimatedListDemo> {
|
|
final GlobalKey<AnimatedListState> _listKey = GlobalKey();
|
|
final listData = [
|
|
UserModel(0, 'Govind', 'Dixit'),
|
|
UserModel(1, 'Greta', 'Stoll'),
|
|
UserModel(2, 'Monty', 'Carlo'),
|
|
UserModel(3, 'Petey', 'Cruiser'),
|
|
UserModel(4, 'Barry', 'Cade'),
|
|
];
|
|
final initialListSize = 5;
|
|
|
|
void addUser() {
|
|
setState(() {
|
|
var index = listData.length;
|
|
listData.add(
|
|
UserModel(++_maxIdValue, 'New', 'Person'),
|
|
);
|
|
_listKey.currentState!
|
|
.insertItem(index, duration: Duration(milliseconds: 300));
|
|
});
|
|
}
|
|
|
|
void deleteUser(int id) {
|
|
setState(() {
|
|
final index = listData.indexWhere((u) => u.id == id);
|
|
var user = listData.removeAt(index);
|
|
_listKey.currentState!.removeItem(
|
|
index,
|
|
(context, animation) {
|
|
return FadeTransition(
|
|
opacity:
|
|
CurvedAnimation(parent: animation, curve: Interval(0.5, 1.0)),
|
|
child: SizeTransition(
|
|
sizeFactor:
|
|
CurvedAnimation(parent: animation, curve: Interval(0.0, 1.0)),
|
|
axisAlignment: 0.0,
|
|
child: _buildItem(user),
|
|
),
|
|
);
|
|
},
|
|
duration: Duration(milliseconds: 600),
|
|
);
|
|
});
|
|
}
|
|
|
|
Widget _buildItem(UserModel user) {
|
|
return ListTile(
|
|
key: ValueKey<UserModel>(user),
|
|
title: Text(user.firstName),
|
|
subtitle: Text(user.lastName),
|
|
leading: CircleAvatar(
|
|
child: Icon(Icons.person),
|
|
),
|
|
trailing: IconButton(
|
|
icon: Icon(Icons.delete),
|
|
onPressed: () => deleteUser(user.id),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('AnimatedList'),
|
|
actions: <Widget>[
|
|
IconButton(
|
|
icon: Icon(Icons.add),
|
|
onPressed: addUser,
|
|
),
|
|
],
|
|
),
|
|
body: SafeArea(
|
|
child: AnimatedList(
|
|
key: _listKey,
|
|
initialItemCount: 5,
|
|
itemBuilder: (context, index, animation) {
|
|
return FadeTransition(
|
|
opacity: animation,
|
|
child: _buildItem(listData[index]),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class UserModel {
|
|
UserModel(
|
|
this.id,
|
|
this.firstName,
|
|
this.lastName,
|
|
);
|
|
|
|
final int id;
|
|
final String firstName;
|
|
final String lastName;
|
|
}
|
|
|
|
int _maxIdValue = 4;
|