How to add a ListView to a Column in Flutter?

后端 未结 13 1116
死守一世寂寞
死守一世寂寞 2020-11-30 22:53

I\'m trying to construct a simple login page for my Flutter app. I\'ve successfully built the TextFields and log in/Sign in buttons. I want to add a horizontal ListView.

13条回答
  •  抹茶落季
    2020-11-30 23:29

    Reason for the error:

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

    Solutions

    So, you need to constrain the height of the ListView. There are many ways of doing it, you can choose that best suits your need.


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

      Column(
        children: [
          Expanded(
            child: ListView(...),
          )
        ],
      )
      

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

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

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

      Column(
        children: [
          ListView(
            shrinkWrap: true, // use it
          )
        ],
      )
      

提交回复
热议问题