| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import 'package:flutter/widgets.dart';
- class AnimatedListController<T> {
- final List<T> items = [];
- final GlobalKey<SliverAnimatedListState> listKey =
- GlobalKey<SliverAnimatedListState>();
- set items(List<T> items) {
- this.items = items;
- }
- void add(T item,
- {Duration duration = const Duration(milliseconds: 300), int? index}) {
- if (index != null) {
- items.insert(index, item);
- listKey.currentState?.insertItem(index, duration: duration);
- } else {
- items.add(item);
- listKey.currentState?.insertItem(items.length - 1, duration: duration);
- }
- }
- void addAtIndex(int index, List<T> newItems,
- {Duration duration = const Duration(milliseconds: 300)}) {
- for (var i = 0; i < newItems.length; i++) {
- items.insert(index + i, newItems[i]);
- listKey.currentState?.insertItem(index + i, duration: duration);
- }
- }
- void addAll(List<T> newItems,
- {Duration duration = const Duration(milliseconds: 300)}) {
- for (var i = 0; i < newItems.length; i++) {
- items.add(newItems[i]);
- listKey.currentState?.insertItem(items.length - 1, duration: duration);
- }
- }
- void update(int index, T item) {
- items[index] = item;
- listKey.currentState?.insertItem(index);
- }
- void remove(
- int index, Widget Function(BuildContext, Animation<double>, T) buildItem,
- {Duration duration = const Duration(milliseconds: 300)}) {
- final removedItem = items.removeAt(index);
- listKey.currentState?.removeItem(
- index,
- (context, animation) => buildItem(context, animation, removedItem),
- duration: duration,
- );
- }
- void removeAtIndex(int index, int count,
- Widget Function(BuildContext, Animation<double>, T) buildItem,
- {Duration duration = const Duration(milliseconds: 300)}) {
- for (var i = 0; i < count; i++) {
- final removedItem = items.removeAt(index);
- listKey.currentState?.removeItem(
- index,
- (context, animation) => buildItem(context, animation, removedItem),
- duration: duration,
- );
- }
- }
- void removeAll(Widget Function(BuildContext, Animation<double>, T) buildItem,
- {Duration duration = const Duration(milliseconds: 300)}) {
- for (var i = items.length - 1; i >= 0; i--) {
- final removedItem = items.removeAt(i);
- listKey.currentState?.removeItem(
- i,
- (context, animation) => buildItem(context, animation, removedItem),
- duration: duration,
- );
- }
- }
- /// *不带动画****
- void clearAt(int index) {
- items.removeAt(index);
- listKey.currentState
- ?.removeItem(index, (context, animation) => Container());
- }
- void clearItem(int index) {
- items.removeAt(index);
- listKey.currentState
- ?.removeItem(index, (context, animation) => Container());
- }
- void clearAll() {
- items.clear();
- listKey.currentState?.removeAllItems((context, animation) => Container());
- }
- /// *不带动画****
- int get length {
- return items.length;
- }
- T get(int index) {
- return items[index];
- }
- }
|