how to render gridviewbuilder's last element in the middle - flutter

i am using gridview.builder to list these icons with crossaxiscount of 3 i want to display the icons of 2nd row in the middle(add spacing before tv icon so both icon shift to the middle)
GridView.builder(
itemCount: state.productList.length,
shrinkWrap: true,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 5,
childAspectRatio: 2),
itemBuilder: (context, i) {
return Container(
child: FlatButton(
child: Image.asset(_icons[i]),
onPressed: () {
_productListBloc.add(
ItemTapProductListEvent(
state.productList[i]));
},
),
);
}),

use flutter_staggered_grid_view for customised grid layout.

Related

Flutter: Using SliverChildBuilderDelegate in a GridView.custom, Get Widget Constraints for Draggable

I am attempting to create a custom grid view, and i need to put constraints on the Draggable feedback or it expands to cover the entire screen. Normally in builders, there is a constraints variable passed, but there is not one in the SliverChildBuilderDelegate. What would be the best way to limit the size of the feedback of the draggable to the current size of the widget? The ?'s in feedback is where the variable should go.
return GridView.custom(
shrinkWrap: true,
primary: false,
scrollDirection: Axis.vertical,
controller: _scrollController,
physics: const ClampingScrollPhysics(),
gridDelegate: SliverQuiltedGridDelegate(
crossAxisCount: 4,
mainAxisSpacing: 10,
crossAxisSpacing: 10,
repeatPattern: QuiltedGridRepeatPattern.inverted,
pattern: widget.pattern,
),
childrenDelegate: SliverChildBuilderDelegate(
childCount: widget.children.length,
(context, index) {
var selectedWidget = widget.children[index];
return LongPressDraggable<GlobalKey>(
data: selectedWidget.key,
onDragStarted: _onDragStarted,
onDragEnd: _onDragEnd,
feedback:
SizedBox(width: ?, height:?, child: selectedWidget),
childWhenDragging: Container(),
child: selectedWidget,
)}))

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,)

Flutter GridView.builder align item after filter

How align item on top (bottom search field) after filter data in GridView.builder?
Below is my example:
Column(
children: [
Padding(
padding: EdgeInsets.all(10),
child: GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
itemCount: widget.emps.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount:
MediaQuery.of(context).orientation == Orientation.portrait ? 2 : 4,
mainAxisSpacing: 20,
crossAxisSpacing: 20,
childAspectRatio: 0.693,
),
itemBuilder: (context, int index) {
return widget.emps[index].ename.contains(searchString) ? EmpCard(
emp: widget.emps[index],
press: () {
},
):Container();
}
there is no issue in alignment, issue is you are displaying container() instead of EmpCard() when you are not find a match,
you have to first create list for search outside of GridView.builder() and that has to be displayed. then alignment issue will be automatically solved.
update-
take new list out side build method;
List search=[];
//-----
onUpdate(value){
searchString=value;
search=[];
for(int i=0;i<widget.emp.lenght;i++)
{
if(<--Your search condition-->){
search.add(<--add your widgets here-->)}
}
}
then create your GridView.builder() with this search list istead of widget.emps.