颤振动态更改库比蒂诺标签栏背景颜色



我想更改选项卡的背景颜色。我有 5 个选项卡,我只希望第一个选项卡的背景是透明的。还有活动颜色和非活动颜色。

选项卡条形码如下所示:

return new WillPopScope(
onWillPop: () => new Future<bool>.value(true),
child: new CupertinoTabScaffold(
tabBar: new CupertinoTabBar(
backgroundColor: Colors.white, //change here
activeColor: Colors.black, // here 
inactiveColor: Colors.grey, // here too
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.access_time),
title: Text('Timeline'),
),
BottomNavigationBarItem(
icon: Icon(Icons.chat_bubble_outline),
title: Text('Talk'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Search'),
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
title: Text('Profile'),
),
],
),
tabBuilder: (BuildContext context, int index) {
return new DefaultTextStyle(
style: const TextStyle(
fontFamily: '.SF UI Text',
fontSize: 17.0,
color: CupertinoColors.black,
),
child: new CupertinoTabView(
builder: (BuildContext context) {
switch (index) {
case 0:
return HomeScreen();
break;
case 1:
return TimelineScreen();
break;
case 2:
return TalkScreen();
break;
case 3:
return SearchScreen();
break;
case 4:
return ProfileScreen();
break;
default:
break;
}
},
),
);
},
),
);

我尝试设置变量_currentIndex并根据哪个索引用户进行更改,但我无法调用setState因为错误显示:

颤振:以下断言被抛出构建_TabSwitchingView(脏,状态:
颤振:_TabSwitchingViewState#86c7b(:
颤振:setState(( 或 markNeedsBuild(( 在构建期间调用。
颤振:此主屏幕小部件无法标记为需要构建,因为框架已经在
颤振:构建小部件的过程中。一个小部件可以在构建阶段颤振期间标记为需要构建
:仅当其祖先之一当前正在构建时。允许此例外是因为框架
flutter: 在子小部件之前构建父小部件,这意味着将始终构建脏的后代。
颤振:否则,框架可能不会在此构建阶段访问此小部件。

有人有实现这一目标的想法吗?

我发现了。我不确定这是否合适。 希望这有帮助。

return new WillPopScope(
int currentIndex = 0;
onWillPop: () => new Future<bool>.value(true),
child: new CupertinoTabScaffold(
tabBar: new CupertinoTabBar(
onTap: (index) {
setState(() {
currentIndex = index;
});
},
backgroundColor: currentIndex == 0 ? Colors.transparent : Colors.white,
activeColor: currentIndex == 0 ? Colors.white : Colors.black,
inactiveColor: currentIndex == 0 ? Colors.white.withOpacity(0.5) : Colors.grey,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.access_time),
title: Text('Timeline'),
),
BottomNavigationBarItem(
icon: Icon(Icons.chat_bubble_outline),
title: Text('Talk'),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
title: Text('Search'),
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
title: Text('Profile'),
),
],
),
tabBuilder: (BuildContext context, int index) {
return new DefaultTextStyle(
style: const TextStyle(
fontFamily: '.SF UI Text',
fontSize: 17.0,
color: CupertinoColors.black,
),
child: new CupertinoTabView(
builder: (BuildContext context) {
switch (index) {
case 0:
return HomeScreen();
break;
case 1:
return TimelineScreen();
break;
case 2:
return TalkScreen();
break;
case 3:
return SearchScreen();
break;
case 4:
return ProfileScreen();
break;
default:
break;
}
},
),
);
},
),
);

最新更新