mirror of
https://github.com/alibaba/flutter-go.git
synced 2025-05-21 06:46:23 +08:00
54 lines
1.6 KiB
Dart
54 lines
1.6 KiB
Dart
/**
|
|
* 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<E> {
|
|
ListModel({
|
|
@required this.listKey,
|
|
@required this.removedItemBuilder,
|
|
Iterable<E> initialItems,
|
|
}) : assert(listKey != null),
|
|
assert(removedItemBuilder != null),
|
|
_items = List<E>.from(initialItems ?? <E>[]);
|
|
|
|
final GlobalKey<AnimatedListState> listKey;
|
|
final dynamic removedItemBuilder;
|
|
final List<E> _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<double> 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);
|
|
} |