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

时间:2018-10-23 10:02:52

标签: android dart flutter

我具有选项卡栏功能,其中显示了甜甜圈图和车辆列表,但我需要显示用户为此选择的选项卡,我在TabBar中具有indicatorColor,但需要填充渐变线,如图所示请帮帮我。

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

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:1)

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

类似的东西:

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

UPD:

知道了。 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]));

这是结果 enter image description here

答案 1 :(得分:1)

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

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或之后需要的尺寸,而不是在选项卡中使用文本,请在顶部使用Container并为其赋予颜色。