import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: AnimatedListExample(),
);
}
}
class AnimatedListExample extends StatefulWidget {
@override
_AnimatedListExampleState createState() => _AnimatedListExampleState();
}
class _AnimatedListExampleState extends State<AnimatedListExample> {
final GlobalKey<AnimatedListState> _listKey = GlobalKey();
final List<int> _items = <int>[];
void _addItem() {
final int index = _items.length;
_items.add(index);
_listKey.currentState.insertItem(index);
}
void _removeItem() {
if (_items.isNotEmpty) {
final int index = _items.length - 1;
_listKey.currentState.removeItem(index, (BuildContext context, Animation<double> animation) {
return SlideTransition(
position: Tween<Offset>(
begin: const Offset(1.0, 0.0),
end: const Offset(0.0, 0.0),
).animate(animation),
child: Container(
color: Colors.red,
child: ListTile(
title: Text('Item ${_items[index]}'),
),
),
);
});
_items.removeAt(index);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: AnimatedList(
key: _listKey,
initialItemCount: _items.length,
itemBuilder: (BuildContext context, int index, Animation<double> animation) {
return SlideTransition(
position: Tween<Offset>(
begin: const Offset(1.0, 0.0),
end: const Offset(0.0, 0.0),
).animate(animation),
child: Container(
color: Colors.blue,
child: ListTile(
title: Text('Item ${_items[index]}'),
),
),
);
},
),
floatingActionButton: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
onPressed: _addItem,
tooltip: 'Add',
child: Icon(Icons.add),
),
SizedBox(height: 8.0),
FloatingActionButton(
onPressed: _removeItem,
tooltip: 'Remove',
child:
评论已关闭