Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter GridView.count in Row Layout Error

Tags:

i'm new in flutter and i want to draw a image grid in a row. i used this example Flutter - Layout a Grid but it show me every time a error

Widget buildView(){
    return new Container(
      color: Colors.white,
      child:
      new Padding(
        padding: EdgeInsets.all(8.0),
        child: new Column(
          children: <Widget>[
            new Row(
              children: <Widget>[
                Flexible(
                  child: new GridView.count(
                      crossAxisCount: 4,
                      childAspectRatio: 1.0,
                      padding: const EdgeInsets.all(4.0),
                      mainAxisSpacing: 4.0,
                      crossAxisSpacing: 4.0,
                      children: <String>[
                        'https://via.placeholder.com/150',
                        'https://via.placeholder.com/150',
                        'https://via.placeholder.com/150',
                        'https://via.placeholder.com/150',
                        'https://via.placeholder.com/150',
                        'https://via.placeholder.com/150',

                      ].map((String url) {
                        return new GridTile(
                            child: new Image.network(url, fit: BoxFit.cover));
                      }).toList()),
                ),
              ],
            ),
              ],
            ),
          ],
        ),
      ),
    );
  }

i becode every time this error "Vertical viewport was given unbounded height"

like image 434
Pla558 Avatar asked May 28 '19 23:05

Pla558


People also ask

What is SliverGridDelegateWithFixedCrossAxisCount?

SliverGridDelegateWithFixedCrossAxisCount class Null safety. Creates grid layouts with a fixed number of tiles in the cross axis. For example, if the grid is vertical, this delegate will create a layout with a fixed number of columns.

How do you calculate Childaspectratio in flutter?

child aspect ratio is basically width/height of the grid. So let's say you want the width of each grid to be 30 and the height to be 20, you would set the aspect ratio to be 3/2.

How do you get rid of extra space in container in flutter?

Set it like this => padding: EdgeInsets. zero. This will eliminate all the unwanted spaces around your gridview. Actually, this is the correct answer among all the other answers.

How do I stop GridView scrolling in flutter?

You can provide physics: NeverScrollableScrollPhysics() on GridView to disable scroll effect. If you want scrollable as secondary widget use primary: false, To have Full Page scrollable, you can use body:SingleChildScrollView(..) or better using body:CustomScrollView(..) Save this answer.


1 Answers

You don't need to wrap GridView in a Row unless you are trying to divide the row. You can just use GridView.

new GridView.count(
          crossAxisCount: 4,
          childAspectRatio: 1.0,
          padding: const EdgeInsets.all(4.0),
          mainAxisSpacing: 4.0,
          crossAxisSpacing: 4.0,
          children: <String>[
            'https://via.placeholder.com/150',
            'https://via.placeholder.com/150',
            'https://via.placeholder.com/150',
            'https://via.placeholder.com/150',
            'https://via.placeholder.com/150',
            'https://via.placeholder.com/150',

          ].map((String url) {
            return new GridTile(
                child: new Image.network(url, fit: BoxFit.cover));
          }).toList()),

If you really want to wrap it inside a row you need wrap GridView with a container and define a width or wrap in a flexible

Row(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          Flexible(
            child: new GridView.count(
                crossAxisCount: 4,
                childAspectRatio: 1.0,
                padding: const EdgeInsets.all(4.0),
                mainAxisSpacing: 4.0,
                crossAxisSpacing: 4.0,
                children: <String>[
                  'https://via.placeholder.com/150',
                  'https://via.placeholder.com/150',
                  'https://via.placeholder.com/150',
                  'https://via.placeholder.com/150',
                  'https://via.placeholder.com/150',
                  'https://via.placeholder.com/150',    

                ].map((String url) {
                  return new GridTile(
                      child: new Image.network(url, fit: BoxFit.cover));
                }).toList()),
          ),
        ],
      ),

If you get unbounded height error just wrap it with a container or sizedbox and define a height

like image 70
Praneeth Dhanushka Fernando Avatar answered Oct 19 '22 07:10

Praneeth Dhanushka Fernando