From 148be5f894aa64d7c4e8318100b5db5ae3c9b823 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Tue, 12 Nov 2024 18:33:51 +0200 Subject: [PATCH] Fix `NavigationBar` label style customization on the widget level (#158510) Fixes [`NavigationBar` only allows `labelTextStyle` from `NavigationBarTheme` ](https://github.com/flutter/flutter/issues/158508) ### Code sample
expand to view the code sample ```dart import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { const TextStyle selectedTextStyle = TextStyle(fontSize: 15, color: Color(0xFF00FF00)); const TextStyle unselectedTextStyle = TextStyle(fontSize: 15, color: Color(0xFF0000FF)); const TextStyle disabledTextStyle = TextStyle(fontSize: 16, color: Color(0xFFFF0000)); return MaterialApp( debugShowCheckedModeBanner: false, theme: ThemeData( navigationBarTheme: NavigationBarThemeData( labelTextStyle: WidgetStatePropertyAll( TextStyle( fontSize: 14, fontWeight: FontWeight.bold, color: Theme.of(context).colorScheme.primary, ), ), )), home: Scaffold( appBar: AppBar( title: const Text('NavigationBar Sample'), ), body: Center( child: Padding( padding: const EdgeInsets.all(16.0), child: Text( "NavigationBar labels are customized using NavigationBar.labelTextStyle", style: Theme.of(context).textTheme.headlineSmall, textAlign: TextAlign.center, ), ), ), bottomNavigationBar: NavigationBar( labelTextStyle: const WidgetStateProperty.fromMap( { WidgetState.disabled: disabledTextStyle, WidgetState.selected: selectedTextStyle, WidgetState.any: unselectedTextStyle, }, ), onDestinationSelected: null, destinations: const [ NavigationDestination( icon: Icon(Icons.home), label: 'Home', ), NavigationDestination( icon: Icon(Icons.favorite_rounded), label: 'Favorites', ), NavigationDestination( enabled: false, icon: Icon(Icons.settings), label: 'Settings', ), ], ), ), ); } } ```
### Preview Screenshot 2024-11-12 at 17 18 00 --- .../lib/src/material/navigation_bar.dart | 28 +++++- .../test/material/navigation_bar_test.dart | 95 +++++++++++++++++-- 2 files changed, 113 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/src/material/navigation_bar.dart b/packages/flutter/lib/src/material/navigation_bar.dart index 5c1311f163..678971499e 100644 --- a/packages/flutter/lib/src/material/navigation_bar.dart +++ b/packages/flutter/lib/src/material/navigation_bar.dart @@ -111,6 +111,7 @@ class NavigationBar extends StatelessWidget { this.height, this.labelBehavior, this.overlayColor, + this.labelTextStyle, this.labelPadding, }) : assert(destinations.length >= 2), assert(0 <= selectedIndex && selectedIndex < destinations.length); @@ -224,6 +225,19 @@ class NavigationBar extends StatelessWidget { /// the [NavigationDestination] is focused, hovered, or pressed. final MaterialStateProperty? overlayColor; + //// The text style of the label. + /// + /// If null, [NavigationBarThemeData.labelTextStyle] is used. If that + /// is also null, the default text style is [TextTheme.labelMedium] with + /// [ColorScheme.onSurface] when the destination is selected, and + /// [ColorScheme.onSurfaceVariant] when the destination is unselected, and + /// [ColorScheme.onSurfaceVariant] with an opacity of 0.38 when the + /// destination is disabled. + /// + /// If [ThemeData.useMaterial3] is false, then the default text style is + /// [TextTheme.labelSmall] with [ColorScheme.onSurface]. + final MaterialStateProperty? labelTextStyle; + /// The padding around the [NavigationDestination.label] widget. /// /// When [labelPadding] is null, [NavigationBarThemeData.labelPadding] @@ -275,6 +289,7 @@ class NavigationBar extends StatelessWidget { indicatorShape: indicatorShape, overlayColor: overlayColor, onTap: _handleTap(i), + labelTextStyle: labelTextStyle, labelPadding: labelPadding, child: destinations[i], ); @@ -426,11 +441,14 @@ class NavigationDestination extends StatelessWidget { ); }, buildLabel: (BuildContext context) { - final TextStyle? effectiveSelectedLabelTextStyle = navigationBarTheme.labelTextStyle?.resolve(selectedState) + final TextStyle? effectiveSelectedLabelTextStyle = info.labelTextStyle?.resolve(selectedState) + ?? navigationBarTheme.labelTextStyle?.resolve(selectedState) ?? defaults.labelTextStyle!.resolve(selectedState); - final TextStyle? effectiveUnselectedLabelTextStyle = navigationBarTheme.labelTextStyle?.resolve(unselectedState) + final TextStyle? effectiveUnselectedLabelTextStyle = info.labelTextStyle?.resolve(unselectedState) + ?? navigationBarTheme.labelTextStyle?.resolve(unselectedState) ?? defaults.labelTextStyle!.resolve(unselectedState); - final TextStyle? effectiveDisabledLabelTextStyle = navigationBarTheme.labelTextStyle?.resolve(disabledState) + final TextStyle? effectiveDisabledLabelTextStyle = info.labelTextStyle?.resolve(disabledState) + ?? navigationBarTheme.labelTextStyle?.resolve(disabledState) ?? defaults.labelTextStyle!.resolve(disabledState); final EdgeInsetsGeometry labelPadding = info.labelPadding ?? navigationBarTheme.labelPadding @@ -604,6 +622,7 @@ class _NavigationDestinationInfo extends InheritedWidget { required this.indicatorShape, required this.overlayColor, required this.onTap, + this.labelTextStyle, this.labelPadding, required super.child, }); @@ -682,6 +701,9 @@ class _NavigationDestinationInfo extends InheritedWidget { /// with [index] passed in. final VoidCallback onTap; + /// The text style of the label. + final MaterialStateProperty? labelTextStyle; + /// The padding around the [label] widget. /// /// Defaults to a padding of 4 pixels on the top. diff --git a/packages/flutter/test/material/navigation_bar_test.dart b/packages/flutter/test/material/navigation_bar_test.dart index 02f7c6b17a..5e38014a69 100644 --- a/packages/flutter/test/material/navigation_bar_test.dart +++ b/packages/flutter/test/material/navigation_bar_test.dart @@ -1567,21 +1567,22 @@ void main() { }); testWidgets('NavigationBar.labelPadding overrides NavigationDestination.label padding', (WidgetTester tester) async { + const String selectedText = 'Home'; + const String unselectedText = 'Settings'; const EdgeInsetsGeometry labelPadding = EdgeInsets.all(8); Widget buildNavigationBar({ EdgeInsetsGeometry? labelPadding }) { return MaterialApp( - theme: ThemeData(useMaterial3: false), home: Scaffold( bottomNavigationBar: NavigationBar( labelPadding: labelPadding, destinations: const [ NavigationDestination( icon: Icon(Icons.home), - label: 'Home', + label: selectedText, ), NavigationDestination( icon: Icon(Icons.settings), - label: 'Settings', + label: unselectedText, ), ], onDestinationSelected: (int i) { }, @@ -1591,12 +1592,83 @@ void main() { } await tester.pumpWidget(buildNavigationBar()); - expect(_getLabelPadding(tester, 'Home'), const EdgeInsets.only(top: 4)); - expect(_getLabelPadding(tester, 'Settings'), const EdgeInsets.only(top: 4)); + expect(_getLabelPadding(tester, selectedText), const EdgeInsets.only(top: 4)); + expect(_getLabelPadding(tester, unselectedText), const EdgeInsets.only(top: 4)); await tester.pumpWidget(buildNavigationBar(labelPadding: labelPadding)); - expect(_getLabelPadding(tester, 'Home'), labelPadding); - expect(_getLabelPadding(tester, 'Settings'), labelPadding); + expect(_getLabelPadding(tester, selectedText), labelPadding); + expect(_getLabelPadding(tester, unselectedText), labelPadding); + }); + + testWidgets('NavigationBar.labelTextStyle overrides NavigationDestination.label text style', (WidgetTester tester) async { + const String selectedText = 'Home'; + const String unselectedText = 'Settings'; + const String disabledText = 'Bookmark'; + final ThemeData theme = ThemeData(); + Widget buildNavigationBar({ WidgetStateProperty? labelTextStyle }) { + return MaterialApp( + theme: theme, + home: Scaffold( + bottomNavigationBar: NavigationBar( + labelTextStyle: labelTextStyle, + destinations: const [ + NavigationDestination( + icon: Icon(Icons.home), + label: selectedText, + ), + NavigationDestination( + icon: Icon(Icons.settings), + label: unselectedText, + ), + NavigationDestination( + enabled: false, + icon: Icon(Icons.bookmark), + label: disabledText, + ), + ], + ), + ), + ); + } + + await tester.pumpWidget(buildNavigationBar()); + + // Test selected label text style. + expect(_getLabelStyle(tester, selectedText).fontSize, equals(12.0)); + expect(_getLabelStyle(tester, selectedText).color, equals(theme.colorScheme.onSurface)); + + // Test unselected label text style. + expect(_getLabelStyle(tester, unselectedText).fontSize, equals(12.0)); + expect(_getLabelStyle(tester, unselectedText).color, equals(theme.colorScheme.onSurfaceVariant)); + + // Test disabled label text style. + expect(_getLabelStyle(tester, disabledText).fontSize, equals(12.0)); + expect(_getLabelStyle(tester, disabledText).color, equals(theme.colorScheme.onSurfaceVariant.withOpacity(0.38))); + + const TextStyle selectedTextStyle = TextStyle(fontSize: 15, color: Color(0xFF00FF00)); + const TextStyle unselectedTextStyle = TextStyle(fontSize: 15, color: Color(0xFF0000FF)); + const TextStyle disabledTextStyle = TextStyle(fontSize: 16, color: Color(0xFFFF0000)); + await tester.pumpWidget(buildNavigationBar( + labelTextStyle: const WidgetStateProperty.fromMap( + { + WidgetState.disabled: disabledTextStyle, + WidgetState.selected: selectedTextStyle, + WidgetState.any: unselectedTextStyle, + }, + ), + )); + + // Test selected label text style. + expect(_getLabelStyle(tester, selectedText).fontSize, equals(selectedTextStyle.fontSize)); + expect(_getLabelStyle(tester, selectedText).color, equals(selectedTextStyle.color)); + + // Test unselected label text style. + expect(_getLabelStyle(tester, unselectedText).fontSize, equals(unselectedTextStyle.fontSize)); + expect(_getLabelStyle(tester, unselectedText).color, equals(unselectedTextStyle.color)); + + // Test disabled label text style. + expect(_getLabelStyle(tester, disabledText).fontSize, equals(disabledTextStyle.fontSize)); + expect(_getLabelStyle(tester, disabledText).color, equals(disabledTextStyle.color)); }); } @@ -1651,3 +1723,12 @@ EdgeInsetsGeometry _getLabelPadding(WidgetTester tester, String text) { ).first, ).padding; } + +TextStyle _getLabelStyle(WidgetTester tester, String text) { + return tester.widget( + find.descendant( + of: find.text(text), + matching: find.byType(RichText), + ), + ).text.style!; +}