6

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"

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

6 Comments

i tried the both methods but the i become the same error
i forgot to say i have this element in a padding element. i think this occurs the error. any idea how i can solve this ?
I think the error is not with this part of code.. Seems its from parent.. Just try my example as the body of a scaffold you'll see its working.. If you could show the parent of this code it'll be useful
just remove column, row and flexible
any other options ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.