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

<details>
<summary>expand to view the code sample</summary> 

```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<TextStyle?>.fromMap(
            <WidgetStatesConstraint, TextStyle?>{
              WidgetState.disabled: disabledTextStyle,
              WidgetState.selected: selectedTextStyle,
              WidgetState.any: unselectedTextStyle,
            },
          ),
          onDestinationSelected: null,
          destinations: const <Widget>[
            NavigationDestination(
              icon: Icon(Icons.home),
              label: 'Home',
            ),
            NavigationDestination(
              icon: Icon(Icons.favorite_rounded),
              label: 'Favorites',
            ),
            NavigationDestination(
              enabled: false,
              icon: Icon(Icons.settings),
              label: 'Settings',
            ),
          ],
        ),
      ),
    );
  }
}
```

</details>

### Preview

<img width="719" alt="Screenshot 2024-11-12 at 17 18 00" src="https://github.com/user-attachments/assets/2b489bfa-78a3-4766-bc22-9273d76eadcd">
This commit is contained in:
Taha Tesser
2024-11-12 18:33:51 +02:00
committed by GitHub
parent 423aa2ac74
commit 148be5f894
2 changed files with 113 additions and 10 deletions

View File

@@ -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<Color?>? 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<TextStyle?>? 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<TextStyle?>? labelTextStyle;
/// The padding around the [label] widget.
///
/// Defaults to a padding of 4 pixels on the top.

View File

@@ -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 <Widget>[
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<TextStyle?>? labelTextStyle }) {
return MaterialApp(
theme: theme,
home: Scaffold(
bottomNavigationBar: NavigationBar(
labelTextStyle: labelTextStyle,
destinations: const <Widget>[
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<TextStyle?>.fromMap(
<WidgetStatesConstraint, TextStyle?>{
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<RichText>(
find.descendant(
of: find.text(text),
matching: find.byType(RichText),
),
).text.style!;
}