From 6388e3d72717c1b7c865b652e9cafcd06dbbff9b Mon Sep 17 00:00:00 2001 From: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com> Date: Wed, 11 Oct 2023 18:17:07 -0700 Subject: [PATCH] Floating `SnackBar` should always float above the bottom widgets (#136411) Fixes #136162 This PR is to fix the floating `SnackBar` issue so that when the top of the bottom bar is higher than the top of the FAB, the snack bar will still float and not be clipped. | Before fix | After fix | |--------|--------| | ![Screenshot 2023-10-11 at 1 54 30 PM](https://github.com/flutter/flutter/assets/36861262/dbe02312-1a80-4532-9bad-a8f162a6a942) | ![Screenshot 2023-10-11 at 1 54 52 PM](https://github.com/flutter/flutter/assets/36861262/68211de0-f04f-4b1a-b554-baf6a8e2f947) | --- .../flutter/lib/src/material/scaffold.dart | 6 ++- .../flutter/test/material/snack_bar_test.dart | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 87fb31857e..9f7e19b266 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -1165,7 +1165,11 @@ class _ScaffoldLayout extends MultiChildLayoutDelegate { FloatingActionButtonLocation() => true, }; if (floatingActionButtonRect.size != Size.zero && isSnackBarFloating && showAboveFab) { - snackBarYOffsetBase = floatingActionButtonRect.top; + if (bottomNavigationBarTop != null) { + snackBarYOffsetBase = math.min(bottomNavigationBarTop, floatingActionButtonRect.top); + } else { + snackBarYOffsetBase = floatingActionButtonRect.top; + } } else { // SnackBarBehavior.fixed applies a SafeArea automatically. // SnackBarBehavior.floating does not since the positioning is affected diff --git a/packages/flutter/test/material/snack_bar_test.dart b/packages/flutter/test/material/snack_bar_test.dart index 1604831abb..db2fd6118a 100644 --- a/packages/flutter/test/material/snack_bar_test.dart +++ b/packages/flutter/test/material/snack_bar_test.dart @@ -2262,6 +2262,44 @@ void main() { }, ); + testWidgetsWithLeakTracking( + '${SnackBarBehavior.floating} should align SnackBar with the top of BottomNavigationBar ' + 'when Scaffold has both BottomNavigationBar and FloatingActionButton and ' + 'BottomNavigationBar.top is higher than FloatingActionButton.top', + (WidgetTester tester) async { + final UniqueKey boxKey = UniqueKey(); + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Container(), + bottomNavigationBar: SizedBox(key: boxKey, width: 800, height: 200), + floatingActionButton: FloatingActionButton(onPressed: () {}), + floatingActionButtonLocation: FloatingActionButtonLocation.endContained, + ), + ), + ); + + final ScaffoldMessengerState scaffoldMessengerState = tester.state(find.byType(ScaffoldMessenger)); + scaffoldMessengerState.showSnackBar( + const SnackBar( + content: Text('SnackBar text'), + behavior: SnackBarBehavior.floating, + ), + ); + + await tester.pumpAndSettle(); // Have the SnackBar fully animate out. + + final Offset snackBarBottomRight = tester.getBottomRight(find.byType(SnackBar)); + final Offset fabTopRight = tester.getTopRight(find.byType(FloatingActionButton)); + final Offset navBarTopRight = tester.getTopRight(find.byKey(boxKey)); + + // Test the top of the navigation bar is higher than the top of the floating action button. + expect(fabTopRight.dy, greaterThan(navBarTopRight.dy)); + + expect(snackBarBottomRight.dy, equals(navBarTopRight.dy)); + }, + ); + Future openFloatingSnackBar(WidgetTester tester) async { final ScaffoldMessengerState scaffoldMessengerState = tester.state(find.byType(ScaffoldMessenger)); scaffoldMessengerState.showSnackBar(