Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Flutter: Minimum height on horizontal list view

I'm trying to create a horizontal scrolling list of items in Flutter, and I want that list to only take up the necessary height based on its children. By design "ListView tries to expand to fit the space available in its cross-direction" (from the Flutter docs), which I also notice in that it takes up the whole height of the viewport, but is there a way to make it not do this? Ideally something similar to this (which obviously doesn't work):

new ListView(
 scrollDirection: Axis.horizontal,
 crossAxisSize: CrossAxisSize.min,
 children: <Widget>[
 new ListItem(),
 new ListItem(),
 // ...
 ],
);

I realize that one way to do this is by wrapping the ListView in a Container with a fixed height. However, I don't necessarily know the height of the items:

new Container(
 height: 97.0,
 child: new ListView(
 scrollDirection: Axis.horizontal,
 children: <Widget>[
 new ListItem(),
 new ListItem(),
 // ...
 ],
 ),
);

I was able to hack together a "solution" by nesting a Row in a SingleChildScrollView in a Column with a mainAxisSize: MainAxisSize.min. However, this doesn't feel like a solution, to me:

new Column(
 mainAxisSize: MainAxisSize.min,
 children: <Widget>[
 new SingleChildScrollView(
 scrollDirection: Axis.horizontal,
 child: new Row(
 children: <Widget>[
 new ListItem(),
 new ListItem(),
 // ...
 ],
 ),
 ),
 ],
);

Answer*

Draft saved
Draft discarded
Cancel

lang-dart

AltStyle によって変換されたページ (->オリジナル) /