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

前端 未结 12 585
情书的邮戳
情书的邮戳 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.

    0 讨论(0)
  • 2020-12-24 11:38

    Here is the solution:

    SingleChildScrollView(
            physics: ScrollPhysics(),
            child: Column(
              children: <Widget>[
                 Text('Hey'),
                 ListView.builder(
                    physics: NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    itemCount:18,
                    itemBuilder: (context,index){
                      return  Text('Some text');
                    })
              ],
            ),
          ),
    
    0 讨论(0)
  • 2020-12-24 11:39

    Placing the ListView inside an Expanded widget should solve your problem:

    @override
    Widget build(BuildContext context) {
    return new Column(
      mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: MainAxisSize.max,
        children: <Widget>[
          new Padding(
              padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
              child: new TextField(
                decoration: new InputDecoration(
                    hintText: "Type in here!"
                ),
              )
          ),
          new Expanded(child: ListView.builder(
              itemCount: _posts.length, itemBuilder: _postBuilder))
        ],
       );
    }
    
    0 讨论(0)
  • 2020-12-24 11:40

    Column is not scrollable, which is why the TextField on top wouldn't scroll but the ListView on the bottom would.

    The best way to solve this in my opinion is to make your TextField the first item in your ListView.

    So you won't need a column, your parent widget is the ListView, and its children are the TextField followed by the remaining items you build with _postBuilder.

    0 讨论(0)
  • 2020-12-24 11:47

    Solution

    SingleChildScrollView(
              child: Column(
                children: <Widget>[
                          ListView.builder(
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
    

    Two properties used here

    shrinkWrap: true
    

    only occupies the space it needs (it will still scroll when there more items).

    physics: NeverScrollableScrollPhysics()
    

    Scroll physics that does not allow the user to scroll. Means only Column+SingleChildScrollView Scrolling work.

    0 讨论(0)
  • 2020-12-24 11:48

    Use physics: NeverScrollableScrollPhysics() and shrinkWrap: true inside ListView.Builder() and enjoy

    0 讨论(0)
提交回复
热议问题