Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: BottomNavigationBarItem colors #96664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 118 additions & 22 deletions packages/flutter/lib/src/material/bottom_navigation_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ class BottomNavigationBar extends StatefulWidget {
/// [selectedIconTheme] and [unselectedIconTheme], and both
/// [IconThemeData.color] and [IconThemeData.size] must be set.
///
/// [IconThemeData]s take precedence over [iconSize], [selectedItemColor]
/// and [unselectedItemColor] for icons.
///
/// If both [selectedLabelStyle].fontSize and [selectedFontSize] are set,
/// [selectedLabelStyle].fontSize will be used.
///
Expand Down Expand Up @@ -193,6 +196,7 @@ class BottomNavigationBar extends StatefulWidget {
this.mouseCursor,
this.enableFeedback,
this.landscapeLayout,
this.useLegacyColorScheme = true,
}) : assert(items != null),
assert(items.length >= 2),
assert(
Expand Down Expand Up @@ -381,6 +385,10 @@ class BottomNavigationBar extends StatefulWidget {
/// orientation.
final BottomNavigationBarLandscapeLayout? landscapeLayout;

/// This flag is controlling how [BottomNavigationBar] is going to use
/// the colors provided by the properties and the theme.
final bool useLegacyColorScheme;

@override
State<BottomNavigationBar> createState() => _BottomNavigationBarState();
}
Expand All @@ -394,7 +402,8 @@ class _BottomNavigationTile extends StatelessWidget {
this.animation,
this.iconSize, {
this.onTap,
this.colorTween,
this.labelColorTween,
this.iconColorTween,
this.flex,
this.selected = false,
required this.selectedLabelStyle,
Expand All @@ -420,7 +429,8 @@ class _BottomNavigationTile extends StatelessWidget {
final Animation<double> animation;
final double iconSize;
final VoidCallback? onTap;
final ColorTween? colorTween;
final ColorTween? labelColorTween;
final ColorTween? iconColorTween;
final double? flex;
final bool selected;
final IconThemeData? selectedIconTheme;
Expand Down Expand Up @@ -523,7 +533,7 @@ class _BottomNavigationTile extends StatelessWidget {
child: _Tile(
layout: layout,
icon: _TileIcon(
colorTween: colorTween!,
colorTween: iconColorTween!,
animation: animation,
iconSize: iconSize,
selected: selected,
Expand All @@ -532,7 +542,7 @@ class _BottomNavigationTile extends StatelessWidget {
unselectedIconTheme: unselectedIconTheme,
),
label: _Label(
colorTween: colorTween!,
colorTween: labelColorTween!,
animation: animation,
item: item,
selectedLabelStyle: selectedLabelStyle,
Expand Down Expand Up @@ -910,24 +920,20 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
return textStyle.fontSize == null ? textStyle.copyWith(fontSize: fontSize) : textStyle;
}

static IconThemeData _effectiveIconTheme(IconThemeData? iconTheme, Color? itemColor) {
final IconThemeData effectiveIconTheme = iconTheme ?? const IconThemeData();
// Prefer the iconTheme over itemColor and textStyle color if present.
// If iconTheme is not set, prefer textStyle color over itemColor
return iconTheme == null ? effectiveIconTheme.copyWith(color: itemColor) : effectiveIconTheme;
}

List<Widget> _createTiles(BottomNavigationBarLandscapeLayout layout) {
final MaterialLocalizations localizations = MaterialLocalizations.of(context);
assert(localizations != null);

final ThemeData themeData = Theme.of(context);
final BottomNavigationBarThemeData bottomTheme = BottomNavigationBarTheme.of(context);

final TextStyle effectiveSelectedLabelStyle =
_effectiveTextStyle(
widget.selectedLabelStyle ?? bottomTheme.selectedLabelStyle,
widget.selectedFontSize,
);
final TextStyle effectiveUnselectedLabelStyle =
_effectiveTextStyle(
widget.unselectedLabelStyle ?? bottomTheme.unselectedLabelStyle,
widget.unselectedFontSize,
);

final Color themeColor;
switch (themeData.brightness) {
case Brightness.light:
Expand All @@ -938,28 +944,117 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
break;
}

final TextStyle effectiveSelectedLabelStyle =
_effectiveTextStyle(
widget.selectedLabelStyle
?? bottomTheme.selectedLabelStyle,
widget.selectedFontSize,
);
final TextStyle effectiveUnselectedLabelStyle =
_effectiveTextStyle(
widget.unselectedLabelStyle
?? bottomTheme.unselectedLabelStyle,
widget.unselectedFontSize,
);

final IconThemeData effectiveSelectedIconTheme =
_effectiveIconTheme(
widget.selectedIconTheme
?? bottomTheme.selectedIconTheme,
widget.selectedItemColor
?? bottomTheme.selectedItemColor
?? themeColor
);

final IconThemeData effectiveUnselectedIconTheme =
_effectiveIconTheme(
widget.unselectedIconTheme
?? bottomTheme.unselectedIconTheme,
widget.unselectedItemColor
?? bottomTheme.unselectedItemColor
?? themeData.unselectedWidgetColor
);

final ColorTween colorTween;
switch (_effectiveType) {
case BottomNavigationBarType.fixed:
colorTween = ColorTween(
colorTween = ColorTween(
begin: widget.unselectedItemColor
?? bottomTheme.unselectedItemColor
?? themeData.unselectedWidgetColor,
end: widget.selectedItemColor
?? bottomTheme.selectedItemColor
?? widget.fixedColor
?? themeColor,
);
break;
);
break;
case BottomNavigationBarType.shifting:
colorTween = ColorTween(
colorTween = ColorTween(
begin: widget.unselectedItemColor
?? bottomTheme.unselectedItemColor
?? themeData.colorScheme.surface,
end: widget.selectedItemColor
?? bottomTheme.selectedItemColor
?? themeData.colorScheme.surface,
);
break;
}

final ColorTween labelColorTween;
switch (_effectiveType) {
case BottomNavigationBarType.fixed:
labelColorTween = ColorTween(
begin: effectiveUnselectedLabelStyle.color
?? widget.unselectedItemColor
?? bottomTheme.unselectedItemColor
?? themeData.unselectedWidgetColor,
end: effectiveSelectedLabelStyle.color
?? widget.selectedItemColor
?? bottomTheme.selectedItemColor
?? widget.fixedColor
?? themeColor,
);
break;
case BottomNavigationBarType.shifting:
labelColorTween = ColorTween(
begin: effectiveUnselectedLabelStyle.color
?? widget.unselectedItemColor
?? bottomTheme.unselectedItemColor
?? themeData.colorScheme.surface,
end: effectiveSelectedLabelStyle.color
?? widget.selectedItemColor
?? bottomTheme.selectedItemColor
?? themeColor,
);
break;
}

final ColorTween iconColorTween;
switch (_effectiveType) {
case BottomNavigationBarType.fixed:
iconColorTween = ColorTween(
begin: effectiveSelectedIconTheme.color
?? widget.unselectedItemColor
?? bottomTheme.unselectedItemColor
?? themeData.unselectedWidgetColor,
end: effectiveUnselectedIconTheme.color
?? widget.selectedItemColor
?? bottomTheme.selectedItemColor
?? widget.fixedColor
?? themeColor,
);
break;
case BottomNavigationBarType.shifting:
iconColorTween = ColorTween(
begin: effectiveUnselectedIconTheme.color
?? widget.unselectedItemColor
?? bottomTheme.unselectedItemColor
?? themeData.colorScheme.surface,
end: effectiveSelectedIconTheme.color
?? widget.selectedItemColor
?? bottomTheme.selectedItemColor
?? themeColor,
);
break;
}

Expand All @@ -978,15 +1073,16 @@ class _BottomNavigationBarState extends State<BottomNavigationBar> with TickerPr
widget.items[i],
_animations[i],
widget.iconSize,
selectedIconTheme: widget.selectedIconTheme ?? bottomTheme.selectedIconTheme,
unselectedIconTheme: widget.unselectedIconTheme ?? bottomTheme.unselectedIconTheme,
selectedIconTheme: widget.useLegacyColorScheme ? widget.selectedIconTheme ?? bottomTheme.selectedIconTheme : effectiveSelectedIconTheme,
unselectedIconTheme: widget.useLegacyColorScheme ? widget.unselectedIconTheme ?? bottomTheme.unselectedIconTheme : effectiveUnselectedIconTheme,
selectedLabelStyle: effectiveSelectedLabelStyle,
unselectedLabelStyle: effectiveUnselectedLabelStyle,
enableFeedback: widget.enableFeedback ?? bottomTheme.enableFeedback ?? true,
onTap: () {
widget.onTap?.call(i);
},
colorTween: colorTween,
labelColorTween: widget.useLegacyColorScheme ? colorTween : labelColorTween,
iconColorTween: widget.useLegacyColorScheme ? colorTween : iconColorTween,
flex: _evaluateFlex(_animations[i]),
selected: i == widget.currentIndex,
showSelectedLabels: widget.showSelectedLabels ?? bottomTheme.showSelectedLabels ?? true,
Expand Down
Loading