Skip to content

Commit e2d1206

Browse files
authored
Fix Backbutton is not displayed when there is a endDrawer (#101869)
1 parent c165b74 commit e2d1206

File tree

4 files changed

+55
-11
lines changed

4 files changed

+55
-11
lines changed

packages/flutter/lib/src/material/app_bar.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -823,9 +823,9 @@ class _AppBarState extends State<AppBar> {
823823

824824
final bool hasDrawer = scaffold?.hasDrawer ?? false;
825825
final bool hasEndDrawer = scaffold?.hasEndDrawer ?? false;
826-
final bool canPop = parentRoute?.canPop ?? false;
827826
final bool useCloseButton = parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog;
828-
827+
final bool requiresAppBarDismiss = scaffold?.requiresAppBarDismiss ?? false;
828+
final bool hasActiveRouteBelow = parentRoute?.hasActiveRouteBelow ?? false;
829829
final double toolbarHeight = widget.toolbarHeight ?? appBarTheme.toolbarHeight ?? kToolbarHeight;
830830
final bool backwardsCompatibility = widget.backwardsCompatibility ?? appBarTheme.backwardsCompatibility ?? false;
831831

@@ -896,7 +896,7 @@ class _AppBarState extends State<AppBar> {
896896
tooltip: MaterialLocalizations.of(context).openAppDrawerTooltip,
897897
);
898898
} else {
899-
if (!hasEndDrawer && canPop)
899+
if (hasActiveRouteBelow || requiresAppBarDismiss)
900900
leading = useCloseButton ? const CloseButton() : const BackButton();
901901
}
902902
}

packages/flutter/lib/src/material/scaffold.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,13 +1917,20 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin, Resto
19171917

19181918
/// Whether this scaffold has a non-null [Scaffold.appBar].
19191919
bool get hasAppBar => widget.appBar != null;
1920+
19201921
/// Whether this scaffold has a non-null [Scaffold.drawer].
19211922
bool get hasDrawer => widget.drawer != null;
1923+
19221924
/// Whether this scaffold has a non-null [Scaffold.endDrawer].
19231925
bool get hasEndDrawer => widget.endDrawer != null;
1926+
19241927
/// Whether this scaffold has a non-null [Scaffold.floatingActionButton].
19251928
bool get hasFloatingActionButton => widget.floatingActionButton != null;
19261929

1930+
/// Whether this scaffold requires [Scaffold.appBar] to automatically add
1931+
/// dismiss button.
1932+
bool get requiresAppBarDismiss => _persistentSheetHistoryEntry != null;
1933+
19271934
double? _appBarMaxHeight;
19281935
/// The max height the [Scaffold.appBar] uses.
19291936
///
@@ -2051,28 +2058,28 @@ class ScaffoldState extends State<Scaffold> with TickerProviderStateMixin, Resto
20512058
PersistentBottomSheetController<dynamic>? _currentBottomSheet;
20522059
final GlobalKey _currentBottomSheetKey = GlobalKey();
20532060

2061+
LocalHistoryEntry? _persistentSheetHistoryEntry;
20542062
void _maybeBuildPersistentBottomSheet() {
20552063
if (widget.bottomSheet != null && _currentBottomSheet == null) {
20562064
// The new _currentBottomSheet is not a local history entry so a "back" button
20572065
// will not be added to the Scaffold's appbar and the bottom sheet will not
20582066
// support drag or swipe to dismiss.
20592067
final AnimationController animationController = BottomSheet.createAnimationController(this)..value = 1.0;
2060-
LocalHistoryEntry? persistentSheetHistoryEntry;
20612068
bool _persistentBottomSheetExtentChanged(DraggableScrollableNotification notification) {
20622069
if (notification.extent > notification.initialExtent) {
2063-
if (persistentSheetHistoryEntry == null) {
2064-
persistentSheetHistoryEntry = LocalHistoryEntry(onRemove: () {
2070+
if (_persistentSheetHistoryEntry == null) {
2071+
_persistentSheetHistoryEntry = LocalHistoryEntry(onRemove: () {
20652072
if (notification.extent > notification.initialExtent) {
20662073
DraggableScrollableActuator.reset(notification.context);
20672074
}
20682075
showBodyScrim(false, 0.0);
20692076
_floatingActionButtonVisibilityValue = 1.0;
2070-
persistentSheetHistoryEntry = null;
2077+
_persistentSheetHistoryEntry = null;
20712078
});
2072-
ModalRoute.of(context)!.addLocalHistoryEntry(persistentSheetHistoryEntry!);
2079+
ModalRoute.of(context)!.addLocalHistoryEntry(_persistentSheetHistoryEntry!);
20732080
}
2074-
} else if (persistentSheetHistoryEntry != null) {
2075-
ModalRoute.of(context)!.removeLocalHistoryEntry(persistentSheetHistoryEntry!);
2081+
} else if (_persistentSheetHistoryEntry != null) {
2082+
ModalRoute.of(context)!.removeLocalHistoryEntry(_persistentSheetHistoryEntry!);
20762083
}
20772084
return false;
20782085
}

packages/flutter/lib/src/widgets/navigator.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ abstract class Route<T> {
486486
}
487487

488488
/// Whether there is at least one active route underneath this route.
489-
@protected
490489
bool get hasActiveRouteBelow {
491490
if (_navigator == null)
492491
return false;

packages/flutter/test/material/app_bar_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,6 +2873,44 @@ void main() {
28732873
expect(tester.getSize(findAppBarMaterial()).height, kToolbarHeight);
28742874
});
28752875

2876+
// Regression test for https://github.com/flutter/flutter/issues/80256
2877+
testWidgets('The second page should have a back button even it has a end drawer', (WidgetTester tester) async {
2878+
final Page<void> page1 = MaterialPage<void>(
2879+
key: const ValueKey<String>('1'),
2880+
child: Scaffold(
2881+
key: const ValueKey<String>('1'),
2882+
appBar: AppBar(),
2883+
endDrawer: const Drawer(),
2884+
)
2885+
);
2886+
final Page<void> page2 = MaterialPage<void>(
2887+
key: const ValueKey<String>('2'),
2888+
child: Scaffold(
2889+
key: const ValueKey<String>('2'),
2890+
appBar: AppBar(),
2891+
endDrawer: const Drawer(),
2892+
)
2893+
);
2894+
final List<Page<void>> pages = <Page<void>>[ page1, page2 ];
2895+
await tester.pumpWidget(
2896+
MaterialApp(
2897+
home: Navigator(
2898+
pages: pages,
2899+
onPopPage: (Route<Object?> route, Object? result) => false,
2900+
),
2901+
),
2902+
);
2903+
2904+
// The page2 should have a back button.
2905+
expect(
2906+
find.descendant(
2907+
of: find.byKey(const ValueKey<String>('2')),
2908+
matching: find.byType(BackButton),
2909+
),
2910+
findsOneWidget
2911+
);
2912+
});
2913+
28762914
testWidgets('backgroundColor with FlexibleSpace - reverse', (WidgetTester tester) async {
28772915
await tester.pumpWidget(
28782916
_buildAppBar(

0 commit comments

Comments
 (0)