diff --git a/packages/flutter/lib/src/material/navigation_bar.dart b/packages/flutter/lib/src/material/navigation_bar.dart index 7121a08cbf..f55b3602e4 100644 --- a/packages/flutter/lib/src/material/navigation_bar.dart +++ b/packages/flutter/lib/src/material/navigation_bar.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +/// @docImport 'package:flutter/services.dart'; /// @docImport 'bottom_navigation_bar.dart'; /// @docImport 'navigation_rail.dart'; /// @docImport 'scaffold.dart'; @@ -113,6 +114,7 @@ class NavigationBar extends StatelessWidget { this.overlayColor, this.labelTextStyle, this.labelPadding, + this.maintainBottomViewPadding = false, }) : assert(destinations.length >= 2), assert(0 <= selectedIndex && selectedIndex < destinations.length); @@ -245,6 +247,24 @@ class NavigationBar extends StatelessWidget { /// the top. final EdgeInsetsGeometry? labelPadding; + /// Specifies whether the underlying [SafeArea] should maintain the bottom + /// [MediaQueryData.viewPadding] instead of the bottom [MediaQueryData.padding]. + /// + /// When true, this will prevent the [NavigationBar] from shifting when opening a + /// software keyboard due to the change in the padding value, especially when the + /// app uses [SystemUiMode.edgeToEdge], which renders the system bars over the + /// application instead of outside it. + /// + /// Defaults to false. + /// + /// See also: + /// + /// * [SafeArea.maintainBottomViewPadding], which specifies whether the [SafeArea] + /// should maintain the bottom [MediaQueryData.viewPadding]. + /// * [SystemUiMode.edgeToEdge], which sets a fullscreen display with status and + /// navigation elements rendered over the application. + final bool maintainBottomViewPadding; + VoidCallback _handleTap(int index) { return onDestinationSelected != null ? () => onDestinationSelected!(index) : () {}; } @@ -265,6 +285,7 @@ class NavigationBar extends StatelessWidget { surfaceTintColor: surfaceTintColor ?? navigationBarTheme.surfaceTintColor ?? defaults.surfaceTintColor, child: SafeArea( + maintainBottomViewPadding: maintainBottomViewPadding, child: SizedBox( height: effectiveHeight, child: Row( diff --git a/packages/flutter/test/material/navigation_bar_test.dart b/packages/flutter/test/material/navigation_bar_test.dart index 9bc3344715..f11ec3a541 100644 --- a/packages/flutter/test/material/navigation_bar_test.dart +++ b/packages/flutter/test/material/navigation_bar_test.dart @@ -1600,6 +1600,37 @@ void main() { expect(_getLabelStyle(tester, disabledText).fontSize, equals(disabledTextStyle.fontSize)); expect(_getLabelStyle(tester, disabledText).color, equals(disabledTextStyle.color)); }); + + testWidgets('NavigationBar.maintainBottomViewPadding can consume bottom MediaQuery.padding', ( + WidgetTester tester, + ) async { + const double bottomPadding = 40; + const TextDirection textDirection = TextDirection.ltr; + + await tester.pumpWidget( + MaterialApp( + home: Directionality( + textDirection: textDirection, + child: MediaQuery( + data: const MediaQueryData(padding: EdgeInsets.only(bottom: bottomPadding)), + child: Scaffold( + bottomNavigationBar: NavigationBar( + maintainBottomViewPadding: true, + destinations: const [ + NavigationDestination(icon: Icon(Icons.ac_unit), label: 'AC'), + NavigationDestination(icon: Icon(Icons.access_alarm), label: 'Alarm'), + ], + ), + ), + ), + ), + ), + ); + + final double safeAreaBottomPadding = + tester.widget(find.byType(Padding).first).padding.resolve(textDirection).bottom; + expect(safeAreaBottomPadding, equals(0)); + }); } Widget _buildWidget(Widget child, {bool? useMaterial3}) {