/** * Created with Android Studio. * User: 一晟 * Date: 2018/12/31 * Time: 下午10:15 * email: zhu.yan@alibaba-inc.com * tartget: FlatButton 的示例 */ /// Keeps a Dart List in sync with an AnimatedList. /// /// The [insert] and [removeAt] methods apply to both the internal list and the /// animated list that belongs to [listKey]. /// /// This class only exposes as much of the Dart List API as is needed by the /// sample app. More list methods are easily added, however methods that mutate the /// list must make the same changes to the animated list in terms of /// [AnimatedListState.insertItem] and [AnimatedList.removeItem]. /// import 'package:flutter/material.dart'; class ListModel { ListModel({ @required this.listKey, @required this.removedItemBuilder, Iterable initialItems, }) : assert(listKey != null), assert(removedItemBuilder != null), _items = List.from(initialItems ?? []); final GlobalKey listKey; final dynamic removedItemBuilder; final List _items; AnimatedListState get _animatedList => listKey.currentState; void insert(int index, E item) { _items.insert(index, item); _animatedList.insertItem(index); } E removeAt(int index) { final E removedItem = _items.removeAt(index); if (removedItem != null) { _animatedList.removeItem(index, (BuildContext context, Animation animation) { return removedItemBuilder(removedItem, context, animation); }); } return removedItem; } int get length => _items.length; E operator [](int index) => _items[index]; int indexOf(E item) => _items.indexOf(item); }