【问题标题】:Flutter Tabbarview underline colorFlutter Tabbarview 下划线颜色
【发布时间】:2019-02-13 15:10:04
【问题描述】:

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

https://ibb.co/mfkzKp

在这里您可以看到未选中的选项卡为灰色,选中的选项卡为蓝色。

【问题讨论】:

标签: flutter


【解决方案1】:

我在文档中没有找到任何关于如何自定义禁用指标的参考。但是,您可以构建自己的小部件,该小部件将采用额外的 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,
        ),
      ),
    ),
  ),
),

这将导致所需的行为:

【讨论】:

    【解决方案2】:

    我知道我回答迟了,但这最终会帮助很多人。你所要做的就是遵循@tomwyr

    decoration中提到的同样的事情

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

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

    结果

    【讨论】:

    【解决方案3】:

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

    我认为最好的答案是将标签栏包裹在一个 Material 小部件中,并给它一个高度(我选择高度为 1)。之后,您可以自定义 Material Widget 的阴影颜色。

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

    【讨论】:

      【解决方案4】:
      TabBar(
      indicatorColor: Color(0xffF15C22),
      unselectedLabelColor: Colors.black,
                      labelColor: Color(0xffF15C22),
                      tabs: [
                        Tab(text: "Tab 1"),
                        Tab(text: "Tab 2"),
                        Tab(text: "Tab 3"),
                      ],
                    ),
      

      indicatorColor 是有助于改变标签视图中线条颜色的属性

      【讨论】:

        【解决方案5】:

        您可以尝试使用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,
              ),
        

        【讨论】:

          【解决方案6】:

          最好的方法是这样的:

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

          【讨论】:

            【解决方案7】:

            您可以只使用 Theme Widget 封装 DefaultTabController 并在 ThemeData 中的 indicatorColor 中传递颜色。

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

            【讨论】:

              猜你喜欢
              • 2012-09-15
              • 2016-09-03
              • 2010-09-16
              • 2022-11-30
              • 2016-06-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-08-22
              相关资源
              最近更新 更多