animated_list_controller.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import 'package:flutter/widgets.dart';
  2. class AnimatedListController<T> {
  3. final List<T> items = [];
  4. final GlobalKey<SliverAnimatedListState> listKey =
  5. GlobalKey<SliverAnimatedListState>();
  6. set items(List<T> items) {
  7. this.items = items;
  8. }
  9. void add(T item,
  10. {Duration duration = const Duration(milliseconds: 300), int? index}) {
  11. if (index != null) {
  12. items.insert(index, item);
  13. listKey.currentState?.insertItem(index, duration: duration);
  14. } else {
  15. items.add(item);
  16. listKey.currentState?.insertItem(items.length - 1, duration: duration);
  17. }
  18. }
  19. void addAtIndex(int index, List<T> newItems,
  20. {Duration duration = const Duration(milliseconds: 300)}) {
  21. for (var i = 0; i < newItems.length; i++) {
  22. items.insert(index + i, newItems[i]);
  23. listKey.currentState?.insertItem(index + i, duration: duration);
  24. }
  25. }
  26. void addAll(List<T> newItems,
  27. {Duration duration = const Duration(milliseconds: 300)}) {
  28. for (var i = 0; i < newItems.length; i++) {
  29. items.add(newItems[i]);
  30. listKey.currentState?.insertItem(items.length - 1, duration: duration);
  31. }
  32. }
  33. void update(int index, T item) {
  34. items[index] = item;
  35. listKey.currentState?.insertItem(index);
  36. }
  37. void remove(
  38. int index, Widget Function(BuildContext, Animation<double>, T) buildItem,
  39. {Duration duration = const Duration(milliseconds: 300)}) {
  40. final removedItem = items.removeAt(index);
  41. listKey.currentState?.removeItem(
  42. index,
  43. (context, animation) => buildItem(context, animation, removedItem),
  44. duration: duration,
  45. );
  46. }
  47. void removeAtIndex(int index, int count,
  48. Widget Function(BuildContext, Animation<double>, T) buildItem,
  49. {Duration duration = const Duration(milliseconds: 300)}) {
  50. for (var i = 0; i < count; i++) {
  51. final removedItem = items.removeAt(index);
  52. listKey.currentState?.removeItem(
  53. index,
  54. (context, animation) => buildItem(context, animation, removedItem),
  55. duration: duration,
  56. );
  57. }
  58. }
  59. void removeAll(Widget Function(BuildContext, Animation<double>, T) buildItem,
  60. {Duration duration = const Duration(milliseconds: 300)}) {
  61. for (var i = items.length - 1; i >= 0; i--) {
  62. final removedItem = items.removeAt(i);
  63. listKey.currentState?.removeItem(
  64. i,
  65. (context, animation) => buildItem(context, animation, removedItem),
  66. duration: duration,
  67. );
  68. }
  69. }
  70. /// *不带动画****
  71. void clearAt(int index) {
  72. items.removeAt(index);
  73. listKey.currentState
  74. ?.removeItem(index, (context, animation) => Container());
  75. }
  76. void clearItem(int index) {
  77. items.removeAt(index);
  78. listKey.currentState
  79. ?.removeItem(index, (context, animation) => Container());
  80. }
  81. void clearAll() {
  82. items.clear();
  83. listKey.currentState?.removeAllItems((context, animation) => Container());
  84. }
  85. /// *不带动画****
  86. int get length {
  87. return items.length;
  88. }
  89. T get(int index) {
  90. return items[index];
  91. }
  92. }