如何在Flutter中为标签栏创建未选择的指示器

时间:2018-08-26 17:35:37

标签: android tabs flutter tabbar indicator

我使用装饰器为标签栏创建了一个自定义指标。 我想为选项卡栏中未选中的选项卡创建一个未选择的指示器。

我做了一个带有自定义装饰的容器,但是当前选择的指示器在容器装饰后面绘制。

new TabBar( 
labelColor: Colors.black,
unselectedLabelColor: Colors.grey,
indicator: new CustomTabIndicator(),
tabs: [
new Container(decoration: new CustomTabInactive(),child: Tab(icon: Icon(Icons.person )))])

tab bar with unselected indicator

4 个答案:

答案 0 :(得分:0)

尝试在标签中使用容器而不是在标签中使用容器(用容器包装TabBar并提供底部边框

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: [
      ...
    ]
  )

看起来不会完全像您想要的那样,但是会给未选中的标签页加上下划线的指示。

答案 1 :(得分:0)

您可以同时使用Stack / Container和TabBar制作下划线

Stack(
            fit: StackFit.passthrough,
            alignment: Alignment.bottomCenter,
            children: <Widget>[
              Container(
                decoration: BoxDecoration(
                  border: Border(
                    bottom: BorderSide(color: colorSecondary, width: 2.0),
                  ),
                ),
              ),
              TabBar(
                isScrollable: true,
                onTap: (index) => tabsModel.value = listTabsModel[index],
                tabs: listTabsModel
                    .map(
                      (value) => Tab(
                        child: value.tabComponent,
                      ),
                    )
                    .toList(),
              ),
            ],
          );

TabBar with an underline for unselected

并非完全符合您的要求,但可以为未选中的标签加下划线。

答案 2 :(得分:0)

我认为您可以在插件中使用以下代码。

TabBar(
      controller: tabController,
      tabs: [
        Tab(text: "ADCD"),
        Tab(text: "EFGH"),
        Tab(text: "IJKL"),
        Tab(text: "MNOP"),
        Tab(text: "QRST"),
      ],
      labelColor: Colors.white,
      labelStyle: TextStyle(
          fontSize: 12.0, fontWeight: FontWeight.w700, fontFamily: 'helvetica'),
      unselectedLabelColor: Colors.black,
      unselectedLabelStyle: TextStyle(
          fontSize: 12.0, fontWeight: FontWeight.w400, fontFamily: 'helvetica'),
      isScrollable: true,
      indicator: new BubbleTabIndicator(
        indicatorHeight: p_35,
        indicatorColor: const Color(0xFF58c8e3),
        tabBarIndicatorSize: TabBarIndicatorSize.tab,
      ),
    );

我已将此插件用于“ BubbleTabIndicator” https://pub.dev/packages/bubble_tab_indicator

答案 3 :(得分:0)

我使用的解决方案与@andy所建议的类似,但有所不同

Stack(
  children: [
    Container(
      // The height of TabBar without icons is 46 (72 with), so 2 pixels for border
      height: 48,
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: Colors.red,
            width: 2,
          ),
        ),
      ),
    ),
    TabBar(
      isScrollable: true,
      indicator: UnderlineTabIndicator(
        borderSide: BorderSide(
          color: Colors.yellow,
          width: 2.0,
        ),
      ),
      indicatorSize: TabBarIndicatorSize.tab,
      tabs: <Widget>[
        Tab(
          text: "Tab 1",
        ),
        Tab(
          text: "Tab 2",
        ),
        Tab(
          text: "Tab 3",
        )
      ],
    ),
  ],
)