flutter - 在 Flutter 中的 TabBar 顶部添加指示器

标签 flutter

喜欢这个:

enter image description here

下面的代码会在TabBar底部添加Indicator:

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

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

最佳答案

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

Left: 50.0, // make indicator small by 50.0 from left

Top: 0.0,

Right: 50.0, // make indicator small by 50.0 from right

Bottom: 40.0 // pushed indicator to top by 40.0 from bottom

如下:

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

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

关于flutter - 在 Flutter 中的 TabBar 顶部添加指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51125056/

相关文章:

dart - Flutter popUntil 抛出Bad state : Future already completed

Flutter RichText 未显示

flutter - RenderFlex 子级具有非零 flex,但传入的高度约束是无界的,已尝试灵活和扩展不起作用

flutter - Flutter 中的区分大小写

dart - 扩展 Google Flutter 中的 UnderlineInputBorder 类

android-studio - 我无法安装程序包缓存的网络镜像

dart - Flutter将扩展容器中的小部件对齐到左上角和右下角?

firebase - Firestore添加条件以仅读取最新数据

dart - 类型为父类(super class)的 Flutter StreamBuilder

json - 使用 json 文件中的动态字符串进行 flutter 国际化