Flutter Tabbarview下划线颜色

时间:2018-09-08 15:00:08

标签: flutter

如何向未选中的标签添加下划线,如下所示:

https://ibb.co/mfkzKp

在这里您可以看到未选中的标签为灰色,已选中的为蓝色。

6 个答案:

答案 0 :(得分:3)

我没有在文档中找到有关如何自定义禁用指标的参考。但是,您可以构建自己的小部件,该小部件将采用其他 decoration 装饰参数:

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,
      ],
    );
  }
}

然后,您可以根据需要装饰TabBar:

appBar: AppBar(
  bottom: DecoratedTabBar(
    tabBar: TabBar(
      tabs: [
        // ...
      ],
    ),
    decoration: BoxDecoration(
      border: Border(
        bottom: BorderSide(
          color: Colors.blue,
          width: 2.0,
        ),
      ),
    ),
  ),
),

这将导致预期的行为:

TabBar custom decoration

答案 1 :(得分:2)

我知道我要晚回答,但这最终将帮助很多人。您要做的就是遵循@tomwyr

装修中提到的相同内容

您不必为此创建自己的小部件,只需执行此操作即可。

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>[
          ...
        ],
      )
    )
  ]
);
}
}

结果

Required resultant

答案 2 :(得分:2)

最好的方法是这样的:

  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()],
    ),
  ),

答案 3 :(得分:2)

您可以将 DefaultTabController 与 Theme Widget 结合起来,并在 ThemeData 中的 indicatorColor 中传递颜色。

Theme(
  data: ThemeData(
    indicatorColor: Colors.red,
  ),
  child: DefaultTabController(
    length: 2,
    child: Scaffold(
      appBar: AppBar(
        title: Text('Example =)'),
  

答案 4 :(得分:1)

您可以尝试使用this package!非常简单,只需将指标添加到标签栏的指标属性

      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,
      ),

答案 5 :(得分:0)

此帖子以前已删除,因为我将其放在两个地方。我删除了另一篇文章,因为这是最好的地方。在这里可以找到类似的问题:How to create unselected indicator for tab bar in Flutter

我认为最好的答案是将选项卡栏包装在“材质”小部件中,并为其指定一个高程(我选择的高程为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),
                      ),
                    ),
                  ),