gpt4 book ai didi

dart - Flutter - 覆盖 CupertinoTabBar 的 "build"方法

转载 作者:IT王子 更新时间:2023-10-29 06:54:39 25 4
gpt4 key购买 nike

我目前正在尝试通过让我自己的类扩展它并覆盖“构建”方法来自定义 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 构建方法。换句话说,我的类(class)没有覆盖 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
);
}
}

关于dart - Flutter - 覆盖 CupertinoTabBar 的 "build"方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52823841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com