Fix gets removedItem instead of its index (#119638)
* fix: gets removedItem instead of its index
add: sliver_animated_list.0_test.dart
* fix: sliver_animated_list.0_test.dart
* fix: pr comments
* fix test import
Co-authored-by: Taha Tesser <tessertaha@gmail.com>
---------
Co-authored-by: Taha Tesser <tessertaha@gmail.com>
diff --git a/examples/api/lib/widgets/animated_list/sliver_animated_list.0.dart b/examples/api/lib/widgets/animated_list/sliver_animated_list.0.dart
index 6193a96..34677e5 100644
--- a/examples/api/lib/widgets/animated_list/sliver_animated_list.0.dart
+++ b/examples/api/lib/widgets/animated_list/sliver_animated_list.0.dart
@@ -135,8 +135,8 @@
}
}
-typedef RemovedItemBuilder = Widget Function(
- int item, BuildContext context, Animation<double> animation);
+typedef RemovedItemBuilder<E> = Widget Function(
+ E item, BuildContext context, Animation<double> animation);
// Keeps a Dart [List] in sync with an [AnimatedList].
//
@@ -155,7 +155,7 @@
}) : _items = List<E>.from(initialItems ?? <E>[]);
final GlobalKey<SliverAnimatedListState> listKey;
- final RemovedItemBuilder removedItemBuilder;
+ final RemovedItemBuilder<E> removedItemBuilder;
final List<E> _items;
SliverAnimatedListState get _animatedList => listKey.currentState!;
@@ -171,7 +171,7 @@
_animatedList.removeItem(
index,
(BuildContext context, Animation<double> animation) =>
- removedItemBuilder(index, context, animation),
+ removedItemBuilder(removedItem, context, animation),
);
}
return removedItem;
@@ -197,7 +197,7 @@
this.selected = false,
required this.animation,
required this.item,
- }) : assert(item >= 0);
+ }) : assert(item >= 0);
final Animation<double> animation;
final VoidCallback? onTap;
diff --git a/examples/api/test/widgets/animated_list/sliver_animated_list.0_test.dart b/examples/api/test/widgets/animated_list/sliver_animated_list.0_test.dart
new file mode 100644
index 0000000..8951fb3
--- /dev/null
+++ b/examples/api/test/widgets/animated_list/sliver_animated_list.0_test.dart
@@ -0,0 +1,45 @@
+// Copyright 2014 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'package:flutter/material.dart';
+import 'package:flutter_api_samples/widgets/animated_list/sliver_animated_list.0.dart' as example;
+import 'package:flutter_test/flutter_test.dart';
+
+void main() {
+ testWidgets(
+ 'Items can be selected, added, and removed from SliverAnimatedList',
+ (WidgetTester tester) async {
+ await tester.pumpWidget(const example.SliverAnimatedListSample());
+
+ expect(find.text('Item 0'), findsOneWidget);
+ expect(find.text('Item 1'), findsOneWidget);
+ expect(find.text('Item 2'), findsOneWidget);
+
+ // Add an item at the end of the list
+ await tester.tap(find.byIcon(Icons.add_circle));
+ await tester.pumpAndSettle();
+ expect(find.text('Item 3'), findsOneWidget);
+
+ // Select Item 1.
+ await tester.tap(find.text('Item 1'));
+ await tester.pumpAndSettle();
+
+ // Add item at the top of the list
+ await tester.tap(find.byIcon(Icons.add_circle));
+ await tester.pumpAndSettle();
+ expect(find.text('Item 4'), findsOneWidget);
+
+ // Remove selected item.
+ await tester.tap(find.byIcon(Icons.remove_circle));
+
+ // Item animation is not completed.
+ await tester.pump();
+ expect(find.text('Item 1'), findsOneWidget);
+
+ // When the animation completes, Item 1 disappears.
+ await tester.pumpAndSettle();
+ expect(find.text('Item 1'), findsNothing);
+ },
+ );
+}