Flutter. How to expand first child of row to listview height as second child?

匆匆过客 提交于 2020-04-12 02:28:49

问题


My example code:

  Row(
      children: <Widget>[
        Container(
          color: Colors.red,
          child: Text('text'),
        ),
        Expanded(
          child: Container(
            color: Colors.green,
            child: ListView(
              shrinkWrap: true,
              children: <Widget>[
                ListTile(
                  title: Text('Title1'),
                ),
                ListTile(
                  title: Text('Title2'),
                )
              ],
            ),
          ),
        )
      ],
    )

How to expand first Container to Listview height? I tried IntrinsicHeight, but it do not works with listview.


回答1:


Assign a list controller to the list and use its position.viewportDimension property to get the height.

There, however, has to be a frame drop as you have to wait for the list item to be laid on screen before you can access the viewport dimension.

Check this example in dartpad

Press the + button in the example to add list items. You can notice the frame lag there but, it works.

Hope this helps!




回答2:


Much simpler way would be to change Listview to Column & wrap it with SingleChildScrollView then wrap Row with IntrinsicHeight

Code:

body: IntrinsicHeight(
    child: Row(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        Container(
          color: Colors.red,
          child: Text('text'),
        ),
        Flexible(
          child: Container(
            color: Colors.green,
            child: SingleChildScrollView(
              child: Column(
                //   shrinkWrap: true,
                children: <Widget>[
                  ListTile(
                    title: Text('Title1'),
                  ),
                  ListTile(
                    title: Text('Title2'),
                  ),
                  ListTile(
                    title: Text('Title1'),
                  ),
                  ListTile(
                    title: Text('Title2'),
                  ),
                  ListTile(
                    title: Text('Title1'),
                  ),
                ],
              ),
            ),
          ),
        )
      ],
    ),
  ),


来源:https://stackoverflow.com/questions/61012369/flutter-how-to-expand-first-child-of-row-to-listview-height-as-second-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!