dart - Flutter - 覆盖 CupertinoTabBar 的“构建”方法
问题描述
我目前正在尝试通过让我自己的类扩展它并覆盖“build”方法来自定义 CupertinoTabBar 小部件。我当前的代码与此类似:
class CustomTabBar extends CupertinoTabBar {
CustomTabBar(
Key key,
@required List<BottomNavigationBarItem> items,
ValueChanged<int> onTap,
int currentIndex = 0,
Color backgroundColor = _kDefaultTabBarBackgroundColor,
Color activeColor = CupertinoColors.activeBlue,
Color inactiveColor = CupertinoColors.inactiveGray,
double iconSize = 30.0,
) : assert(items != null),
super(items: items, onTap: onTap, currentIndex: currentIndex,
backgroundColor: backgroundColor, activeColor: activeColor,
inactiveColor: inactiveColor, iconSize: iconSize);
@override
Widget build(BuildContext context) {
//build logic for the custom bar
}
}
但是,当我运行该应用程序时,将调用 CupertinoTabBar 构建方法而不是 CustomTabBar 构建方法。换句话说,我的课程并没有覆盖 CupertinoTabBar 的构建。
我在这里做错了吗?我这样做的原因是因为我使用 CupertinoTabScaffold 为每个选项卡保留单独的导航器,并且 CupertinoTabScaffold 的 tabBar 参数只接受 CupertinoTabBar。
解决方案
您需要覆盖copyWith方法。
class CustomTabBar extends CupertinoTabBar {
CustomTabBar(
Key key,
@required List<BottomNavigationBarItem> items,
ValueChanged<int> onTap,
int currentIndex = 0,
Color backgroundColor = _kDefaultTabBarBackgroundColor,
Color activeColor = CupertinoColors.activeBlue,
Color inactiveColor = CupertinoColors.inactiveGray,
double iconSize = 30.0,
) : assert(items != null),
super(items: items, onTap: onTap, currentIndex: currentIndex,
backgroundColor: backgroundColor, activeColor: activeColor,
inactiveColor: inactiveColor, iconSize: iconSize);
@override
Widget build(BuildContext context) {
//build logic for the custom bar
}
@override
CustomTabBar copyWith({
Key key,
List<BottomNavigationBarItem> items,
Color backgroundColor,
Color activeColor,
Color inactiveColor,
Size iconSize,
Border border,
int currentIndex,
ValueChanged<int> onTap,
}) {
return CustomTabBar(
key: key ?? this.key,
items: items ?? this.items,
backgroundColor: backgroundColor ?? this.backgroundColor,
activeColor: activeColor ?? this.activeColor,
inactiveColor: inactiveColor ?? this.inactiveColor,
iconSize: iconSize ?? this.iconSize,
border: border ?? this.border,
currentIndex: currentIndex ?? this.currentIndex,
onTap: onTap ?? this.onTap
);
}
}
推荐阅读
- javascript - 如何获取视频海报的图像尺寸?
- android - 如何每天晚上运行 Job Scheduler
- javascript - javascript/jquery .animate() 色相旋转
- c - 字符串的哈希函数
- shell - 在 shell 脚本的间隔中查找数据集之间的持续时间及其最大值
- python - AttributeError:模块“随机”没有属性“选择”Django
- r - 反应性闪亮模块共享数据
- powershell - 使用 PowerShell 将机器识别为服务器或 PC
- javascript - jQuery从div中的文本中删除所有URL
- javascript - (自动)在 react-admin 中突变后更新查询