如何在Flutter中向标头ExpansionTile添加渐变

时间:2020-02-26 07:42:45

标签: flutter header gradient expand

我正在尝试在ExpansionTile中获得某种外观, 1.折叠后的纯标题颜色 2.当标题中的展开渐变仅在带有元素的正文中不显示。

这是我要实现的目标: enter image description here

我正在尝试使用以下代码:

            Theme(
              data: ThemeData(
                //brightness: Brightness.dark,
                primaryColor: Colors.black87,
                accentColor: Colors.black87,
                dividerColor: Colors.transparent,
              ),
              child: Container(
                decoration: BoxDecoration(
                    border: Border.all(
                        width: 1, color: Color.fromRGBO(121, 85, 72, 1)),
                    gradient: LinearGradient(
                        begin: FractionalOffset.bottomCenter,
                        end: FractionalOffset.topCenter,
                       // stops: [0.1, 1.0],
                       // tileMode: TileMode.clamp,


                        colors: [
                          isExpanded
                              ? Color.fromRGBO(255,255,255,100)
                              : Color.fromRGBO(197, 181, 176, 1),
                          Color.fromRGBO(197, 181, 176, 1)//closed solid
                        ])),
                // color: Colors.brown,
                child: ExpansionTile(
              //backgroundColor: Colors.amberAccent,
                  trailing: isExpanded //assets/collapse_arrow.png
                      ? Image.asset('assets/collapse_arrow.png')
                      : Image.asset('assets/expand_arrow.png'),
                  onExpansionChanged: (bool expanding) =>
                      setState(() => isExpanded = expanding),
                  title: Text(
                    'Header',
                    style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                  ),
                  children: <Widget>[

                    _dropDownItem('Rank', _selectRankFamily2,
                        _rankFamily2, 'Select Rank'),
                    _dropDownItem('Rank', _selectRankFamily2,
                        _rankFamily2, 'Select Rank'),
                    _dropDownItem('Rank', _selectRankFamily2,
                        _rankFamily2, 'Select Rank')
                  ],
                ),
              ),
            ),

但是我得到这个结果: enter image description here

所以渐变不只是标题,是否有一种方法可以只对标题部分进行渐变或纯色,并且仅在扩展时才进行? 谢谢/

*编辑 如果我使用摘要答案仅包装TITLE,则会得到以下结果:

enter image description here

所以我需要以某种方式包装尾随图标或什么?或在顶部制作“假”可扩展框。

2 个答案:

答案 0 :(得分:0)

您要用容器包装整个扩展图块,而应该只包装标题小部件,这可能就是您要寻找的

Theme(
      data: ThemeData(
        //brightness: Brightness.dark,
        primaryColor: Colors.black87,
        accentColor: Colors.black87,
        dividerColor: Colors.transparent,
      ),
      child: ExpansionTile(
        //backgroundColor: Colors.amberAccent,
        trailing: isExpanded //assets/collapse_arrow.png
            ? Image.asset('assets/collapse_arrow.png')
            : Image.asset('assets/expand_arrow.png'),
        onExpansionChanged: (bool expanding) =>
            setState(() => isExpanded = expanding),
        title: Container(
          decoration: BoxDecoration(
              border:
                  Border.all(width: 1, color: Color.fromRGBO(121, 85, 72, 1)),
              gradient: LinearGradient(
                  begin: FractionalOffset.bottomCenter,
                  end: FractionalOffset.topCenter,
                  // stops: [0.1, 1.0],
                  // tileMode: TileMode.clamp,

                  colors: [
                    isExpanded
                        ? Color.fromRGBO(255, 255, 255, 100)
                        : Color.fromRGBO(197, 181, 176, 1),
                    Color.fromRGBO(197, 181, 176, 1) //closed solid
                  ])),
          child: Text(
            'Header',
            style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
          ),
        ),
        children: <Widget>[
          _dropDownItem(
              'Rank', _selectRankFamily2, _rankFamily2, 'Select Rank'),
          _dropDownItem(
              'Rank', _selectRankFamily2, _rankFamily2, 'Select Rank'),
          _dropDownItem('Rank', _selectRankFamily2, _rankFamily2, 'Select Rank')
        ],
      ),
    ),

答案 1 :(得分:0)

我找到了,

如果检查ExpansionTitle的源代码,您会注意到标题项是ListTile,因此您不能更改背景,因为它没有具有color属性的父项。 我对类进行了一些修改以支持您的需求。

将此文件添加到您的项目中:https://gist.github.com/diegoveloper/02424eebd4d6e06ae649b31e4ebcf53c << / p>

现在有一种类似的方法可以超速驱动纯色的Gradient属性吗?