在Flutter的TabBar顶部添加指示器

Bla*_*nka 6 flutter

像这个:

在此处输入图片说明

下面的代码将在TabBar的底部添加指标:

  DefaultTabController(
    length: 2,
    child : new TabBar(
      labelColor: Colors.purple,
      indicatorColor: Colors.purple,
      indicatorSize: TabBarIndicatorSize.label,
      unselectedLabelColor: Colors.black,
    tabs: [
        new Tab(icon: Icon(Icons.chrome_reader_mode),),
        new Tab(icon: Icon(Icons.clear_all),),
    ]),
  );
Run Code Online (Sandbox Code Playgroud)

但我需要Indicator之上TabBar。我认为构建自定义选项卡栏不是一个好主意,因为我不想为这个简单的事情做很多工作。

Bla*_*nka 6

有一个简单的技巧,那就是使用indicator属性并添加UnderlineTabIndicator(),并且该类已将命名参数称为insets和作为我添加的值EdgeInsets.fromLTRB(50.0, 0.0, 50.0, 40.0),

左:50.0,//将指示器从左减小50.0

最高:0.0,

右:50.0,//将指示器从右缩小50.0

底部:40.0 //将指示器从底部向上推40.0

如下所示:

const textStyle = TextStyle(
    fontSize: 12.0,
    color: Colors.white,
    fontFamily: 'OpenSans',
    fontWeight: FontWeight.w600);

//.....
new TabBar(
  controller: controller,
  labelColor: Color(0xFF343434),
  labelStyle: textStyle.copyWith(
      fontSize: 20.0,
      color: Color(0xFFc9c9c9),
      fontWeight: FontWeight.w700),
  indicator: UnderlineTabIndicator(
    borderSide: BorderSide(color: Color(0xDD613896), width: 8.0),
    insets: EdgeInsets.fromLTRB(50.0, 0.0, 50.0, 40.0),
  ),
  unselectedLabelColor: Color(0xFFc9c9c9),
  unselectedLabelStyle: textStyle.copyWith(
      fontSize: 20.0,
      color: Color(0xFFc9c9c9),
      fontWeight: FontWeight.w700),
  tabs: [
    new Tab(
      text: 'LOGIN',
    ),
    new Tab(
      text: 'SIGNUP',
    ),
  ],
),
Run Code Online (Sandbox Code Playgroud)

您还可以创建自定义指标扩展 Decoration


Luc*_*lli 5

另一种选择是为小部件Decorationindicator:属性定义一个新的TabBar,这样您就不会绑定到 TabBar 高度(与使用一样insets: EdgeInsets.fromLTRB(50.0, 0.0, 50.0, 40.0),),但您不能创建一个小于选项卡本身的指示器。

这里的代码:

TabBar(
  indicator: BoxDecoration(
    border: Border(
      top: BorderSide(
        color: Theme.of(context).accentColor,
        width: 3.0
      ),
    ),
  ),
),
Run Code Online (Sandbox Code Playgroud)