diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 7757cf1025..b5c62ab7c0 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -2124,6 +2124,17 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto return false; } + // Stop the animation and unmount the dismissed sheets from the tree immediately, + // otherwise may cause duplicate GlobalKey assertion if the sheet sub-tree contains + // GlobalKey widgets. + if (_dismissedBottomSheets.isNotEmpty) { + final List<_StandardBottomSheet> sheets = List<_StandardBottomSheet>.of(_dismissedBottomSheets, growable: false); + for (final _StandardBottomSheet sheet in sheets) { + sheet.animationController.reset(); + } + assert(_dismissedBottomSheets.isEmpty); + } + _currentBottomSheet = _buildBottomSheet( (BuildContext context) { return NotificationListener( diff --git a/packages/flutter/test/material/persistent_bottom_sheet_test.dart b/packages/flutter/test/material/persistent_bottom_sheet_test.dart index dada826f03..17302b37aa 100644 --- a/packages/flutter/test/material/persistent_bottom_sheet_test.dart +++ b/packages/flutter/test/material/persistent_bottom_sheet_test.dart @@ -22,6 +22,24 @@ void main() { expect(dyDelta1, isNot(moreOrLessEquals(dyDelta2, epsilon: 0.1))); } + // Regression test for https://github.com/flutter/flutter/issues/83668 + testWidgets('Scaffold.bottomSheet update test', (WidgetTester tester) async { + Widget buildFrame(Widget? bottomSheet) { + return MaterialApp( + home: Scaffold( + body: const Placeholder(), + bottomSheet: bottomSheet, + ), + ); + } + + await tester.pumpWidget(buildFrame(const Text('I love Flutter!'))); + await tester.pumpWidget(buildFrame(null)); + + // The disappearing animation has not yet been completed. + await tester.pumpWidget(buildFrame(const Text('I love Flutter!'))); + }); + testWidgets('Verify that a BottomSheet can be rebuilt with ScaffoldFeatureController.setState()', (WidgetTester tester) async { final GlobalKey scaffoldKey = GlobalKey(); int buildCount = 0;