BottomNavigationBar constructor Null safety

BottomNavigationBar(
  1. {Key? key,
  2. required List<BottomNavigationBarItem> items,
  3. ValueChanged<int>? onTap,
  4. int currentIndex = 0,
  5. double? elevation,
  6. BottomNavigationBarType? type,
  7. Color? fixedColor,
  8. Color? backgroundColor,
  9. double iconSize = 24.0,
  10. Color? selectedItemColor,
  11. Color? unselectedItemColor,
  12. IconThemeData? selectedIconTheme,
  13. IconThemeData? unselectedIconTheme,
  14. double selectedFontSize = 14.0,
  15. double unselectedFontSize = 12.0,
  16. TextStyle? selectedLabelStyle,
  17. TextStyle? unselectedLabelStyle,
  18. bool? showSelectedLabels,
  19. bool? showUnselectedLabels,
  20. MouseCursor? mouseCursor,
  21. bool? enableFeedback,
  22. BottomNavigationBarLandscapeLayout? landscapeLayout}
)

Creates a bottom navigation bar which is typically used as a Scaffold's Scaffold.bottomNavigationBar argument.

The length of items must be at least two and each item's icon and label must not be null.

If type is null then BottomNavigationBarType.fixed is used when there are two or three items, BottomNavigationBarType.shifting otherwise.

The iconSize, selectedFontSize, unselectedFontSize, and elevation arguments must be non-null and non-negative.

If selectedLabelStyle.color and unselectedLabelStyle.color values are non-null, they will be used instead of selectedItemColor and unselectedItemColor.

If custom IconThemeDatas are used, you must provide both selectedIconTheme and unselectedIconTheme, and both IconThemeData.color and IconThemeData.size must be set.

If both selectedLabelStyle.fontSize and selectedFontSize are set, selectedLabelStyle.fontSize will be used.

Only one of selectedItemColor and fixedColor can be specified. The former is preferred, fixedColor only exists for the sake of backwards compatibility.

If showSelectedLabels is null, BottomNavigationBarThemeData.showSelectedLabels is used. If BottomNavigationBarThemeData.showSelectedLabels is null, then showSelectedLabels defaults to true.

If showUnselectedLabels is null, BottomNavigationBarThemeData.showUnselectedLabels is used. If BottomNavigationBarThemeData.showSelectedLabels is null, then showUnselectedLabels defaults to true when type is BottomNavigationBarType.fixed and false when type is BottomNavigationBarType.shifting.

Implementation

BottomNavigationBar({
  super.key,
  required this.items,
  this.onTap,
  this.currentIndex = 0,
  this.elevation,
  this.type,
  Color? fixedColor,
  this.backgroundColor,
  this.iconSize = 24.0,
  Color? selectedItemColor,
  this.unselectedItemColor,
  this.selectedIconTheme,
  this.unselectedIconTheme,
  this.selectedFontSize = 14.0,
  this.unselectedFontSize = 12.0,
  this.selectedLabelStyle,
  this.unselectedLabelStyle,
  this.showSelectedLabels,
  this.showUnselectedLabels,
  this.mouseCursor,
  this.enableFeedback,
  this.landscapeLayout,
}) : assert(items != null),
     assert(items.length >= 2),
     assert(
      items.every((BottomNavigationBarItem item) => item.label != null),
      'Every item must have a non-null label',
     ),
     assert(0 <= currentIndex && currentIndex < items.length),
     assert(elevation == null || elevation >= 0.0),
     assert(iconSize != null && iconSize >= 0.0),
     assert(
       selectedItemColor == null || fixedColor == null,
       'Either selectedItemColor or fixedColor can be specified, but not both',
     ),
     assert(selectedFontSize != null && selectedFontSize >= 0.0),
     assert(unselectedFontSize != null && unselectedFontSize >= 0.0),
     selectedItemColor = selectedItemColor ?? fixedColor;