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
The answer mentioned by @RuslanLeshchenko is correct but if it is still not working for you, trying the following properties on GridView
shrinkwrap
to truephysics: BouncingScrollPhysics()
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(),
//...
)
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")
],
),
Try changing childAspectRatio
property of GridView to 0.8 or lower.
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.
If you want to allow GridView
to take up all remaining space inside Column
use Flexible
.
Column(
children: <Widget>[
Flexible(
child: GridView(...),
)
],
)
If you want to limit your GridView
to certain height
, you can use SizedBox
.
Column(
children: <Widget>[
SizedBox(
height: 200, // constrain height
child: GridView(...),
)
],
)
If your GridView
is small, you may try shrinkWrap
property on it.
Column(
children: <Widget>[
GridView(
shrinkWrap: true, // use it
)
],
)