问题
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