Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 9b86a48

Browse files
jehhxuxuTahaTesser
andauthored
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 <[email protected]> --------- Co-authored-by: Taha Tesser <[email protected]>
1 parent d875899 commit 9b86a48

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

examples/api/lib/widgets/animated_list/sliver_animated_list.0.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ class _SliverAnimatedListSampleState extends State<SliverAnimatedListSample> {
135135
}
136136
}
137137

138-
typedef RemovedItemBuilder = Widget Function(
139-
int item, BuildContext context, Animation<double> animation);
138+
typedef RemovedItemBuilder<E> = Widget Function(
139+
E item, BuildContext context, Animation<double> animation);
140140

141141
// Keeps a Dart [List] in sync with an [AnimatedList].
142142
//
@@ -155,7 +155,7 @@ class ListModel<E> {
155155
}) : _items = List<E>.from(initialItems ?? <E>[]);
156156

157157
final GlobalKey<SliverAnimatedListState> listKey;
158-
final RemovedItemBuilder removedItemBuilder;
158+
final RemovedItemBuilder<E> removedItemBuilder;
159159
final List<E> _items;
160160

161161
SliverAnimatedListState get _animatedList => listKey.currentState!;
@@ -171,7 +171,7 @@ class ListModel<E> {
171171
_animatedList.removeItem(
172172
index,
173173
(BuildContext context, Animation<double> animation) =>
174-
removedItemBuilder(index, context, animation),
174+
removedItemBuilder(removedItem, context, animation),
175175
);
176176
}
177177
return removedItem;
@@ -197,7 +197,7 @@ class CardItem extends StatelessWidget {
197197
this.selected = false,
198198
required this.animation,
199199
required this.item,
200-
}) : assert(item >= 0);
200+
}) : assert(item >= 0);
201201

202202
final Animation<double> animation;
203203
final VoidCallback? onTap;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_api_samples/widgets/animated_list/sliver_animated_list.0.dart' as example;
7+
import 'package:flutter_test/flutter_test.dart';
8+
9+
void main() {
10+
testWidgets(
11+
'Items can be selected, added, and removed from SliverAnimatedList',
12+
(WidgetTester tester) async {
13+
await tester.pumpWidget(const example.SliverAnimatedListSample());
14+
15+
expect(find.text('Item 0'), findsOneWidget);
16+
expect(find.text('Item 1'), findsOneWidget);
17+
expect(find.text('Item 2'), findsOneWidget);
18+
19+
// Add an item at the end of the list
20+
await tester.tap(find.byIcon(Icons.add_circle));
21+
await tester.pumpAndSettle();
22+
expect(find.text('Item 3'), findsOneWidget);
23+
24+
// Select Item 1.
25+
await tester.tap(find.text('Item 1'));
26+
await tester.pumpAndSettle();
27+
28+
// Add item at the top of the list
29+
await tester.tap(find.byIcon(Icons.add_circle));
30+
await tester.pumpAndSettle();
31+
expect(find.text('Item 4'), findsOneWidget);
32+
33+
// Remove selected item.
34+
await tester.tap(find.byIcon(Icons.remove_circle));
35+
36+
// Item animation is not completed.
37+
await tester.pump();
38+
expect(find.text('Item 1'), findsOneWidget);
39+
40+
// When the animation completes, Item 1 disappears.
41+
await tester.pumpAndSettle();
42+
expect(find.text('Item 1'), findsNothing);
43+
},
44+
);
45+
}

0 commit comments

Comments
 (0)