From 16dda865393f9b7df93102ebc8cf1b7ca7b0f0da Mon Sep 17 00:00:00 2001 From: xubaolin Date: Wed, 23 Mar 2022 06:55:13 +0800 Subject: [PATCH] Fix a ModalbottomSheet bug (#99970) --- .../flutter/lib/src/material/scaffold.dart | 19 +++++--- .../test/material/bottom_sheet_test.dart | 44 +++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 8f7227f1a2..e5114c1da2 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -2357,6 +2357,7 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto late _StandardBottomSheet bottomSheet; bool removedEntry = false; + bool doingDispose = false; void _removeCurrentBottomSheet() { removedEntry = true; if (_currentBottomSheet == null) { @@ -2380,11 +2381,19 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto final LocalHistoryEntry? entry = isPersistent ? null : LocalHistoryEntry(onRemove: () { - if (!removedEntry && _currentBottomSheet?._widget == bottomSheet) { + if (!removedEntry && _currentBottomSheet?._widget == bottomSheet && !doingDispose) { _removeCurrentBottomSheet(); } }); + void _removeEntryIfNeeded() { + if (!isPersistent && !removedEntry) { + assert(entry != null); + entry!.remove(); + removedEntry = true; + } + } + bottomSheet = _StandardBottomSheet( key: bottomSheetKey, animationController: animationController, @@ -2394,11 +2403,7 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto return; } assert(_currentBottomSheet!._widget == bottomSheet); - if (!isPersistent && !removedEntry) { - assert(entry != null); - entry!.remove(); - removedEntry = true; - } + _removeEntryIfNeeded(); }, onDismissed: () { if (_dismissedBottomSheets.contains(bottomSheet)) { @@ -2408,6 +2413,8 @@ class ScaffoldState extends State with TickerProviderStateMixin, Resto } }, onDispose: () { + doingDispose = true; + _removeEntryIfNeeded(); if (shouldDisposeAnimationController) { animationController.dispose(); } diff --git a/packages/flutter/test/material/bottom_sheet_test.dart b/packages/flutter/test/material/bottom_sheet_test.dart index f2dcd2b6d4..f23eb3042a 100644 --- a/packages/flutter/test/material/bottom_sheet_test.dart +++ b/packages/flutter/test/material/bottom_sheet_test.dart @@ -1149,6 +1149,50 @@ void main() { expect(tester.takeException(), isNull); }); + // Regression test for https://github.com/flutter/flutter/issues/99627 + testWidgets('The old route entry should be removed when a new sheet popup', (WidgetTester tester) async { + final GlobalKey scaffoldKey = GlobalKey(); + PersistentBottomSheetController? sheetController; + + await tester.pumpWidget(MaterialApp( + home: Scaffold( + key: scaffoldKey, + body: const Center(child: Text('body')), + ), + )); + + final ModalRoute route = ModalRoute.of(scaffoldKey.currentContext!)!; + expect(route.canPop, false); + + scaffoldKey.currentState!.showBottomSheet((_) { + return Builder( + builder: (BuildContext context) { + return Container(height: 200.0); + }, + ); + }); + + await tester.pump(); + expect(find.byType(BottomSheet), findsOneWidget); + expect(route.canPop, true); + + // Trigger the second sheet will remove the first sheet from tree. + sheetController = scaffoldKey.currentState!.showBottomSheet((_) { + return Builder( + builder: (BuildContext context) { + return Container(height: 200.0); + }, + ); + }); + await tester.pump(); + expect(find.byType(BottomSheet), findsOneWidget); + expect(route.canPop, true); + + sheetController.close(); + + expect(route.canPop, false); + }); + // Regression test for https://github.com/flutter/flutter/issues/87708 testWidgets('The framework does not dispose of the transitionAnimationController provided by user.', (WidgetTester tester) async { const Key tapTarget = Key('tap-target');