How to solve the grid view problem in flutter app? - flutter

I have a problem of grid view. I add lists as described in 1st screenshot, then the same list will be updated on the another page (as per second screenshot). However, as you can see overflow error, that is happening and I can not solve that. So, can you guys explain how to increase it's height as I add element in the lists. Hope, it is clear.
1st image is here
2nd image is here
Here is a Code:
Consumer<HomeProvider>(
builder: (context, provider, child) => GridView.builder(
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: provider.allData.length,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: provider.isPress ? 2 : 1,
childAspectRatio: 1.4
),
itemBuilder: ((context, index) {
return provider.allData[index];
}))

you can use flutter_staggered_grid_view # package
like this,
StaggeredGrid.count(
crossAxisCount: 2,
crossAxisSpacing: 10,
mainAxisSpacing: 12,
children: [ 'your data' ]
);

GridView crossAxisCount is not having enough room on gridItem. You can change crossAxisCount by increasing height.
childAspectRatio: width/height
Decrease childAspectRatio value like .6 or less

const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 20,
mainAxisSpacing: 10,
mainAxisExtent:
110,
)
Try setting mainAxisextent to something more and it will increase the hieght of that current grid element

Just use flutter_staggered_grid_view: ^0.6.2
StaggeredGrid.count(
crossAxisCount: 4,
mainAxisSpacing: 4,
crossAxisSpacing: 4,
children: const [
StaggeredGridTile.count(
crossAxisCellCount: 2,
mainAxisCellCount: 2,
child: Tile(index: 0),
),
StaggeredGridTile.count(
crossAxisCellCount: 2,
mainAxisCellCount: 1,
child: Tile(index: 1),
),
StaggeredGridTile.count(
crossAxisCellCount: 1,
mainAxisCellCount: 1,
child: Tile(index: 2),
),
StaggeredGridTile.count(
crossAxisCellCount: 1,
mainAxisCellCount: 1,
child: Tile(index: 3),
),
StaggeredGridTile.count(
crossAxisCellCount: 4,
mainAxisCellCount: 2,
child: Tile(index: 4),
),
],
);

Related

How to layout the grid cells from Left to Right (Top to Bottom) with horizontal scroll using GridView.builder, in Flutter

I have set up a GridView.builder to scroll horizontally with 2 rows.
However, I want the grid cells to be built out from Left to Right, Top to Bottom (e.g. the top row would build 1,2,3,4 then the remaining 5,6,7 on the second row.).
I tried a Wrap widget but that doesn't have a scroll function and can't seem to get this working.
final times = [1, 2, 3, 4, 5, 6, 7];
...
SizedBox(
height: size.height * 0.25,
child: GridView.builder(
scrollDirection: Axis.horizontal,
itemCount: times.length,
itemBuilder: (context, index) => Container(
color: Colors.blue,
child: Center(child: Text(times.elementAt(index).toString())),
),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2, crossAxisSpacing: 6, mainAxisSpacing: 6),
),
),
Change scrollDirection to scrollDirection: Axis.vertical, and update crossAxisCount: 4.
Updated code :
SizedBox(
height: MediaQuery.of(context).size.height / 2,
child: GridView.builder(
scrollDirection: Axis.vertical,
itemCount: 8,
itemBuilder: (context, index) => Container(
color: Colors.blue,
child: SizedBox(child: Center(child: Text("Hey $index"))),
),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 4, crossAxisSpacing: 6, mainAxisSpacing: 6),
),
),
Result will be -

GridView.count cross-axis spacing too big

I'm trying to create columns of widgets. So, something like
Hello0 Hello1
Hello2 Hello3
When I try
GridView.count(
padding: const EdgeInsets.all(10.0),
crossAxisCount: 2,
crossAxisSpacing: 20,
physics: const NeverScrollableScrollPhysics(),
// mainAxisSpacing: 20,
children: const [
Text('Hello0'),
Text('Hello1'),
Text('Hello2'),
Text('Hello3'),
])
I get the output below
Is there a way to tighten the spacing between the rows or does GridView put everything in a square?
GridView.count calculate its height based on aspect ratio which is 1.0 by default, so its children has extra space.
Instead you can use gridDelegate
GridView(
padding: const EdgeInsets.all(10.0),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 20,
mainAxisExtent: 80,
),
physics: const NeverScrollableScrollPhysics(),
// mainAxisSpacing: 20,
children: const [
Text('Hello1'),
Text('Hello2'),
Text('Hello3'),
])

how to set column count for each row dynamically in flutter

I have gridView builder which is displaying 2 items in a row. like below image
I want to display like this image
\n
Please help me to achieve this in flutter
GridView.builder(
gridDelegate:
SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200,
childAspectRatio: 2.5 / 3,
crossAxisSpacing: 10,
mainAxisSpacing: 10),
shrinkWrap: true,
primary: false,
itemCount: nListLevels.modules.length,
itemBuilder: (context, j) {
// displaying contents here
}
));
I guess you can return "column" and inside the "column", you can add "Row" widget to suit your UI
for different size of grid items, you need flutter_staggered_grid_view:
StaggeredGridView.countBuilder(
crossAxisCount: 4,
itemCount: 8,
itemBuilder: (BuildContext context, int index) => new Container(
color: Colors.green,
child: new Center(
child: new CircleAvatar(
backgroundColor: Colors.white,
child: new Text('$index'),
),
)),
staggeredTileBuilder: (int index) =>
new StaggeredTile.count(2, index.isEven ? 2 : 1),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
)

how to set Container() height which has GridView child?? flutter

i'm trying to make gif pictures gallery.
I wanna wrap gridview with container but container need to setting height.
how can I set Container()'s height relatively??
i wanna make to gridview.items grows, container height grows too.
red line is current container height i set.
Container(
margin: EdgeInsets.only(top: 10),
padding: EdgeInsets.symmetric(horizontal: 10),
height: 1000,
width: double.infinity,
child: GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: 6,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
// childAspectRatio: 3 / 2,
// crossAxisSpacing: 10.0,
// mainAxisSpacing: 10.0,
),
itemBuilder: (context, index) => ChangeNotifierProvider.value(
value: videos[index],
child: VideoGifItem(),
)),
),
If you need your GridView widget to take up space based on the number of items it has.
You can set the shinkWrap property of the GridView widget to true.
I added a demo using your code as an example:
Container(
margin: EdgeInsets.only(top: 10),
padding: EdgeInsets.symmetric(horizontal: 10),
width: double.infinity,
child: GridView.builder(
// set the shrinkWrap property to true
shrinkWrap: true, // new line
physics: NeverScrollableScrollPhysics(),
itemCount: 6,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
// childAspectRatio: 3 / 2,
// crossAxisSpacing: 10.0,
// mainAxisSpacing: 10.0,
),
itemBuilder: (context, index) => ChangeNotifierProvider.value(
value: videos[index],
child: VideoGifItem(),
),
),
),

flutter gridview vertical filling

By default GridView puts items in row until crossAxisCount will not reach
GridView.count(
crossAxisCount: 5,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
padding: EdgeInsets.all(10.0),
children: hours
)
Is there way to change build direction from horizontal to vertical?
Changing the scroll direction should do what you want:
GridView.count(
crossAxisCount: 5,
mainAxisSpacing: 10,
scrollDirection: Axis.vertical,
crossAxisSpacing: 10,
padding: EdgeInsets.all(10.0),
children: hours
)