Flutter 颤振选项卡视图下划线颜色
如何向未选中的选项卡添加下划线,如下所示:Flutter 颤振选项卡视图下划线颜色,flutter,Flutter,如何向未选中的选项卡添加下划线,如下所示: 在这里您可以看到,对于未选中的选项卡,它是灰色的,对于选中的选项卡,它是蓝色的。我在文档中没有找到有关如何自定义禁用指示器的任何参考。但是,您可以构建自己的小部件,该小部件将采用额外的装饰参数: class DecoratedTabBar extends StatelessWidget implements PreferredSizeWidget { DecoratedTabBar({@required this.tabBar, @require
在这里您可以看到,对于未选中的选项卡,它是灰色的,对于选中的选项卡,它是蓝色的。我在文档中没有找到有关如何自定义禁用指示器的任何参考。但是,您可以构建自己的小部件,该小部件将采用额外的装饰参数:
class DecoratedTabBar extends StatelessWidget implements PreferredSizeWidget {
DecoratedTabBar({@required this.tabBar, @required this.decoration});
final TabBar tabBar;
final BoxDecoration decoration;
@override
Size get preferredSize => tabBar.preferredSize;
@override
Widget build(BuildContext context) {
return Stack(
children: [
Positioned.fill(child: Container(decoration: decoration)),
tabBar,
],
);
}
}
然后,您可以随心所欲地装饰选项卡栏:
appBar: AppBar(
bottom: DecoratedTabBar(
tabBar: TabBar(
tabs: [
// ...
],
),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.blue,
width: 2.0,
),
),
),
),
),
这将导致期望的行为:
我知道我回答晚了,但这最终会帮助很多人。你要做的就是遵循《装饰》中提到的同样的事情 您不必为此制作自己的小部件,只要这样做就可以了
class CustomTabBarMenu extends StatefulWidget {
@override
_CustomTabBarMenuState createState() => _CustomTabBarMenuState();
}
class _CustomTabBarMenuState extends State<CustomTabBarMenu>
with SingleTickerProviderStateMixin{
TabController _controller;
@override
void initState() {
// TODO: implement initState
super.initState();
_controller = new TabController(length: YOUR_LENGTH, vsync: this);
}
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
//This is responsible for the background of the tabbar, does the magic
decoration: BoxDecoration(
//This is for background color
color: Colors.white.withOpacity(0.0),
//This is for bottom border that is needed
border: Border(bottom: BorderSide(color: Colors.grey, width: 0.8))),
child: TabBar(
controller: _controller,
tabs: [
...
]
)
),
Container(
height: MediaQuery.of(context).size.height/2.3,
child: new TabBarView(
controller: _controller,
children: <Widget>[
...
],
)
)
]
);
}
}
类CustomTabBarMenu扩展StatefulWidget{
@凌驾
_CustomTabBarMenuState createState()=>\u CustomTabBarMenuState();
}
类\u CustomTabBarMenuState扩展状态
使用SingleTickerProviderStateMixin{
TabController\u控制器;
@凌驾
void initState(){
//TODO:实现initState
super.initState();
_controller=new TabController(长度:YOUR_length,vsync:this);
}
@凌驾
小部件构建(构建上下文){
返回列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
//这是负责背景的tabbar,有魔法吗
装饰:盒子装饰(
//这是背景色
颜色:颜色。白色。不透明度(0.0),
//这是用于所需的底部边框
边框:边框(底部:边框边(颜色:Colors.grey,宽度:0.8)),
孩子:TabBar(
控制器:_控制器,
选项卡:[
...
]
)
),
容器(
高度:MediaQuery.of(context).size.height/2.3,
子项:新选项卡视图(
控制器:_控制器,
儿童:[
...
],
)
)
]
);
}
}
结果
这篇文章之前被删除了,因为我把它放在了两个地方。我删除了另一篇文章,因为这是最好的地方。类似的问题可以在这里找到: 我认为最好的答案是将选项卡栏包装在材质小部件中,并给它一个标高(我选择了标高1)。然后,您可以自定义材质小部件的阴影颜色
Material(
type: MaterialType.canvas,
shadowColor: Colors.orange, //Custom unselected underline color
elevation: 1.0, //Create underline for entire tab bar
child: Container(
color: Color(0xFFe3f2fd), //Gives tab bar a background color
child: TabBar(tabs:
[Tab(text: 'ACTIVITY'),
Tab(text: 'LEADERBOARD',),
Tab(text: 'SETTINGS',)],
labelColor: Theme.of(context).primaryColor,
indicatorColor: Theme.of(context).primaryColor,
labelStyle: TextStyle(
fontWeight: FontWeight.bold,
fontFamily: 'Montserrat'),
indicatorPadding:
EdgeInsets.symmetric(horizontal: 20.0),
),
),
),
indicatorColor是有助于更改选项卡视图中线条颜色的属性您可以尝试使用!这很简单,只需将指示器添加到选项卡栏的指示器属性中即可
bottom: TabBar(
isScrollable: true,
indicatorSize: TabBarIndicatorSize.label,
labelColor: Theme.of(context).accentColor,
unselectedLabelColor: Color(0xff5f6368),
**indicator: MD2Indicator(
indicatorHeight: 3,
indicatorColor: Theme.of(context).accentColor,
indicatorSize: MD2IndicatorSize.full),**
tabs: Constants.tabItems,
),
最好的方法是:
Scaffold(
appBar: AppBar(
titleSpacing : 0 ,
automaticallyImplyLeading: false,
elevation: 0,
title: Container(
width: double.infinity,
decoration: BoxDecoration(
color: Colors.white,
border: Border(
bottom: BorderSide(color: Colors.grey, width: 0.8))),
child: TabBar(
unselectedLabelColor: Colors.grey,
unselectedLabelStyle: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 16,
color: Color.fromRGBO(142, 142, 142, 1)),
labelColor: Colors.blue,
labelPadding: EdgeInsets.fromLTRB(0, toppadding, 0, 8),
labelStyle: TextStyle(
fontFamily: "Roboto",
fontSize: 16,
fontWeight: FontWeight.w700,
),
controller: tabController,
indicatorColor: Colors.blue,
indicator: UnderlineTabIndicator(
borderSide:
BorderSide(color: Colors.grey, width: 2.0),
),
tabs: [
Text(
'Title1',
),
Text(
'Title2',
),
])),
),
body: TabBarView(
controller: tabController,
children: <Widget>[Container(), Container()],
),
),
脚手架(
appBar:appBar(
标题间距:0,
自动嵌入:false,
海拔:0,
标题:集装箱(
宽度:double.infinity,
装饰:盒子装饰(
颜色:颜色,白色,
边界:边界(
底部:边框(颜色:Colors.grey,宽度:0.8)),
孩子:TabBar(
未选择的标签颜色:Colors.grey,
未选择的标签样式:TextStyle(
fontWeight:fontWeight.w700,
尺寸:16,
颜色:color.fromRGBO(142、142、142、1)),
labelColor:Colors.blue,
标签填充:从LTRB(0,顶部添加,0,8)开始的边缘设置,
标签样式:文本样式(
fontFamily:“机器人”,
尺寸:16,
fontWeight:fontWeight.w700,
),
控制器:选项卡控制器,
指示颜色:颜色。蓝色,
指示器:下划线指示器(
边界:
边框(颜色:颜色:灰色,宽度:2.0),
),
选项卡:[
正文(
“标题1”,
),
正文(
“标题2”,
),
])),
),
正文:选项卡视图(
控制器:选项卡控制器,
子项:[Container(),Container()],
),
),
您只需使用一个主题小部件包围DefaultTabController,并将颜色传递到indicatorColor中的ThemeData中即可
Theme(
data: ThemeData(
indicatorColor: Colors.red,
),
child: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text('Example =)'),
您的图像链接似乎已断开。直接指向图像。你好,你知道怎么解决这个问题吗?
Theme(
data: ThemeData(
indicatorColor: Colors.red,
),
child: DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
title: Text('Example =)'),