简体   繁体   English

Flutter:ListView 中动态大小的小部件

[英]Flutter: Dynamic sized Widget in ListView

I would like to create a page with a ListView with a dynamically sized widget to archive this behavior:我想创建一个带有 ListView 的页面,其中包含一个动态大小的小部件来存档此行为:

在此处输入图片说明

The dynamically sized widget (shown in red) has a minimal and maximal size.动态大小的小部件(以红色显示)具有最小和最大尺寸。

  1. When there is enough space on the page/listView for all elements, the dynamically sized widget should have its maximum size and there should not be any scrolling behavior in the listView.当页面/listView 上有足够的空间容纳所有元素时,动态调整大小的小部件应具有最大尺寸,并且在 listView 中不应有任何滚动行为。
  2. When there is not enough space for all the elements and the full maximum size widget, the widget should decrease in size until it fits exactly.当所有元素和完整的最大尺寸小部件没有足够的空间时,小部件应该减小尺寸直到完全适合。 There should be no scroll behavior.不应该有滚动行为。
  3. If the widget would have to decrease below its minimal size to make all elements fit the listView, it should keep its minimal size.如果小部件必须减小到其最小尺寸以下以使所有元素适合 listView,则它应保持其最小尺寸。 Since there is not enough space for elements, the ListView should be scrollable.由于元素没有足够的空间,ListView 应该是可滚动的。

How can I archive this behavior in flutter?如何在颤振中存档此行为?

You can use SliverAppBar with a flexibleSpace all inside a CustomScrollView您可以在CustomScrollView 中使用带有灵活空间的 SliverAppBar

CustomScrollView(
        slivers: <Widget>[
          SliverAppBar(
            floating: false,
            pinned: true,
            snap: false,
            expandedHeight: 150.0,
            flexibleSpace: FlexibleSpaceBar(
                title: Text("SliverAppBar Widget"),
                ),
          ),
          SliverFixedExtentList(
            itemExtent: 50.0,
            delegate: SliverChildBuilderDelegate(
              (BuildContext context, int index) {
                return Container(
                  alignment: Alignment.center,
                  color: Colors.lightBlue[100 * (index % 9)],
                  child: Text('List Item $index'),
                );
              },
            ),
          ),
        ],
      )

You can see an example in my code你可以在我的代码中看到一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM