Skip to content

Commit 8420246

Browse files
authored
Fix BottomNavigationBar label style text colors (#102638)
1 parent b5e7fb0 commit 8420246

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

packages/flutter/lib/src/material/bottom_navigation_bar.dart

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class BottomNavigationBar extends StatefulWidget {
138138
///
139139
/// If [selectedLabelStyle].color and [unselectedLabelStyle].color values
140140
/// are non-null, they will be used instead of [selectedItemColor] and
141-
/// [unselectedItemColor].
141+
/// [unselectedItemColor] to style the label color.
142142
///
143143
/// If custom [IconThemeData]s are used, you must provide both
144144
/// [selectedIconTheme] and [unselectedIconTheme], and both
@@ -384,7 +384,7 @@ class _BottomNavigationTile extends StatelessWidget {
384384
this.animation,
385385
this.iconSize, {
386386
this.onTap,
387-
this.colorTween,
387+
this.itemColorTween,
388388
this.flex,
389389
this.selected = false,
390390
required this.selectedLabelStyle,
@@ -410,7 +410,7 @@ class _BottomNavigationTile extends StatelessWidget {
410410
final Animation<double> animation;
411411
final double iconSize;
412412
final VoidCallback? onTap;
413-
final ColorTween? colorTween;
413+
final ColorTween? itemColorTween;
414414
final double? flex;
415415
final bool selected;
416416
final IconThemeData? selectedIconTheme;
@@ -513,7 +513,7 @@ class _BottomNavigationTile extends StatelessWidget {
513513
child: _Tile(
514514
layout: layout,
515515
icon: _TileIcon(
516-
colorTween: colorTween!,
516+
itemColorTween: itemColorTween!,
517517
animation: animation,
518518
iconSize: iconSize,
519519
selected: selected,
@@ -522,7 +522,7 @@ class _BottomNavigationTile extends StatelessWidget {
522522
unselectedIconTheme: unselectedIconTheme,
523523
),
524524
label: _Label(
525-
colorTween: colorTween!,
525+
itemColorTween: itemColorTween!,
526526
animation: animation,
527527
item: item,
528528
selectedLabelStyle: selectedLabelStyle,
@@ -602,7 +602,7 @@ class _Tile extends StatelessWidget {
602602

603603
class _TileIcon extends StatelessWidget {
604604
const _TileIcon({
605-
required this.colorTween,
605+
required this.itemColorTween,
606606
required this.animation,
607607
required this.iconSize,
608608
required this.selected,
@@ -612,7 +612,7 @@ class _TileIcon extends StatelessWidget {
612612
}) : assert(selected != null),
613613
assert(item != null);
614614

615-
final ColorTween colorTween;
615+
final ColorTween itemColorTween;
616616
final Animation<double> animation;
617617
final double iconSize;
618618
final bool selected;
@@ -622,7 +622,7 @@ class _TileIcon extends StatelessWidget {
622622

623623
@override
624624
Widget build(BuildContext context) {
625-
final Color? iconColor = colorTween.evaluate(animation);
625+
final Color? iconColor = itemColorTween.evaluate(animation);
626626
final IconThemeData defaultIconTheme = IconThemeData(
627627
color: iconColor,
628628
size: iconSize,
@@ -646,22 +646,22 @@ class _TileIcon extends StatelessWidget {
646646

647647
class _Label extends StatelessWidget {
648648
const _Label({
649-
required this.colorTween,
649+
required this.itemColorTween,
650650
required this.animation,
651651
required this.item,
652652
required this.selectedLabelStyle,
653653
required this.unselectedLabelStyle,
654654
required this.showSelectedLabels,
655655
required this.showUnselectedLabels,
656-
}) : assert(colorTween != null),
656+
}) : assert(itemColorTween != null),
657657
assert(animation != null),
658658
assert(item != null),
659659
assert(selectedLabelStyle != null),
660660
assert(unselectedLabelStyle != null),
661661
assert(showSelectedLabels != null),
662662
assert(showUnselectedLabels != null);
663663

664-
final ColorTween colorTween;
664+
final ColorTween itemColorTween;
665665
final Animation<double> animation;
666666
final BottomNavigationBarItem item;
667667
final TextStyle selectedLabelStyle;
@@ -679,10 +679,16 @@ class _Label extends StatelessWidget {
679679
selectedLabelStyle,
680680
animation.value,
681681
)!;
682+
final ColorTween labelColor = ColorTween(
683+
begin: unselectedLabelStyle.color
684+
?? itemColorTween.begin,
685+
end: selectedLabelStyle.color
686+
?? itemColorTween.end,
687+
);
682688
Widget text = DefaultTextStyle.merge(
683689
style: customStyle.copyWith(
684690
fontSize: selectedFontSize,
685-
color: colorTween.evaluate(animation),
691+
color: labelColor.evaluate(animation),
686692
),
687693
// The font size should grow here when active, but because of the way
688694
// font rendering works, it doesn't grow smoothly if we just animate
@@ -923,10 +929,10 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
923929
break;
924930
}
925931

926-
final ColorTween colorTween;
932+
final ColorTween itemColorTween;
927933
switch (_effectiveType) {
928934
case BottomNavigationBarType.fixed:
929-
colorTween = ColorTween(
935+
itemColorTween = ColorTween(
930936
begin: widget.unselectedItemColor
931937
?? bottomTheme.unselectedItemColor
932938
?? themeData.unselectedWidgetColor,
@@ -937,7 +943,7 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
937943
);
938944
break;
939945
case BottomNavigationBarType.shifting:
940-
colorTween = ColorTween(
946+
itemColorTween = ColorTween(
941947
begin: widget.unselectedItemColor
942948
?? bottomTheme.unselectedItemColor
943949
?? themeData.colorScheme.surface,
@@ -971,7 +977,7 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
971977
onTap: () {
972978
widget.onTap?.call(i);
973979
},
974-
colorTween: colorTween,
980+
itemColorTween: itemColorTween,
975981
flex: _evaluateFlex(_animations[i]),
976982
selected: i == widget.currentIndex,
977983
showSelectedLabels: widget.showSelectedLabels ?? bottomTheme.showSelectedLabels ?? true,

packages/flutter/test/material/bottom_navigation_bar_test.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ void main() {
140140
});
141141

142142
testWidgets('Custom selected and unselected font styles', (WidgetTester tester) async {
143-
const TextStyle selectedTextStyle = TextStyle(fontWeight: FontWeight.w200, fontSize: 18.0);
144-
const TextStyle unselectedTextStyle = TextStyle(fontWeight: FontWeight.w600, fontSize: 12.0);
143+
const Color selectedTextColor = Color(0xff00ff00);
144+
const Color unselectedTextColor = Color(0xff0000ff);
145+
const TextStyle selectedTextStyle = TextStyle(color: selectedTextColor, fontWeight: FontWeight.w200, fontSize: 18.0);
146+
const TextStyle unselectedTextStyle = TextStyle(color: unselectedTextColor, fontWeight: FontWeight.w600, fontSize: 12.0);
145147

146148
await tester.pumpWidget(
147149
MaterialApp(
@@ -167,8 +169,10 @@ void main() {
167169

168170
final TextStyle selectedFontStyle = tester.renderObject<RenderParagraph>(find.text('AC')).text.style!;
169171
final TextStyle unselectedFontStyle = tester.renderObject<RenderParagraph>(find.text('Alarm')).text.style!;
172+
expect(selectedFontStyle.color, equals(selectedTextColor));
170173
expect(selectedFontStyle.fontSize, equals(selectedTextStyle.fontSize));
171174
expect(selectedFontStyle.fontWeight, equals(selectedTextStyle.fontWeight));
175+
expect(unselectedFontStyle.color, equals(unselectedTextColor));
172176
expect(
173177
tester.firstWidget<Transform>(find.ancestor(of: find.text('Alarm'), matching: find.byType(Transform))).transform,
174178
equals(Matrix4.diagonal3(Vector3.all(unselectedTextStyle.fontSize! / selectedTextStyle.fontSize!))),

0 commit comments

Comments
 (0)