【问题标题】:Flutter custom scrolling with dynamic list带有动态列表的 Flutter 自定义滚动
【发布时间】:2019-07-09 18:55:30
【问题描述】:

有一个关于flutter的问题CustomScrollView

我现在拥有的是:

@override
Widget build(BuildContext context) {
  return Container(
    child: CustomScrollView(
      shrinkWrap: true,
      slivers: <Widget>[
        SliverPadding(
          padding: EdgeInsets.all(0),
          sliver: SliverList(
            delegate: SliverChildListDelegate(
              <Widget>[
                Card(),
                Card(),
                Container(
                  child: ListView.builder(
                    physics: const NeverScrollableScrollPhysics(),
                  ),
                )
              ],
            ),
          ),
        ),
      ],
    ),
  );
}

*代码被简化为示例。

如您所见,有 2 个Cards() 和一个Container,里面有ListView。两张卡片都有固定高度,带有列表的容器具有动态高度。 现在它是如何工作的:随着 ListView 的成长,我们可以滚动到列表的底部。所以通常两个Card 小部件都变得不可见,因为List 的元素占据了整个屏幕。

这个想法是强制将第二个Card() 固定到屏幕顶部。所以基本上它应该在它自己的位置上,但是随着越来越多的滚动它应该固定在屏幕顶部,而我越来越多地滚动列表。 问题是我该如何做这样的事情?

谢谢!

【问题讨论】:

  • 您只想将第二张卡片固定在顶部吗?

标签: flutter flutter-layout


【解决方案1】:

你可以使用SliverPersistentHeader,我为你准备了这个示例:

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SafeArea(
            child: CustomScrollView(
              slivers: <Widget>[
                SliverPadding(
                  padding: EdgeInsets.all(0),
                  sliver: SliverList(
                      delegate: SliverChildBuilderDelegate(
                    (context, index) {
                      return ListTile(
                        title: Text("index: $index"),
                      );
                    },
                    childCount: 3,
                  )),
                ),
                SliverPersistentHeader(
                  pinned: true,
                  delegate: PersistentHeader("Card 1"),
                ),
                SliverPersistentHeader(
                  pinned: true,
                  delegate: PersistentHeader("Card 2"),
                ),
                SliverPadding(
                  padding: EdgeInsets.all(0),
                  sliver: SliverList(
                      delegate: SliverChildBuilderDelegate((context, index) {
                    return ListTile(
                      title: Text("index: $index"),
                    );
                  })),
                ),
              ],
            ),
          ),
        );
      }
    }

    class PersistentHeader extends SliverPersistentHeaderDelegate {
      final String title;

      PersistentHeader(this.title);

      @override
      Widget build(
          BuildContext context, double shrinkOffset, bool overlapsContent) {
        return Container(
          color: Colors.white,
          child: Card(
            color: Colors.white,
            elevation: 7.0,
            child: SizedBox(height: 100.0, child: Center(child: Text(title))),
          ),
        );
      }

      @override
      double get maxExtent => 100.0;

      @override
      double get minExtent => 100.0;

      @override
      bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
        return false;
      }
    }

更多信息:https://docs.flutter.io/flutter/widgets/SliverPersistentHeader-class.html

【讨论】:

  • 谢谢。这对我有用。还有一个问题。在这些对象之间传输数据的最佳方式是什么?我的意思是在卡片 1、卡片 2 和列表之间。因为他们目前在不同的班级。
  • 我试过用它,但没有用。我的意思是:继承的小部件包含最终对象。我正在从不同的地方修改最终对象的一些字段。问题是我希望更改对象字段以重建 InheritedWidget 的整个树。我哪里错了?这是 InheritedWidget 案例吗?
猜你喜欢
  • 1970-01-01
  • 2022-12-28
  • 2018-06-07
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
相关资源
最近更新 更多