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

Ale*_*ett 7 tabs android indicator tabbar flutter

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

我做了一个具有自定义装饰的容器,但是当前选择的指示器落后于容器装饰。

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

带未选择指示器的标签栏

dlo*_*ani 13

不要在选项卡中使用容器,而是尝试这个(用 DecoratedBox 包裹 TabBar 并提供底部边框

DecoratedBox(
  //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: [
      ...
    ],
  ),
)
Run Code Online (Sandbox Code Playgroud)

看起来不会完全如您所愿,但会给未选择的选项卡提供下划线指示。


小智 6

可以同时使用 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(),
              ),
            ],
          );
Run Code Online (Sandbox Code Playgroud)

带有下划线的 TabBar 表示未选中

不完全是您要查找的内容,但它可以为未选择的选项卡提供下划线。