[flutter_adaptive_scaffold] Prevent duplicate keys in `AnimatedSwitcher` (#2725)
diff --git a/packages/flutter_adaptive_scaffold/CHANGELOG.md b/packages/flutter_adaptive_scaffold/CHANGELOG.md
index 20bb4c8..86a954a 100644
--- a/packages/flutter_adaptive_scaffold/CHANGELOG.md
+++ b/packages/flutter_adaptive_scaffold/CHANGELOG.md
@@ -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?`
diff --git a/packages/flutter_adaptive_scaffold/lib/src/slot_layout.dart b/packages/flutter_adaptive_scaffold/lib/src/slot_layout.dart
index 8abbd2a..ce3f5a3 100644
--- a/packages/flutter_adaptive_scaffold/lib/src/slot_layout.dart
+++ b/packages/flutter_adaptive_scaffold/lib/src/slot_layout.dart
@@ -100,7 +100,8 @@
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,
],
);
diff --git a/packages/flutter_adaptive_scaffold/pubspec.yaml b/packages/flutter_adaptive_scaffold/pubspec.yaml
index 59f6f67..01ff56f 100644
--- a/packages/flutter_adaptive_scaffold/pubspec.yaml
+++ b/packages/flutter_adaptive_scaffold/pubspec.yaml
@@ -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
diff --git a/packages/flutter_adaptive_scaffold/test/adaptive_layout_test.dart b/packages/flutter_adaptive_scaffold/test/adaptive_layout_test.dart
index e877cec..848e3e0 100644
--- a/packages/flutter_adaptive_scaffold/test/adaptive_layout_test.dart
+++ b/packages/flutter_adaptive_scaffold/test/adaptive_layout_test.dart
@@ -142,6 +142,35 @@
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));
@@ -159,7 +188,7 @@
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);
@@ -216,7 +245,7 @@
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);
@@ -237,7 +266,7 @@
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);
}