在Flutter中重叠SliverList子项

3

基于这个设计:

enter image description here

我正在尝试使用SliverListSliverAppBar,但似乎无法重叠项目,因此当应用左上角和右上角半径时,前一个项目的颜色会出现。 类似于这篇文章: How to overlap SliverList on a SliverAppBar

但是,我正在尝试将Stack应用于所有SliverList子项。到目前为止,我取得的最好的成就是通过在另一个Container中包装该项并应用先前的背景颜色来解决问题:

CustomScrollView(
  slivers: <Widget>[
    SliverAppBar(
      title: Text('Test'),
    ),
    SliverList(
      delegate: SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return Container(
            child: Container(
              child: Column(
                children: <Widget>[
                  Container(
                    child: Text(
                      'Index is $index'.toUpperCase(),
                    ),
                    alignment: Alignment.centerLeft,
                    padding: EdgeInsets.only(bottom: 10.0),
                  ),
                  Container(height: 200.0)
                ],
              ),
              constraints: BoxConstraints.tightForFinite(width: 200),
              decoration: BoxDecoration(
                color:
                    index % 2 == 0 ? Color(0XFF45766E) : Color(0XFFECB141),
                borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(40.0),
                  topRight: Radius.circular(40.0),
                ),
              ),
              padding: EdgeInsets.only(
                left: 20.0,
                top: 10.0,
              ),
            ),
            decoration: BoxDecoration(
              color: index % 2 == 0 ? Color(0XFFECB141) : Color(0XFF45766E),
            ),
          );
        },
      ),
    ),
  ],
);

谢谢!


请问您能否添加一个视频剪辑,展示您想要做的事情? - Josteve
2个回答

2
CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            title: Text('Test'),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return Container(
                  child: Container(
                    child: Column(
                      children: <Widget>[
                        Container(
                          child: Text(
                            'Index is $index'.toUpperCase(),
                          ),
                          alignment: Alignment.centerLeft,
                          padding: EdgeInsets.only(bottom: 10.0),
                        ),
                        Container(height: 200.0)
                      ],
                    ),
                    constraints: BoxConstraints.tightForFinite(width: 200),
                    decoration: BoxDecoration(
                      color: index % 2 == 0
                          ? Color(0XFF45766E)
                          : Color(0XFFECB141),
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(40.0),
                        topRight: Radius.circular(40.0),
                      ),
                    ),
                    padding: EdgeInsets.only(
                      left: 20.0,
                      top: 10.0,
                    ),
                  ),
                  decoration: BoxDecoration(
                    //the only change required here
                    color: index % 2 == 0
                        ? index == 0 ? Colors.white : Color(0XFFECB141)
                        : Color(0XFF45766E),
                  ),
                );
              },
            ),
          ),
        ],
      );

您只需要明确地更改第一个位置即可。

这只是一个解决方法,我试图实现的目标可能更好地通过比较来解释:就像您可以使用子构建器创建无限数量的项目列表而不必明确说明数量一样(除非您使用childCount),我想要无限地创建一系列自定义小部件,它们略微重叠在彼此之上,而不指定父Container的高度并且不使用解决方法。我实际上要做的是将列表的子项堆叠在委托内部。如果使用了positioned,则需要显式高度。 - tevvek

0
CustomScrollView->
  SliverList->
    SliverChildBuilderDelegate->
      Container->
        Stack->children[
          Container, 
          positioned(bottom:0, 
            child: Container(decoration: BoxDecoration(
                      color: (next index color),
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(40.0),
                        topRight: Radius.circular(40.0),
                      ),
                    ),)
          )
        ]

这个能用吗?因为它类似于我做过的某些东西,但是特定位置的使用,例如 bottom: 0,会破坏代码,因为它需要知道它可以增长到的高度,因为它倾向于无限增长。 - tevvek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接