mirror of
https://github.com/alibaba/flutter-go.git
synced 2025-05-24 08:37:00 +08:00
43 lines
1.2 KiB
Dart
43 lines
1.2 KiB
Dart
/// Created with Android Studio.
|
|
/// User: 一晟
|
|
/// Date: 2018/12/31
|
|
/// Time: 下午10:15
|
|
/// email: zhu.yan@alibaba-inc.com
|
|
/// target: model 的示例
|
|
|
|
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);
|
|
} |