78

I have a problem with gridview and column. In this case, i want put an image in upper of gridview. Please give me a solution..

return new Container(
  child: new Column(
    children: <Widget>[
      new Container(
        child: new Image.asset(
          "assets/promo.png",
          fit: BoxFit.cover,
        ),
      ),
      new Container(
        child: new GridView.count(
          crossAxisCount: 2,
          childAspectRatio: (itemWidth / itemHeight),
          controller: new ScrollController(keepScrollOffset: false),
          shrinkWrap: true,
          scrollDirection: Axis.vertical,
          children: new List<Widget>.generate(16, (index) {
            return new GridTile(
                header: new Container(
                  padding: const EdgeInsets.all(10.0),
                  alignment: Alignment.centerRight,
                  child: new Icon(
                    Icons.shopping_cart,
                    size: 20.0,
                    color: Colors.red,
                  ),
                ),
                child: new MyList(
                  nomor: '$index',
                ));
          }),
        ),
      ),
    ],
  ),
);

and this is the result: Flutter Gridview in Column

10 Answers 10

156

You just need to put your grid view into Expanded widget, for example:

body: new Column(
  children: <Widget>[
    new Expanded(
      child: GridView.count(
        // Create a grid with 2 columns. If you change the scrollDirection to
        // horizontal, this would produce 2 rows.
        crossAxisCount: 2,
        // Generate 100 Widgets that display their index in the List
        children: List.generate(10, (index) {
          return _buildCard(index);
        }),
      ),
    ),
    new Text("text")
  ],
),
Sign up to request clarification or add additional context in comments.

1 Comment

This only works when the GridView is the first item of Column. To make the GridView fit into any where in the column, remove the Expanded wrapper and add shrinkWrap: true property in Gridview.count
139

Reason for the error:

Column expands to the maximum size in main axis direction (vertical axis), and so does the GridView (scroll direction is vertical by default)

Solution

You need to constrain the height of the GridView, so that it expands to fill the remaining space inside Column, there are several ways of solving this issue, use whichever suits you better.


  1. If you want to allow GridView to take up all remaining space inside Column use Flexible.

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

  1. If you want to limit your GridView to certain height, you can use SizedBox.

    Column(
      children: <Widget>[
        SizedBox(
          height: 200, // constrain height
          child: GridView(...),
        )
      ],
    )
    

  1. If your GridView is small, you may try shrinkWrap property on it.

    Column(
      children: <Widget>[
        GridView(
          shrinkWrap: true, // use it
        )
      ],
    )
    

7 Comments

Great answer. Covers more cases than the currently accepted one.
I have put Gridview inside Expanded widget but it is still not working!!
@VinothVino That's not possible as of today, you need to use third party library, like staggered_grid_view.
upvote, The reason understanding the problem is much more important than just final solution
I also want to mention that I think the answer could be improved, as it doesn't explain the difference between using a Flexible and shrinkWrap: true. I would love if it would be edited to include some more information in regards of this.
|
48

If Column is the parent of GridView then it will give rendering issue as It happens because Column and GridView both take the entire space of the screen individually which is there default behavior(Default axis of scrolling is Vertical).

Solution:

To solve the above problem we have to disable scrolling of GridView, This can be possible by shrinkWrap and physics property

shrinkWrap:true - With this GridView only occupies the space it needs

physics: NeverScrollableScrollPhysics() - It disables scrolling functionality of GridView, which means now we have only SingleChildScrollView who provide the scrolling functionality.

Code:

SingleChildScrollView
   Column
        GridView(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                //...
                )

Comments

18

The answer mentioned by @RuslanLeshchenko is correct but if it is still not working for you, trying the following properties on GridView

  1. Try changing shrinkwrap to true
  2. Apply physics: BouncingScrollPhysics()

1 Comment

Worked for me. I set this property to my GridView. I have Column and first two children are two image and 3rd one is GridView with 10 items. So the complete Column height is more than the Device height. When we scroll the grid i could not see all items. Thanks,
10

You can solve it by these two properties and change them as follows :

shrinkWrap: true,
primary: false,
  • shrinkWrap will wrap your and will handle overflow.
  • Through primary we can set either you want to make this list scrollable or it's parent list. By setting it to false it allows to take scroll of parent list or scrollable view.

NOTE : You can also wrap your column with SingleChildScrollView to have scroll in your view.

Comments

2

Just wrap with scaffold and scrollview

Scaffold(
  resizeToAvoidBottomInset: true,
  body: SingleChildScrollView(
    child: Column(
            children: [ 
          ...

Tested working on dialog model aswell.

Comments

0

I had the same problem and couldn't fix. Then again I used a GridView.builder().

I changed it to GridView.count and added shrinkWrap: true and physics: NeverScrollableScrollPhysics() as mentioned in the other comments.

It works fine now. Thanks!

Comments

-2

Try changing childAspectRatio property of GridView to 0.8 or lower.

Comments

-2
body:Container(
   body: Container(
          child: Column(
            children: [
              Text('Exemple'),
              Expanded(
                child: GridView.count(
                    children: <Widget>[
                          //Widgets
                    ]
               )
             )
           ]
         )
       ) 

Comments

-2

Just wrap GridView inside ListView

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

Also it works with SingleChildScrollView

SingleChildScrollView(
    child: Column(
        children: <Widget>[
            ListView(
                shrinkWrap: true,
                physics: NeverScrollableScrollPhysics(),
                children: [
                    GridView(
                        shrinkWrap: true,
                        physics: NeverScrollableScrollPhysics(),
                    ),
                ],
            ),
    )
)

Works like a charm (:

Comments

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.