how to use ResponsiveGridView.builder() in responsive_framework in flutter? - flutter

how to use gridDeligate inside ResponsiveGridView.builder() in responsive_framework package.
padding: EdgeInsets.symmetric(
horizontal: ResponsiveValue(
context,
defaultValue: 10,
valueWhen: [
Condition.largerThan(name: MOBILE, value: 20),
Condition.largerThan(name: TABLET, value: 85),
],
).value!.toDouble(),
vertical: 0),
shrinkWrap: true,
itemCount: 8,
gridDelegate: ResponsiveGridDelegate(
crossAxisSpacing: 50,
mainAxisSpacing: 50,
),
itemBuilder: (context, index) => Container(
child: NewsCardWidget(),
),
)),

You need to use it with a ResponsiveGridView.
ResponsiveGridView.builder(
itemCount: controller.tools.length,
padding: EdgeInsets.all(8.0),
shrinkWrap: true,
gridDelegate: ResponsiveGridDelegate(
crossAxisSpacing: 50,
mainAxisSpacing: 50,
minCrossAxisExtent: 150),
itemBuilder: (BuildContext context, int index) =>
ToolCard(controller.tools[index]),
)

Related

Flutter how to set a column that wrap gridview height

i trying to make the height of column into dynamically because the more product i have i need more height for the column, down there was my code
Container(
width: double.infinity,
height: 1000,
child: GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: productsList.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 22 / 30,
mainAxisSpacing: 5,
crossAxisSpacing: 3),
itemBuilder: (ctx, i) {
return ChangeNotifierProvider.value(
value: productsList[i],
child: FeedsProduct(),
);
},
),
),
how can i set the container height dynamically?
There is a property inside GridView called : shrinkWrap, see shrinkWrap property
Here you can find simple example
class SimpleExample extends StatelessWidget {
const SimpleExample({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
child: SingleChildScrollView(
child: Column(
children: [
GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: 30,
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 22 / 30,
mainAxisSpacing: 5,
crossAxisSpacing: 3),
itemBuilder: (ctx, i) {
return Container(
color: Colors.grey,
child: Center(
child: Text("Item $i"),
),
);
},
),
],
),
),
);
}
}
I don't know how your code works exactly but you can wrap the Expanded() widget on GridView() instead of a container:
Expanded(
child: GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: productsList.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 22 / 30,
mainAxisSpacing: 5,
crossAxisSpacing: 3),
itemBuilder: (ctx, i) {
return ChangeNotifierProvider.value(
value: productsList[i],
child: FeedsProduct(),
);
},
),
);

Flutter Listview Builder Without Specific Height

How to make flutter ListView.builder without a specific height(dynamic or not declare the height)
I want to make a list of item, and then when the item going to full height it will wrapped
When i run this code, it returns error because it need height and width
I want to make the list of item like this
Wrap(
spacing: 12,
runSpacing: 10,
children: [
ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: movieDetail['genre'].length,
itemBuilder: (context, index) {
return ClipRRect(
borderRadius: BorderRadius.circular(8),
child: Container(
color: darkGrayColor,
child: Padding(
padding: EdgeInsets.symmetric(
vertical: 4, horizontal: 12),
child: Text('Testing', style: secondaryInfoText),
),
),
);
},
),
],
),
Use GridView.builder
GridView.builder(
scrollDirection: Axis.horizontal,
itemCount: movieDetail['genre'].length,
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: 1),
itemBuilder: (context, index) {
...
}

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

Flutter Horizontal gridview with dynamic width

I have tried using Flutter_staggered_grid_view, but it seems as though that its built better for vertical scrolling.
My goal is to have a horizontal gridview with dynamic widths to make the grid feel natural and not so spread apart
This is what I have (I removed some of the UI code, but its essentially the same)
GridView.builder(
scrollDirection: Axis.horizontal,
itemCount: sourceList.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2, childAspectRatio: .3),
itemBuilder: (context, i) {
final e = sourceList[i];
return Text(
e.name,
);
},
);
And this is what I am looking for
This is the max I could achieve by using ternary operator to adjust mainAxisCellCount. You can adjust the the count according to the length of you shortest and longest string.
Container(
height: 180.0,
child: StaggeredGridView.countBuilder(
crossAxisCount: 4,
itemCount: 8,
scrollDirection: Axis.horizontal,
itemBuilder: (BuildContext context, int index) => Container(
child: Center(child: Container(
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 15.0),
child: Chip(
label: Text('${fruits[index]}', overflow: TextOverflow.visible, maxLines: 1, style: TextStyle(),)))),
),
staggeredTileBuilder: (int index) => StaggeredTile.count(2, fruits[index].length > 3 ? fruits[index].length > 8 ? fruits[index].length > 2 ? 4 : 3 : 2 : 1),
mainAxisSpacing: 0.0,
crossAxisSpacing: 0.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(),
),
),
),