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*

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
6
  • I am using this workaround. I don't know why we don't have a way to provide a prototypeItem that applies to the crossAxis. The one we have only applies to the main axis. This would make everything much easier. Commented Oct 5, 2022 at 21:02
  • Or, at least, have a native PrototypeConstraints widget. I think I am going to make a package that implements something like this so we don't have to rely on Stack. Commented Oct 5, 2022 at 21:03
  • 1
    Here it is, still in non-stable version: pub.dev/packages/prototype_constrained_box Commented Oct 13, 2022 at 23:45
  • This also has lazily build compatibility? Commented Nov 10, 2023 at 2:01
  • 1
    @Hayyaun Yes, the list can have infinite items because it's dynamically loaded. Commented Nov 10, 2023 at 5:31

lang-dart

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