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 6193a963ff..34677e5417 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 @@ class _SliverAnimatedListSampleState extends State { } } -typedef RemovedItemBuilder = Widget Function( - int item, BuildContext context, Animation animation); +typedef RemovedItemBuilder = Widget Function( + E item, BuildContext context, Animation animation); // Keeps a Dart [List] in sync with an [AnimatedList]. // @@ -155,7 +155,7 @@ class ListModel { }) : _items = List.from(initialItems ?? []); final GlobalKey listKey; - final RemovedItemBuilder removedItemBuilder; + final RemovedItemBuilder removedItemBuilder; final List _items; SliverAnimatedListState get _animatedList => listKey.currentState!; @@ -171,7 +171,7 @@ class ListModel { _animatedList.removeItem( index, (BuildContext context, Animation animation) => - removedItemBuilder(index, context, animation), + removedItemBuilder(removedItem, context, animation), ); } return removedItem; @@ -197,7 +197,7 @@ class CardItem extends StatelessWidget { this.selected = false, required this.animation, required this.item, - }) : assert(item >= 0); + }) : assert(item >= 0); final Animation 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 0000000000..8951fb3e2d --- /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); + }, + ); +}