繁体   English   中英

Flutter Tabbarview下划线颜色

[英]Flutter Tabbarview underline color

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

https://ibb.co/mfkzKp

在这里你可以看到未选中的标签是灰色的,选中的是蓝色的。

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

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 自定义装饰

TabBar(
indicatorColor: Color(0xffF15C22),
unselectedLabelColor: Colors.black,
                labelColor: Color(0xffF15C22),
                tabs: [
                  Tab(text: "Tab 1"),
                  Tab(text: "Tab 2"),
                  Tab(text: "Tab 3"),
                ],
              ),

indicatorColor 是有助于更改选项卡视图中线条颜色的属性

我知道我回答晚了,但这最终会帮助很多人。 你所要做的就是遵循@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>[
          ...
        ],
      )
    )
  ]
);
}
}

结果

所需的结果

最好的方法是这样的:

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

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

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

你可以尝试使用这个包 它非常简单,只需将指标添加到标签栏的指标属性

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

这个帖子之前已经被删除了,因为我把它放在了两个地方。 我删除了另一篇文章,因为这是最好的地方。 可以在这里找到类似的问题: 如何在 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),
                      ),
                    ),
                  ),

你在标签栏中添加这个

 indicator: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    color: Colors.white,
                    width: 2.0,
                  ),
                ),
              ),

然后你想出了这样的东西

 TabBar(
              labelColor: Colors.white,
              indicator: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    color: Colors.white,
                    width: 2.0,
                  ),
                ),
              ),
              tabs: [
                Tab(child:
                FxText.titleMedium("Pofile", fontWeight: 600,color: Colors.white,)),
                Tab(
                    child: FxText.titleMedium(
                        "Events",
                        fontWeight: 600,color: Colors.white)),
              ],
            )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM