325

This is my code:

@override
Widget build(BuildContext context) {
  return new Material(
    color: Colors.deepPurpleAccent,
    child: new Column(
     mainAxisAlignment: MainAxisAlignment.center,
        children:<Widget>[new GridView.count(crossAxisCount: _column,children: new List.generate(_row*_column, (index) {
          return new Center(
              child: new CellWidget()
          );
        }),)]
    )
  );
}

Exception as follows:

I/flutter ( 9925): ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
I/flutter ( 9925): The following assertion was thrown during performResize():
I/flutter ( 9925): Vertical viewport was given unbounded height.
I/flutter ( 9925): Viewports expand in the scrolling direction to fill their container.In this case, a vertical
I/flutter ( 9925): viewport was given an unlimited amount of vertical space in which to expand. This situation
I/flutter ( 9925): typically happens when a scrollable widget is nested inside another scrollable widget.
I/flutter ( 9925): If this widget is always nested in a scrollable widget there is no need to use a viewport because
I/flutter ( 9925): there will always be enough vertical space for the children. In this case, consider using a Column
I/flutter ( 9925): instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
I/flutter ( 9925): the height of the viewport to the sum of the heights of its children.
I/flutter ( 9925): 
I/flutter ( 9925): When the exception was thrown, this was the stack:
I/flutter ( 9925): #0      RenderViewport.performResize.<anonymous closure> (package:flutter/src/rendering/viewport.dart:827:15)
I/flutter ( 9925): #1      RenderViewport.performResize (package:flutter/src/rendering/viewport.dart:880:6)
I/flutter ( 9925): #2      RenderObject.layout (package:flutter/src/rendering/object.dart:1555:9)
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
pallav bohara
  • 6,199
  • 6
  • 24
  • 45
  • you can also use simple Container as well instead of Column(mainly for more than one child widget.) – Napolean May 10 '18 at 07:44
  • 1
    Widget build(BuildContext context) { return new Material( color: Colors.deepPurpleAccent, child: new Container( alignment: Alignment.center, child: new GridView.count(crossAxisCount: _column,children: new List.generate(_row*_column, (index) { return new Center( child: new CellWidget() ); }) ), ) ); --This code also does not helps @Napolean – pallav bohara May 14 '18 at 06:28
  • try adding mainAxisSize to max under Column, and see if it helps. – pblead26 Jun 11 '18 at 12:28

27 Answers27

710

Add this two lines

    ListView.builder(
        scrollDirection: Axis.vertical,
        shrinkWrap: true,
    ...

In this code snippet, have a ListView.builder with the following properties:

  1. scrollDirection: Axis.vertical: This property specifies the scrolling direction of the list. In this case, it is set to Axis.vertical, which means the list will scroll vertically. You can also set it to Axis.horizontal if you want the list to scroll horizontally.

  2. shrinkWrap: true: By default, a ListView takes up as much space as it can along its main axis. However, when you set shrinkWrap to true, the ListView will try to wrap its content and only take up as much space as needed. This can be helpful when you want to include a ListView inside another scrollable view or when the size of the list is not predetermined.

BIS Tech
  • 17,000
  • 12
  • 99
  • 148
  • 41
    Thanks,just a small advice you can surround the ListView by Flexible to make it scroll-able – Shady Mohamed Sherif Feb 26 '20 at 13:17
  • 9
    Here is the documentation for `shrinkWrap` https://api.flutter.dev/flutter/widgets/ScrollView/shrinkWrap.html – Omer Celik Apr 21 '20 at 21:51
  • 16
    For future reference, please be aware that, according to the documentation, `shrinkWrap` being set to true can be significantly more expensive if there are a lot of widgets in the list view. Please see this snippet from the documentation: `Shrink wrapping the content of the scroll view is significantly more expensive than expanding to the maximum allowed size because the content can expand and contract during scrolling, which means the size of the scroll view needs to be recomputed whenever the scroll position changes.` – Nirvan Nagar Feb 14 '21 at 15:06
  • 9
    The `shrinkWrap: true` was expensive and it made my list laggy, and this was solved by surrounding my ListView with a Flexible. – RukshanJS Mar 18 '21 at 20:38
  • @BloodLoss I use shrinkWrap: true, my problem is solved but the scoll does not work on listView..why – benten Jun 09 '21 at 06:21
  • ListView.builder( scrollDirection: Axis.vertical, shrinkWrap: true, ) – benten Jun 09 '21 at 10:01
303

This generally happens when you try to use a ListView/GridView inside a Column, there are many ways of solving it, I am listing few here.

  1. Wrap ListView in Expanded

    Column(
      children: <Widget>[
        Expanded( // wrap in Expanded
          child: ListView(...),
        ),
      ],
    )
    
  2. Wrap ListView in SizedBox and give a bounded height

    Column(
      children: <Widget>[
        SizedBox(
          height: 400, // fixed height
          child: ListView(...),
        ),
      ],
    )
    
  3. Use shrinkWrap: true in ListView.

    Column(
      children: <Widget>[
        ListView(
          shrinkWrap: true, // use this
        ),
      ],
    )
    
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
  • 10
    Tried this with a vertical listview containing a horizontal listview. Only the sizedbox method would work but its too prescriptive on the height. How else to do this? – fractal Feb 11 '20 at 23:08
  • 4
    `shrinkWrap: true` did the job – Ali80 Jun 14 '20 at 08:43
  • This is what I was looking for. Any chance you can add the pros and cons of each choice? – emragins Aug 08 '20 at 00:19
  • @fractal, I agree, only `SizedBox` solution worked for me as well when placing a `ListView` inside a `Column`. You can get the device screen size, and set `height` relative to that: https://stackoverflow.com/a/61718245/2162226 . Now working, wrap `Widget listView = ListView.builder(..)` this way: `listView = SizedBox(height: _scale.scaledHeight(.43), child: listView);` for the % of space visibly available on the screen – Gene Bo Sep 24 '20 at 17:58
  • For me wrapping GridView in Expanded and specifiying shrinkWap: true for GridView worked, I needed to add both. – Munes Jan 11 '21 at 03:06
  • @Munes If that's the case, simply wrap your `GridView` in `Flexible`, you may not need to use `shrinkWrap` then. – CopsOnRoad Jan 11 '21 at 03:35
  • 2
    This is the best answer. Just be aware that if the `ListView` has a lot of elements inside and `shrinkWrap` is set to `true`, it is significantly more expensive. Taken from the documentation: `"Shrink wrapping the content of the scroll view is significantly more expensive than expanding to the maximum allowed size because the content can expand and contract during scrolling, which means the size of the scroll view needs to be recomputed whenever the scroll position changes."` – Nirvan Nagar Feb 14 '21 at 15:13
  • @CopsOnRoad I use shrinkWrap: true, my problem is solved but the scoll does not work on listView..any idea – benten Jun 09 '21 at 06:23
  • @benten Use other solutions because `shrinkWrap` is only suitable if your `ListView` isn't too long. – CopsOnRoad Jun 10 '21 at 13:51
  • I use this => physics: ScrollPhysics(), it start working – benten Jun 21 '21 at 11:38
  • @benten It's better to remove `shrinkWrap` then if you're adding the `ScrollPhysics`. – CopsOnRoad Jun 21 '21 at 11:51
55

So, everyone posted answers but no one cared to explain why: I'll copy the documentation about shrinkWrap:

Whether the extent of the scroll view in the [scrollDirection] should be determined by the contents being viewed.

If the scroll view does not shrink wrap, then the scroll view will expand to the maximum allowed size in the [scrollDirection]. If the scroll view has unbounded constraints in the [scrollDirection], then [shrinkWrap] must be true.

Shrink wrapping the content of the scroll view is significantly more expensive than expanding to the maximum allowed size because the content can expand and contract during scrolling, which means the size of the scroll view needs to be recomputed whenever the scroll position changes.

Defaults to false.

So, taking the vocabulary of the docs, what's happening here is that our ListView is in a situation of unbounded constraints (in the direction that we are scrolling), thus the ListView will complain that:

... a vertical viewport was given an unlimited amount of vertical space in which to expand.

By simply setting shrinkWrap to true will make sure that it wraps its size defined by the contents. A sample code to illustrate:

// ...
  ListView(
    // Says to the `ListView` that it must wrap its
    // height (if it's vertical) and width (if it's horizontal).
    shrinkWrap: true,
  ),
// ...

That's what is going with your code, nonetheless, @Rémi suggestion is the best for this case, using Align instead of a Column.

Guilherme Matuella
  • 2,193
  • 1
  • 17
  • 32
41

put grid view inside Flexible or Expanded widget

return new Material(
    color: Colors.deepPurpleAccent,
    child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children:<Widget>[
         Flexible(
          child:  GridView.count(crossAxisCount: _column,children: new List.generate(_row*_column, (index) {
          return new Center(
             child: new CellWidget(),
          );
        }),))]
    )
);
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Manoj Kumar
  • 808
  • 9
  • 15
34

Cause of Viewport Exception

GridView / ListView grow until constraints (limits) stop this expansion & then scroll to view items beyond that size.

But Column lays out its children without any constraints, completely ignoring its own size and screen size. There is infinite space inside of Column.

Viewport was given unbounded height exception occurs because Grid/ListView expands to infinity inside a Column.

Flexible/Expanded exist to give Column children constraints based Column's size. (These two widgets cannot be used anywhere else, except inside Row and Column.)

Inside a Flexible/Expanded widget in Column, Grid/ListView only uses remaining space not taken by other, non-flexible widgets, which get laid out first. (See bottom for layout phases info.)

shrinkWrap is not a good solution

Using shrinkWrap: true on ListView inside a Column isn't really helpful as:

  • ListView no longer scrolls
  • ListView can still overflow

A ListView tall enough to show all of its items, will not scroll. Arguably defeats the purpose of using a ScrollView widget (parent class of ListView).

In Column layout phase 1 (see bottom for explanation of layout phases), ListView can be any height it wants (there are no constraints). A ListView with shrinkWrap:true will grow in height to show all its items. With enough items, a shrinkWrapped ListView will grow & grow (it never scrolls) to overflow whatever Column is inside, be it screen or other tighter constraint.

Shouldn't shrinkWrap just make ListView only as big as its items OR remaining space (up to screen height), and then scroll?

That would make intuitive sense, but inside a Column in phase 1 layout is done in unbounded space.

So, remaining space is unlimited/infinite. A max height is never reached. shrinkWrap:true just keeps growing Column height as items are added until overflowing the screen (or other smaller constraint).

Example shrinkWrap:true Overflow

Here's an example of adding items to a shrinkwrapped ListView in a Column until its height is taller than the screen, creating an overflow warning area:

(just keep pressing the + sign Floating Action Button)

import 'package:flutter/material.dart';

class ColumnListViewPage extends StatefulWidget {
  @override
  _ColumnListViewPageState createState() => _ColumnListViewPageState();
}

class _ColumnListViewPageState extends State<ColumnListViewPage> {
  int _count = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Column & ListView'),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          setState(() {
            _count++;
          });
        },
      ),
      body: SafeArea(
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(color: Colors.red)
          ),
          /// // without center, Column will be as narrow as possible
          alignment: Alignment.center,
          /// Column is forced (constrained) to match screen dimension,
          child: Column(
            children: [
              Text('Inner (Nested) Column'),
              ListView.separated( // ← should be wrapped in Expanded
                itemCount: _count,
                separatorBuilder: (context, index) => const Divider(),
                itemBuilder: (context, index) => Padding(
                  padding: const EdgeInsets.symmetric(vertical: 38.0),
                  child: Text('Row $index'),
                ),
                shrinkWrap: true, // should be removed
              )
            ],
          ),
        ),
      ),
    );
  }
}
For GridView/ListView in a Column, wrapping within Expanded or Flexible is likely the safest solution.

Expanded / Flexible

Expanded and Flexible can only be used inside Column (and its siblings like Row, etc.).

They must be used as immediate children of Column. We can't "nest" Expanded/Flexible in SizedBox or other widgets. Only directly under Column as immediate children.

Inside Column, placing ListView/GridView in Expanded or Flexible ensures it will use only available space, rather than infinite space.

Column
→ Expanded
  → ListView

ListView/GridView inside Expanded/Flexible in a Column doesn't need shrinkWrap because it is constrained (given a max. height) by the Expanded/Flexible widget. So when that constrained/defined height is used up, ListView/GridView will stop growing & begin scrolling.


Column Layout Phases

Column lays out children in 2 phases:

  • 1st without constraints (unbounded space)
  • 2nd with remaining space based on Column's parent

Phase 1

Any Column child not inside Flexible or Expanded will be laid out in phase 1, in infinite, limitless space completely ignoring screen size.

Widgets that need a vertical boundary/constraint to limit their growth (e.g. ListView) will cause a Vertical viewport was given unbounded height exception in phase 1 as there are no vertical bounds in unbounded space.

Most widgets have intrinsic size. Text for example, is only as high as its content & font size/style. It doesn't try to grow to fill a space. Defined-height widgets play well in Phase 1 of Column layout.

Phase 2

Any widget that grows to fill bounds, should be put in a Flexible or Expanded for Phase 2 layout.

Phase 2 calculates Column's remaining space from its parent constraints minus space used in Phase 1. This remaining space is provided to Phase 2 children as constraints.

ListView for example, will only grow to use remaining space & stop, avoiding viewport exceptions.

More info on Column layout algorithm and how Expanded works within it.

Baker
  • 24,730
  • 11
  • 100
  • 106
30

Although shrinkWrap do the thing, but you can't scroll in ListView.

If you want scrolling functionality, you can add physics property:

ListView.builder(
    scrollDirection: Axis.vertical,
    shrinkWrap: true,
    physics: ScrollPhysics(),
...

It is also helpful when you want to extend the scope of scrolling to other widgets of the screen:

Column(
  children: [
    ListTile(
    //Some other functionality
    ),
    ListView.builder(
      shrinkWrap: true,
      itemCount: 10,
      physics: ScrollPhysics(),
      // Some other functionality
    ),
  ],
),
Ali Radmanesh
  • 2,248
  • 22
  • 26
17

This situation typically happens when a scrollable widget is nested inside another scrollable widget.

In my case i use GridView inside of Column and that error throws.

GridView widget has shrinkWrap property/named parameter, to set it true my problem is fixed.

GridView.count(
  shrinkWrap: true,
  // rest of code
)
Robin
  • 4,902
  • 2
  • 27
  • 43
12

Don't use Column for single child alignment. Use Align instead.

new Align(
  alignment: Alignment.topCenter,
  child: new GridView(),
)
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • It though solved my error but still, I am not able to center my GridView vertically. It shows same as before. Tried all flags for Alignment.* What I am missing? – pallav bohara May 14 '18 at 06:20
10

There are a lot of answer but each one have its own limitation. everyone is saying use shrinkWrap: true. Yes you should use it but the effect of these code is when you will scroll the list, it will automatically move to top item, means scroll back to top. So perfect solution is use shrinkwrap with physics then only the list will be able to scroll smoothly.

      shrinkWrap: true,          //
      physics: ScrollPhysics(),  // both line is mandatory

If you want to see other example of Listview.builder with column as a child and list can be scroll vertically then you can see the example here - arrange multiple text in a vertical list.

Kamal Bunkar
  • 1,354
  • 1
  • 16
  • 20
8

Adding this two lines

ListView.builder( scrollDirection: Axis.vertical, shrinkWrap: true,

...

6

I wanted to post another answer because many answers (including the accepted one!) suggest using shrinkWrap: true in ListView.builder. While this removes the error, this solution is NOT ideal for performance reasons for this case. Flutter designed ListView with infinite size specifically to discourage this behaviour only when necessary!

Shrink wrapping the content of the scroll view is significantly more expensive than expanding to the maximum allowed size because the content can expand and contract during scrolling, which means the size of the scroll view needs to be recomputed whenever the scroll position changes.

In some cases/devices, this property caused glitchy scrolls and animation lag in my projects. Even if the lag isn't noticeable, this property does hinder scroll performance.

The OP shows that he is creating a small (finite) number of children for ListView. If we want to resolve the error for these lists, Google recommends using a Flexible widget, i.e. Expanded (a Flexible with flex 1) on the list itself.

Expanded(
    child: ListView(
        ...
    ),
) 

There are other layout issues here, but the vertical viewport unbounded height is because the ListView doesn't have a parent that bounds its size.

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
  • While true, there are many use cases where a ListView is legitimately surrounded with a parent like Semantics (see the image_picker example) and the shrinkWrap performs a valuable role in those cases. Thanks this was very helpful to understand the trade-offs. You may want to add ScrollPhysics to test the performance impact. Expanded needs to be nested as an immediate child of a flexible widget which can sometimes limit its use. – Tommie C. Sep 08 '21 at 13:11
4

Two ways which we can solve the above issues

1. Using Flexible Widget,

The Flexible widget can be used inside Row, Column, and Flex.The flexibility to expand to fill the available space in the main axis (e.g., horizontally for a [Row] or vertically for a [Column])

But, remember, unlike Expanded, The Flexible widget does not require the child to fill available space. So, it's always better to use Flexible.

Column(
  children: <Widget>[
    Flexible( 
      child: ListView(...),
    ),
  ],
)

2. Using shrinkWrap: true

Using shrinkWrap as true, we tell the Listview to render its List to available space, not take below remaining screen.

Column(
  children: <Widget>[
    ListView(
      shrinkWrap: true, 
    ),
  ],
)

Also, physics attribute can be used to allow/disallow scrolling of Listview

Stop Scrolling

 ListView(
      shrinkWrap: true, 
      physics: NeverScrollableScrollPhysics(),
    ),

Allow Scrolling

 ListView(
      shrinkWrap: true, 
      physics: ScrollPhysics(),
    ),

 
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
4

I faced this problem because I wanted to display a GridView inside a ListView, and the solution was by adding the following not only in the ListView but also in the GridView as well:

  1. shrinkwrap: true
  2. scrollDirection: Axis.vertical
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Welcome to SO. Though we thank you for your answer, it would be better if it provided additional value on top of the other answers. In this case, your answer does not provide additional value, since another user already posted that solution. If a previous answer was helpful to you, you should [vote it up](https://stackoverflow.com/help/privileges/vote-up) once you have enough [reputation](https://stackoverflow.com/help/whats-reputation) – lepsch Jun 18 '22 at 08:42
  • 1
    I see, but all answers for adding those steps just in list view, i was already tried it and the error was the same without any change. After adding those not only in listview, but also in gridview it was working. i just saying what happened to me – Marim mohamed Jun 19 '22 at 11:59
  • Got it. I'd edit the answer then to make it a bit clear. Something like: "_I faced this problem cause I wanted to display a `GridView` inside a `ListView`, and the solution was by adding the following not only in the `ListView` but also in the `GridView` as well:_" – lepsch Jun 19 '22 at 14:01
  • This is exactly what I wanted to clarify through my answer in this way, and thank you for your understanding – Marim mohamed Jun 20 '22 at 14:05
4
ListView.builder(
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                itemCount: 10,
                itemBuilder: (BuildContext context, int index) {
                  return Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: Container(
                      height: 100,
                      width: double.infinity,
                      color: Colors.pinkAccent,

                    ),
                  );
                },
              ),
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
3

If you still face this problem in 2021 ..here is the best and easy solution

      ListView.builder(
         scrollDirection: Axis.vertical,
         shrinkWrap: true,
     //     itemExtent: 400, use this if you give hight of your items
        physics: ScrollPhysics(),)

their are many solution .. but they have problem.. like when we use ListView and use its property shrinkWrap .then one thing you many notice the scroll does not work on listView so you must use physics: ScrollPhysics()

benten
  • 696
  • 7
  • 18
3

FINALLY SOLVED!

I had a problem where were 2 nested Listview.builders inside a column. The problem that layout calculates dynamically and you cannot set the fix height for the ListView.builder (via SizedBox for example).

To avoid this problem, try:

  1. to change your Column with a widget ListView.
  2. Then to make sure that you take all available space you have to wrap it with the Expanded widget. (or try Padding instead, it works also sometimes, but i don't understand why).
  3. Then set to the ListView the property shrinkWrap to true to limit it from infinity. And inside Listvie.builder set also shrinkWrap to true to limit from infinity even here. And here also you have to add physics: ScrollPhysics() to make it scrollable.

Here a simple example, but it works for more complicated structure of course.

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Column(
            children: [
              Expanded(
                child: ListView(
                  shrinkWrap: true,
                  children: [
                    ListView.builder(
                        shrinkWrap: true,
                        physics: ScrollPhysics(),
                        itemCount: 100,
                        itemBuilder: (context, i) {
                          return Center(
                            child: Text('index is $i'),
                          );
                        })
                  ],
                ),
              ),
              Container(
                height: 400,
                decoration: BoxDecoration(color: Colors.greenAccent),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Here also en example for nested ListView.builder:

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SafeArea(
          child: Column(
            children: [
              Expanded(
                child: ListView(
                  shrinkWrap: true,
                  children: [
                    ListView.builder(
                        shrinkWrap: true,
                        physics: ScrollPhysics(),
                        itemCount: 100,
                        itemBuilder: (context, i) {
                          return Column(
                            children: [
                              Center(
                                child: Text(
                                  'index is $i',
                                  style: TextStyle(fontSize: 24),
                                ),
                              ),
                              ListView.builder(
                                  shrinkWrap: true,
                                  itemCount: 4,
                                  physics: NeverScrollableScrollPhysics(),
                                  itemBuilder: (context, j) {
                                    return Center(
                                      child:
                                          Text('nested ListView index is: $j'),
                                    );
                                  }),
                              Divider(
                                height: 1,
                                thickness: 2,
                                color: Colors.blueGrey,
                              )
                            ],
                          );
                        }),
                  ],
                ),
              ),
              Container(
                height: 400,
                decoration: BoxDecoration(color: Colors.greenAccent),
              )
            ],
          ),
        ),
      ),
    );
  }
}
KiriLL Sabko
  • 113
  • 1
  • 5
2
    Container(
       height: 100,
       child: ListView.builder(
       scrollDirection: Axis.vertical,
       physics: BouncingScrollPhysics(),
       shrinkWrap: true,
       itemCount: 5,
       itemBuilder: (context, index) {
       return Text("Hello World $index");
      },
    ),
  );
1

test this one for second listview

ListView.builder(
            physics: NeverScrollableScrollPhysics(),
Mehrdad
  • 1,477
  • 15
  • 17
1

Just want to add my solution that worked in my case where I have vertical list view together with other buttons and texts on the same screen:

Flexible(
child:  
  ListView.builder(
  // make sure to add the following lines:  
  shrinkWrap: true,
  physics: ScrollPhysics(),
  // the rest of your list view code
 ) // ListView
) // Flexible

This solution works well particularly when you have other widgets on the SAME SCREEN. In this particular case, "Expand" or "Flexible" alone will not work.

us_david
  • 4,431
  • 35
  • 29
0

There is a widget called Center. It should help you achive what you want to do (center it vertically). Official Documentation for Center widget

Martin Kersten
  • 5,127
  • 8
  • 46
  • 77
0

Also meet this error, because the code like this

return Container(
      child: Column(
        children: [
          ListView.separated(
              itemBuilder: (context, index) {
                return CircleManagerSettingItem(widget.membersInfo[index]);
              },
              separatorBuilder: (_, index) => DD(
                    height: 1,
                    paddingLeft: 10,
                    paddingRight: 10,
                  ),
              itemCount: (widget.membersInfo.length)),
        ],
      ),
    );

remove the Column inside the Container then Ok.

chengxcv5
  • 147
  • 1
  • 10
0

in my case, I had to wrap the ListView inside a Container and give this container a specific Height

A.Ktns
  • 381
  • 6
  • 12
0

I have made a custom function to solve your problem. You can directly use this function in your code base:

Widget horizontalListView(height, width, color, child, margin) {
return Column(
  children: [
    SizedBox(
      height: 200,
      child: ListView.builder(
        itemCount: 10,
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        itemBuilder: (context,index) {
          return Container(
            height: height,
            width: width,
            margin: EdgeInsets.all(margin),
            color: color,
            child: child
          );
        }
      ),
    ),
  ],
 );
})

P.S. Sorry for bad alignment of code.

Chirag Kothiya
  • 955
  • 5
  • 12
  • 28
0

You can put Expanded() as a parent of that widget too.

Deven
  • 684
  • 7
  • 10
0

If you have a ListView inside SingleChildScrollView, just set shrinkWrap: True and physics: const NeverScrollableScrollPhysics() in the ListView.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 22 '23 at 18:16
0

This worked for me and kept the ListView scrollabe

return ListView(
shrinkWrap: true, //<-
physics: ClampingScrollPhysics(), //<-
children: <Widget>[
        ListTile(
            title: Text("Exp1")),
 ListTile(
            title: Text("Exp2"))]);
L3xpert
  • 1,109
  • 1
  • 10
  • 19
-1

if you are using CustomScrollView

you can try SliverFillRemaining

TuGordoBello
  • 4,350
  • 9
  • 52
  • 78
Furkan Cetintas
  • 600
  • 5
  • 13
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Oct 25 '22 at 08:53