Flutter how to remove extra spacing from GridView.builder element? - flutter

I have a GridView.builder that build me a grid with elements inside, how can I remove the distance from the bottom of my grid element so that there is no overflow on all devices, I use the screenutil package.
Here is my griddelegate code:
Widget buildProduct(List<Product> product) => GridView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 20.w / 33.h,
crossAxisSpacing: 10.w,
mainAxisSpacing: 10.h,
),
itemCount: product.length,
itemBuilder: (context, index) {...}
);

need more code to solve the problem but maybe you can try to wrap the Gridview with Expanded and remove shrinkWrap

Related

Flutter: Add custom widget periodically to GridView that doesn't fit its specifications

I want to have a GridView in Flutter with a crossAxisCount of to but periodically show a widget that takes up the whole width (an advertisement banner). How can I do that with a GridView?
Here is my code where I want to incorporate ad banners for every 10th row or so:
GridView.count(
childAspectRatio: 1 / 1.05,
controller: _scrollController,
scrollDirection: Axis.vertical,
crossAxisCount: 2,
crossAxisSpacing: 0,
mainAxisSpacing: 5.0,
physics: const AlwaysScrollableScrollPhysics(),
children: widgetList.map((value) {
return value;
}).toList(),
)
You can use Wrap instead of GridView.

Flutter GridView off screen widgets destroyed on scroll

I have a screen with a GridView widget containing custom Cards with images and texts. When the app initially launches it loads all the cards and their images, however, they get destroyed when scrolled off the screen. Is there a way that I can stop flutter from destroying the off-screen widgets?
Expanded(
child: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1.5,
),
itemCount: categories.length,
itemBuilder: (context, index) => CategoryCard(
category: categories[index],
),
),
),
Give flex or shrinkWrap: true, or use both
Expanded(
flex: 3, <--
child: GridView.builder(
shrinkWrap: true, <--
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1.5,
),
itemCount: categories.length,
itemBuilder: (context, index) => CategoryCard(
category: categories[index],
),
),
),
Figured it out! GridView, ListView, etc., have addAutomaticKeepAlives and cacheExtent properties. By default addAutomaticKeepAlives: true doesn't destroy off-screen widgets, and providing the cacheExtent: DOUBLE VALUE describes how many pixels the cache area extends before the leading edge and after the trailing edge of the viewport.

How to reduce Gridview.builder default height

I have used GridView.builder to show API data with below code.
GridView.builder(
shrinkWrap: true,
physics: ScrollPhysics(),
itemCount: snapshot.data!.items.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 1 / .3,
),
itemBuilder: (context, index) {
return InkWell(
onTap: () {},
child: CategoryItem(categoryItem: snapshot.data!.items[index],
),
);
},
),
Here Now my data length is 4 only. when it increases then data will be scrollable. And that working fine But thing is, I want to get resizable height with data length. It takes a default height which is not compatible.
Is there any way to reduce default height or customize it?
Thanks in advance.
Getting below output :

Why gridview reload images in Flutter?

I'm trying to use show images using by Image.file(File(path)) in gridview. When I scrolling it down and then up it's reloading images. This caused showing them in white. When I'm looking for this issue there is mentions "lazy loading list" but I don't think it's related with this. Also I tried caching Image widget with LruCache but it's still same. How can I prevent this reloading?
Here is my code snippet:
child: GridView.builder(
itemCount: paths.length,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: 0.5,
crossAxisSpacing: 6,
mainAxisSpacing: 6,
crossAxisCount: 5,
),
itemBuilder: (BuildContext context, int index) {
return GestureDetector(
onTap: () => {remove(paths[index])},
child: Image.file(File(paths[index])),
);
},
),
This is an error: stop reason = EXC_RESOURCE RESOURCE_TYPE_MEMORY flutter
GridView(gridDelegate: gridDelegate, cacheExtent: 9999,)

is their any way we can provide specific height (aspect ratio provides error)to grid item in flutter using GridView.builder

Since i am new to flutter, In the case of gridview.builder, By providing "childAspectRatio: 1/1", the height differs, some devices i am getting overflow by 10pixels etc, is their any way we can provide specific height to grid item.
GridView.builder(
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 0.0,
childAspectRatio: 1 / 1),
itemBuilder: (BuildContext context, int index) {
Map<String, String> product = products[index].cast<String, String>();
return _buildGridItems(index, product);
},
itemCount: products.length,
shrinkWrap: true,
physics: ClampingScrollPhysics(),
)
Only workaround for this problem that i have found is to use expanded widget and use the flex property to size the widgets (if you have column as the child).