From cf3ed1ee17a0891821934f39b3c1d57f8373a261 Mon Sep 17 00:00:00 2001 From: Shashi <38147403+sharmashashi@users.noreply.github.com> Date: Fri, 15 Dec 2023 03:16:51 +0545 Subject: [PATCH] Fix BottomNavigationBarItem label overflow (#120206) This PR wraps the `label` with `IntrinsicWidth` and then `Flexible` which allows DefaulTextStyle `TextOverflow.ellipsis` to work. Wrapping `label` directly with `Flexible` brings more space between `icon` and `label`. `IntrinsicWidth` fixes this by giving reasonable width. Fixes #112163 --- .../src/material/bottom_navigation_bar.dart | 9 +++- .../material/bottom_navigation_bar_test.dart | 53 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/material/bottom_navigation_bar.dart b/packages/flutter/lib/src/material/bottom_navigation_bar.dart index dd4f4f0407..3e57788995 100644 --- a/packages/flutter/lib/src/material/bottom_navigation_bar.dart +++ b/packages/flutter/lib/src/material/bottom_navigation_bar.dart @@ -647,7 +647,14 @@ class _Tile extends StatelessWidget { heightFactor: 1, child: Row( mainAxisSize: MainAxisSize.min, - children: [icon, const SizedBox(width: 8), label], + children: [ + icon, + const SizedBox(width: 8), + // Flexible lets the overflow property of + // label to work and IntrinsicWidth gives label a + // resonable width preventing extra space before it. + Flexible(child: IntrinsicWidth(child: label)) + ], ), ); } diff --git a/packages/flutter/test/material/bottom_navigation_bar_test.dart b/packages/flutter/test/material/bottom_navigation_bar_test.dart index c724bba3ca..568c1b0e86 100644 --- a/packages/flutter/test/material/bottom_navigation_bar_test.dart +++ b/packages/flutter/test/material/bottom_navigation_bar_test.dart @@ -3122,6 +3122,59 @@ void main() { expect(tester.getRect(find.byKey(icon0)), Rect.fromLTRB(firstItemLeft, iconTop, firstItemLeft + iconWidth, iconTop + iconHeight)); expect(tester.getRect(find.byKey(icon1)), Rect.fromLTRB(secondItemLeft, iconTop, secondItemLeft + iconWidth, iconTop + iconHeight)); }, skip: kIsWeb && !isCanvasKit); // https://github.com/flutter/flutter/issues/99933 + + testWidgets('BottomNavigationBar linear landscape layout label RenderFlex overflow',(WidgetTester tester) async { + //Regression test for https://github.com/flutter/flutter/issues/112163 + + tester.view.physicalSize = const Size(540, 340); + + await tester.pumpWidget( + MaterialApp( + home: Builder( + builder: (BuildContext context) { + return Scaffold( + bottomNavigationBar: BottomNavigationBar( + type: BottomNavigationBarType.fixed, + landscapeLayout: BottomNavigationBarLandscapeLayout.linear, + items: const [ + BottomNavigationBarItem( + icon: Icon(Icons.home_rounded), + label: 'Home Challenges', + backgroundColor: Colors.grey, + tooltip: '', + ), + BottomNavigationBarItem( + icon: Icon(Icons.date_range_rounded), + label: 'Daily Challenges', + backgroundColor: Colors.grey, + tooltip: '', + ), + BottomNavigationBarItem( + icon: Icon(Icons.wind_power), + label: 'Awards Challenges', + backgroundColor: Colors.grey, + tooltip: '', + ), + BottomNavigationBarItem( + icon: Icon(Icons.bar_chart_rounded), + label: 'Statistics Challenges', + backgroundColor: Colors.grey, + tooltip: '', + ), + ], + )); + }, + ), + ), + ); + + await expectLater( + find.byType(MaterialApp), + matchesGoldenFile('bottom_navigation_bar.label_overflow.png'), + ); + + addTearDown(tester.view.resetPhysicalSize); + }); } Widget boilerplate({ Widget? bottomNavigationBar, required TextDirection textDirection, bool? useMaterial3 }) {