5

我有一个标签栏功能,在其中我显示了一个圆环图和车辆列表,但我需要显示用户选择的哪个标签,我在 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,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );

在此处输入图像描述

在此处输入图像描述

4

2 回答 2

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

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

于 2019-05-09T11:44:53.887 回答
4

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

就像是:

return ShapeDecoration(shape: UnderlineInputBorder(), gradient: LinearGradient(
    colors: [Colors.blue, Colors.green]));

升级版:

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

return Rect.fromLTWH(tabLeft, 0.0, tabRight - tabLeft, tabBarSize.height);

此字符串返回 rect 进行装饰。如果你改变它 - 你可以得到下划线:

return Rect.fromLTWH(tabLeft, tabBarSize.height - 4.0, tabRight - tabLeft, 4.0);

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

return ShapeDecoration(shape: RoundedRectangleBorder(), gradient: LinearGradient(
    colors: [Colors.blue, Colors.green]));

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

于 2018-10-23T11:03:53.423 回答