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

前端 未结 12 586
情书的邮戳
情书的邮戳 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:48

    Use Expanded widget to constrain without overflowing those pixels, :)

    Column(
      children: <Widget>[
        Expanded(
          child: ListView(),
        ),
        Expanded(
          child: ListView(),
        ),
      ],
    )
    
    0 讨论(0)
  • 2020-12-24 11:49

    Reason for the error:

    Column expands to the maximum size in main axis direction (vertical axis), and so does the ListView

    Solution

    You need to constrain the height of the ListView, so that it does expand to match Column, there are several ways of solving this issue, I'm listing a few here:


    1. If you want to allow ListView to take up all remaining space inside Column use Flexible.

      Column(
        children: <Widget>[
          Flexible(
            child: ListView(...),
          )
        ],
      )
      

    1. If you want to limit your ListView to certain height, you can use SizedBox.

      Column(
        children: <Widget>[
          SizedBox(
            height: 200, // constrain height
            child: ListView(),
          )
        ],
      )
      

    1. If your ListView is small, you may try shrinkWrap property on it.

      Column(
        children: <Widget>[
          ListView(
            shrinkWrap: true, // use it
          )
        ],
      )
      
    0 讨论(0)
  • 2020-12-24 11:50

    The best way will be to make the column scrollable by making the column child of SingleChildScrollView and then assigning the same ScrollController to both the SingleChildScrollView and the ListView.builder. This will make the text field and the below ListView as scrollable.

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

    Just add physics: NeverScrollableScrollPhysics() in ListView.builder() so you can scroll

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

    Add physics: NeverScrollableScrollPhysics() inside Listview.builder() method and the nested Listview will scroll

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

    In my case with a future i did it like this:

    SingleChildScrollView(
          physics: ScrollPhysics(),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              Text("Hey ho let's go!"),
              
              Flexible(
                child: FutureBuilder(
                  future: getData(),
                  builder: (BuildContext context,
                      AsyncSnapshot<List<Sale>> snapshot) {
                    
    
                    if (snapshot.connectionState != ConnectionState.done ||
                        snapshot.hasData == null) {
                      
                        return CircularProgressIndicator();
    
                    } else {
                      data = snapshot.data;
                      return ListView.builder(
                          physics: NeverScrollableScrollPhysics(),
                          shrinkWrap: true,
                          itemBuilder: (BuildContext context, int index) {
                            return dataItemWidget(size, data[index], context);
                          },
                          itemCount: data.length,
                        );
                    }
                  },
                ),
              ),
            ],
          ),
        ),
    
    0 讨论(0)
提交回复
热议问题