I have a horizontal ListView placed in a column, and I want it to have as much height as it only needs. One way to do this is to wrap the ListView in a SizedBox and give it a specific hardcoded height. Also, wrapping it in an Expanded widget will cause the ListView take the additional available space in the column. But, I want it to take as much height as it only needs, automatically.
How can this be achieved? Thank you.
The sample code is as follows:
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(20),
child: Column(
children: [
Text("Hi there!"),
SizedBox(
height: 50,
child: ListView(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
children: List.generate(
4,
(index) => Text("Item $index "),
),
),
),
ElevatedButton(
onPressed: () {},
child: Text("This is a button"),
),
],
),
),
),
);
}
But what I want (no more or less space around the ListView):


