【问题标题】:Using a SliverFillRemaining with a CustomScrollView and SliverList将 SliverFillRemaining 与 CustomScrollView 和 SliverList 一起使用
【发布时间】:2018-04-01 21:15:51
【问题描述】:

我正在尝试创建一个可以在滚动列表底部有页脚的滚动列表。如果列表没有填满所有的垂直屏幕空间,页脚将需要向下移动到页面底部。

我尝试在CustomScrollView 中使用SliverListSliverFillRemaining 来实现这一点,但我相信SliverFillRemaining 会出现一些意外行为。它占用了比需要更多的空间(参见 gif)。

我使用以下代码创建了这个列表:

child: new CustomScrollView(
  slivers: <Widget>[
    new SliverList(
      delegate: new SliverChildBuilderDelegate(
        (BuildContext context, int index) {
          return new Card(
            child: new Container(
              padding: new EdgeInsets.all(20.0),
              child: new Center(child: new Text("Card $index")),
            ),
          );
        },
        childCount: 3,
      ),
    ),
    new SliverPadding(
      padding: new EdgeInsets.all(5.0),
    ),
    new SliverFillRemaining(
      child: new Container(
        color: Colors.red,
      ),
    )
  ],
)

【问题讨论】:

  • 来自 SliverFillRemaining 的文档 - “一个包含单个框子的条子,填充视口中的剩余空间”和“除了这个条子之外,永远没有任何空间。”这似乎完全在意料之中。
  • @JonahWilliams 尽管SliverFillRemaining 之后没有其他小部件。另外,我想我误解了“包含一个填充视口中剩余空间的单个盒子子元素的条子”。你能澄清一下吗?
  • 我遇到了同样的问题。有一个SliverList 后跟一个SliverFillRemaining,在CustomScrollView 中包含一个小部件,我希望滚动视图不会滚动,因为所有内容都适合屏幕。我注意到SliverFillRemaininghasScrollBody 参数默认为true,但我的内容不会滚动。所以我把它切换到false,这解决了我的问题。没有深入挖掘。
  • 谢谢@t.animal。对我有用。

标签: dart flutter


【解决方案1】:

对于任何正在寻找这个问题的答案的人,我有一个解决方案,只要我需要类似的东西,它就可以很好地工作。

这就是我的管理方式:

class ScrollOrFitBottom extends StatelessWidget {
  final Widget scrollableContent;
  final Widget bottomContent;

  ScrollOrFitBottom({this.scrollableContent, this.bottomContent});

  @override
  Widget build(BuildContext context) {
    return CustomScrollView(
      slivers: <Widget>[
        SliverFillRemaining(
          hasScrollBody: false,
          child: Column(
            children: <Widget>[
              Expanded(child: scrollableContent),
              bottomContent
            ],
          ),
        ),
      ],
    );
  }
}

顾名思义,如果内容太多,它会滚动,否则将某些东西推到底部。

我认为这很简单且不言自明,但如果您有任何问题,请告诉我

示例:https://codepen.io/lazarohcm/pen/xxdWJxb

【讨论】:

    【解决方案2】:

    SliverFillRemaining 将自动调整大小以填充最后一个列表项底部和视口底部之间的空间。代码见SliverFillRemainingperformLayout方法:

    https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/rendering/sliver_fill.dart#L118

    我不认为你可以使用它来实现你想要的效果,尽管你可以创建一个可以工作的子类。

    【讨论】:

      【解决方案3】:

      查看我的回答 here 和 Gist 文件 here。它可能会引导你走向正确的方向。

      @override
      Widget build(BuildContext context) {
        return LayoutBuilder(
          builder: (BuildContext context, BoxConstraints constraints) {
            return SingleChildScrollView(
              child: ConstrainedBox(
                constraints: constraints.copyWith(
                  minHeight: constraints.maxHeight,
                  maxHeight: double.infinity,
                ),
                child: IntrinsicHeight(
                  child: Column(
                    children: <Widget>[
                      Container(height: 200, color: Colors.blue),
                      Container(height: 200, color: Colors.orange),
                      Container(height: 200, color: Colors.green),
                      Container(height: 50, color: Colors.pink),
                      Expanded(
                        child: Align(
                          alignment: Alignment.bottomCenter,
                          child: Container(
                            width: double.infinity,
                            color: Colors.red,
                            padding: EdgeInsets.all(12.0),
                            child: Text('FOOTER', textAlign: TextAlign.center,),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            );
          }
        );
      }
      

      【讨论】:

      • 如果你有一个特定的问题希望我从上面的代码中回答,请说清楚。否则,期望逐行解释是不合理的。正如我上面提到的,这只是一个例子。事实上,这个特定的主题在 Flutter 中是一个非常难以实现的主题。如果你有兴趣,Flutter 上有几个讨论为什么会这样。 github.com/flutter/flutter/issues/38641github.com/flutter/flutter/issues/38640 供您参考。
      猜你喜欢
      • 1970-01-01
      • 2018-09-25
      • 2021-08-01
      • 2021-05-03
      • 2021-02-14
      • 2019-11-20
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      相关资源
      最近更新 更多