Skip to content

Commit

Permalink
[flutter_adaptive_scaffold] Prevent duplicate keys in `AnimatedSwitch…
Browse files Browse the repository at this point in the history
…er` (flutter#2725)
  • Loading branch information
a-wallen authored Oct 24, 2022
1 parent d0af2aa commit b85f308
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/flutter_adaptive_scaffold/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.7

* Patch duplicate key error in SlotLayout.

## 0.0.6

* Change type of `appBar` parameter from `AppBar?` to `PreferredSizeWidget?`
Expand Down
3 changes: 2 additions & 1 deletion packages/flutter_adaptive_scaffold/lib/src/slot_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ class _SlotLayoutState extends State<SlotLayout>
layoutBuilder: (Widget? currentChild, List<Widget> previousChildren) {
final Stack elements = Stack(
children: <Widget>[
if (hasAnimation) ...previousChildren,
if (hasAnimation && previousChildren.isNotEmpty)
previousChildren.first,
if (currentChild != null) currentChild,
],
);
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_adaptive_scaffold/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_adaptive_scaffold
description: Widgets to easily build adaptive layouts, including navigation elements.
version: 0.0.6
version: 0.0.7
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_adaptive_scaffold%22
repository: https://github.com/flutter/packages/tree/main/packages/flutter_adaptive_scaffold

Expand Down
35 changes: 32 additions & 3 deletions packages/flutter_adaptive_scaffold/test/adaptive_layout_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,35 @@ void main() {
expect(begin, findsNothing);
expect(end, findsOneWidget);
});

testWidgets('AnimatedSwitcher does not spawn duplicate keys on rapid resize',
(WidgetTester tester) async {
// Populate the smaller slot layout and let the animation settle.
await tester.pumpWidget(slot(300));
await tester.pumpAndSettle();
expect(begin, findsOneWidget);
expect(end, findsNothing);

// Jumping back between two layouts before allowing an animation to complete.
// Produces a chain of widgets in AnimatedSwitcher that includes duplicate
// widgets with the same global key.
for (int i = 0; i < 2; i++) {
// Resize between the two slot layouts, but do not pump the animation
// until completion.
await tester.pumpWidget(slot(500));
await tester.pump(const Duration(milliseconds: 100));
expect(begin, findsOneWidget);
expect(end, findsOneWidget);

await tester.pumpWidget(slot(300));
await tester.pump(const Duration(milliseconds: 100));
expect(begin, findsOneWidget);
expect(end, findsOneWidget);
}
// TODO(gspencergoog): Remove skip when AnimatedSwitcher fix rolls into stable.
// https://github.com/flutter/flutter/pull/107476
}, skip: true);

testWidgets('slot layout can tolerate rapid changes in breakpoints',
(WidgetTester tester) async {
await tester.pumpWidget(slot(300));
Expand All @@ -159,7 +188,7 @@ void main() {
await tester.pumpAndSettle();
expect(begin, findsOneWidget);
expect(end, findsNothing);
// TODO(gspencergoog): Remove skip when AnimatedSwitcher fix rolls into stable.
// TODO(a-wallen): Remove skip when AnimatedSwitcher fix rolls into stable.
// https://github.com/flutter/flutter/pull/107476
}, skip: true);

Expand Down Expand Up @@ -216,7 +245,7 @@ void main() {
expect(tester.getTopLeft(secondaryTestBreakpoint), const Offset(200, 10));
expect(
tester.getBottomRight(secondaryTestBreakpoint), const Offset(390, 790));
// TODO(gspencergoog): Remove skip when AnimatedSwitcher fix rolls into stable.
// TODO(a-wallen): Remove skip when AnimatedSwitcher fix rolls into stable.
// https://github.com/flutter/flutter/pull/107476
}, skip: true);

Expand All @@ -237,7 +266,7 @@ void main() {
expect(tester.getTopLeft(secondaryTestBreakpoint), const Offset(200, 10));
expect(
tester.getBottomRight(secondaryTestBreakpoint), const Offset(390, 790));
// TODO(gspencergoog): Remove skip when AnimatedSwitcher fix rolls into stable.
// TODO(a-wallen): Remove skip when AnimatedSwitcher fix rolls into stable.
// https://github.com/flutter/flutter/pull/107476
}, skip: true);
}
Expand Down

0 comments on commit b85f308

Please sign in to comment.