Flutter ListView.Builder() in scrollable Column with other widgets

前端 未结 12 600
情书的邮戳
情书的邮戳 2020-12-24 10:58

I have a TabBarView() with an amount of different views. I want of them to be a Column with a TextField at top and a ListView.Builder() below, but both widgets should be in

12条回答
  •  轮回少年
    2020-12-24 11:36

    Here is an efficient solution:

    class NestedListExample extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return CustomScrollView(
          slivers: [
            const SliverToBoxAdapter(
              child: Text('Header'),
            ),
            SliverList(
              delegate: SliverChildBuilderDelegate(
                (ctx, index) {
                  return ListTile(title:Text('Item $index'));
                },
              ),
            ),
          ],
        );
      }
    }
    

    Here is a preview on dartpad.

    You can use a SliverToBoxAdapter for the other children as only Slivers can be a direct child of a CustomScrollView.

    If all the list items are the same height, then you could use SliverFixedExtentList, which is more efficient because the height of each child isn't calculated on the fly, but you will have to know the exact pixel height. You could also use a SliverPrototypeExtentList, where you provide the first item in the list(the prototype), and all the other children will use the height of the prototype so you don't need to know the exact height in pixels.

提交回复
热议问题