如何在标签栏指示器中给出渐变线

Har*_*han 5 android dart flutter

我有一个标签栏功能,我在其中显示一个甜甜圈图和车辆列表,但我需要显示用户选择的标签我在 TabBar 中有指标颜色,但我需要填充渐变线,如图所示,请帮助我出去。

PS:如果可能的话,请让我知道如何给主题颜色意味着主色中的主色作为渐变???

return Scaffold(
    body: new DefaultTabController(
        length: 2,
        child: new Column(
          children: <Widget>[
            new Container(
              width: 1200.0,
              child: new Container(
                color: Colors.white,
                child: new TabBar(
                  labelColor: Colors.black,
                  tabs: [
                    Tab(
                      child: new Text("Visual",
                      style: new TextStyle(fontSize: 20.0)
                      ),
                    ),

                    Tab(
                      child: new Text("Tabular",
                      style: new TextStyle(fontSize: 20.0)), 
                    ),
                  ],
                ),
              ),
            ),
            new Expanded(
              child: new TabBarView(
                children: [
                  Tab(
                    child: new RefreshIndicator(
                      child: new Text('DONUT CHART'),
                      onRefresh: refreshList,
                      key: refreshKey1,
                    ),
                  ),
                  Tab(
                    child: new RefreshIndicator(
                      child: new Text('List of vehicles'),
                      onRefresh: refreshList,
                      key: refreshKey2,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在此处输入图片说明

小智 7

而不是在这里更改核心文件是一个对我有用的更好的答案。

TabBar(
          controller: tabController,
          indicatorPadding: EdgeInsets.all(0.0),
          indicatorWeight: 4.0,
          labelPadding: EdgeInsets.only(left: 0.0, right: 0.0),
          indicator: ShapeDecoration(
              shape: UnderlineInputBorder(
                  borderSide: BorderSide(
                      color: Colors.transparent,
                      width: 4.0,
                      style: BorderStyle.solid)),
              gradient: LinearGradient(colors: [pinkLight, pinkDark])),
          tabs: <Widget>[
            Center(
              child: Container(
                alignment: Alignment.center,
                color: whiteColor,
                child: Text(
                  "Rating",
                  style: themeData.accentTextTheme.title,
                ),
              ),
            ),
            Container(
              alignment: Alignment.center,
              color: whiteColor,
              child: Text(
                "Category",
                style: themeData.accentTextTheme.title,
              ),
            ),
            Container(
              alignment: Alignment.center,
              color: whiteColor,
              child: Text(
                "Friend",
                style: themeData.accentTextTheme.title,
              ),
            ),
          ],
        ),
Run Code Online (Sandbox Code Playgroud)

使指示器填充为零,标签填充为零,指示器重量为 4.0 或之后您需要的大小,而不是在选项卡中使用文本,在顶部使用容器并为其赋予颜色。


And*_*sky 5

我认为唯一的方法是创建自定义 TabBar。您可以从 tabs.dart 复制 TabBar 的代码,并在 _TabBarState 中进行更改Decoration get _indicator

就像是:

return ShapeDecoration(shape: UnderlineInputBorder(), gradient: LinearGradient(
    colors: [Colors.blue, Colors.green]));
Run Code Online (Sandbox Code Playgroud)

更新:

知道了。ShapeDecoration 不起作用。有了它我可以为整个选项卡设置渐变。_IndicatorPainter对于下划线 -同一文件中有类。并且有Rect indicatorRect这个类的方法。

return Rect.fromLTWH(tabLeft, 0.0, tabRight - tabLeft, tabBarSize.height);
Run Code Online (Sandbox Code Playgroud)

该字符串返回用于装饰的矩形。如果你改变它 - 你会得到下划线:

return Rect.fromLTWH(tabLeft, tabBarSize.height - 4.0, tabRight - tabLeft, 4.0);
Run Code Online (Sandbox Code Playgroud)

并且里面Decoration get _indicator不要忘记更改return UnderlineTabIndicator

return ShapeDecoration(shape: RoundedRectangleBorder(), gradient: LinearGradient(
    colors: [Colors.blue, Colors.green]));
Run Code Online (Sandbox Code Playgroud)

这是结果 在此输入图像描述