1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| //定义变量 List<int> _list = [0,1]; final GlobalKey<AnimatedListState> _listKey=GlobalKey<AnimatedListState>();
//试图 body:AnimatedList( key: _listKey, initialItemCount: _list.length, itemBuilder: (BuildContext context,int index,Animation animation){ return _buildItem(_list[index].toString(), animation); }, ), floatingActionButton: Row( mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ FloatingActionButton(onPressed: ()=>_addItem(),child: Icon(Icons.add),), SizedBox(width: 60,), FloatingActionButton(onPressed: ()=>_removeItem(),child: Icon(Icons.remove),) ], ) //方法 void _addItem(){ final int _index=_list.length; _list.insert(_index, _index); //AnimatedList.of(context).insertItem(_index); _listKey.currentState.insertItem(_index); }
void _removeItem(){ final int _index=_list.length-1; var item=_list[_index].toString(); //AnimatedList.of(context).removeItem(_index, (context, animation) => _buildItem(item,animation)); _listKey.currentState.removeItem(_index, (context, animation) => _buildItem(item,animation)); _list.removeAt(_index);
} Widget _buildItem(String _item,Animation _animation){ return SlideTransition(position: _animation.drive(CurveTween(curve: Curves.easeInOutQuart)).drive(Tween<Offset>(begin: Offset(1,0),end: Offset(0,0))), child: Card(child: ListTile(title: Text(_item),),), ); }
|