Flutter Tab Bar-如何分配每个选定选项卡的不同颜色?
TabBar(
controller: _tabController,
unselectedLabelColor: Colors.grey,
indicator: BoxDecoration(
borderRadius: BorderRadius.circular(
16.0,
),
color: colors[_tabController.index],
),
tabs: const [
Tab(text: "INFO"),
Tab(text: "ENGAGEMENT"),
Tab(text: "TRACK"),
],
labelStyle: const TextStyle(
color: Colors.white,
fontSize: 13,
fontWeight: FontWeight.w600),
unselectedLabelStyle: const TextStyle(
color: Colors.grey,
fontSize: 13,
fontWeight: FontWeight.w600),
),
),
),
我希望每个选项卡在选择时都具有不同的颜色 因此,当索引通过将其存储在列表中时,我试图更改颜色 但是它不用setState就无法正常工作(我知道它不会改变状态,因为快速重新加载后颜色会更改)
=“ nofollow noreferrer”> < 。
输出
TabBar(
controller: _tabController,
unselectedLabelColor: Colors.grey,
indicator: BoxDecoration(
borderRadius: BorderRadius.circular(
16.0,
),
color: colors[_tabController.index],
),
tabs: const [
Tab(text: "INFO"),
Tab(text: "ENGAGEMENT"),
Tab(text: "TRACK"),
],
labelStyle: const TextStyle(
color: Colors.white,
fontSize: 13,
fontWeight: FontWeight.w600),
unselectedLabelStyle: const TextStyle(
color: Colors.grey,
fontSize: 13,
fontWeight: FontWeight.w600),
),
),
),
I wanted Each Tab to have a Different color when they're selected
So I tried to change the color when the index changes by storing them in a list
But it doesnt work without setState (I know its not changing the state because the color changes after quick reload)
This is the expected output
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在
TabController
上包含一个侦听器,并在此处调用setState
。You can include a listener on
TabController
and callsetState
there.下面的代码应该解决问题。方法的缺失部分是
setstate
使用当前选择的选项卡索引刷新屏幕。查看屏幕录制和=“ https://dartpad.dev/?id=d45cffbf73ae3E6713CF36CC2C82248D” rel =“ nofollow noreforler” noreflow noreferrer“ /PI.flutter.dev/flutter/material/tabbar-class.html“ rel =“ nofollow noreferrer”> tabbar 几乎已经完成了所有工作。只有外部圆形边框才能进行自定义,这只是将
tabar
包装到容器。容器
非常易于使用,并且高度可定制。这是代码本身作为 minimal-reproducible-example :
The code below should do the trick. The missing part of your approach is a
setState
to refresh the screen with the current selected tab index. Check out the screen recording and the live demo on DartPad:The TabBar do almost all the job already. Only the outside round border should be customized and it's just a matter of wrapping the
TabBar
into a Container.Containers
are pretty easy to work with and highly customizable.And here's the code itself as a minimal-reproducible-example: