GridView grid delegate display grids the same on all devices - flutter

I used gridView.builder. To set the size of the grid, I use MediaQuery, but for some reason the gridView is displayed differently on different devices, using which parameter in gridDelegate: can I fix this?
my gridView code:
Widget buildCatalog(List<Product> product) => CustomScrollView(
controller: sController,
slivers: [
SliverGrid(
gridDelegate: SliverGridDelegateWithMaxCrossAxisExtent(
// mainAxisExtent: ,
maxCrossAxisExtent: 300,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 1.3),
crossAxisSpacing: 10,
mainAxisSpacing: 10),
delegate: SliverChildBuilderDelegate(childCount: product.length,
(BuildContext context, int index) {
return GestureDetector(
onTap: () {},
child: Container(
padding: const EdgeInsets.only(left: 2, right: 2, top: 5),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8)),
child: Column(
children: <Widget>[
InkWell(
child: Container(
margin: const EdgeInsets.only(top: 5),
child: Image.network(),
),
),
const SizedBox(
height: 10,
),
Text(),
// Spacer(),
const SizedBox(height: 8),
Text(),
const SizedBox(height: 5),
buildButton(index, productItem.id),
],
),
),
);
}),
),
]
);
This is my grid on big displays:
and this on small displays:

The solution was flutter_screenutil package

Related

How to solve Flutter bottom overflow error

I have tried to create Listview.builder with help of API(for displaying API product). but i got error in bottom pixel. i tried many ways to solve this problem. but couldn't find anything,
how can i solve this problems by editing code... And what is the main reason for this error
code is here
Container(
height: 650,
child: FutureBuilder(
future: _getProduct(),
builder: (context, snapshot) {
if (snapshot.data == null) {
return const Center(child: Text("Loading"));
} else {
return GridView.builder(
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
itemCount: snapshot.data?.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(right: 10),
child: Container(
width: 130,
height: 485,
decoration: BoxDecoration(
color: Colors.white,
borderRadius:
BorderRadius.circular(10)),
child: Container(
height: 240,
child: Column(
children: [
Padding(
padding: EdgeInsets.all(5),
child: Image.network(
snapshot.data![index].image)),
Padding(
padding: const EdgeInsets.all(10),
child: Row(
mainAxisAlignment:
MainAxisAlignment
.spaceBetween,
children: [
Expanded(
child: Text(
snapshot.data![index].name,
overflow: TextOverflow.fade,
maxLines: 2,
style: const TextStyle(
fontSize: 15,
fontWeight:
FontWeight.w700),
)),
Text(
snapshot.data![index].price)
],
),
)
],
),
),
),
);
},
);
}
}),
)
Issue with this
GridView.builder(
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2),
Needed to add Shrinkwrap & Add the Ratio
shrinkwrap: true,
gridDelegate:
const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 0.8,
),
I am a beginner in flutter,
I changed to your code and working ok,
need a single line to be added to your code...
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: 1 / 1.5,//<- add this one (it will increase height of your card or container)
crossAxisCount: 2),

GridView.count() doesn't scroll - Flutter

I want the Card widgets to scroll horizontally, but it's not working. I've tried and changed GridView to ListView.
Adding physics is not helping either.
Here's the code:
#override
Widget build(BuildContext context) {
SizeConfig().init(context);
return Expanded(
child: Column(
children: [
Padding(
padding: EdgeInsets.symmetric(
vertical: SizeConfig.blockSizeVertical * 2),
child: (Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Names',
style: Theme.of(context).textTheme.headline6,
),
InkWell(
onTap: () {},
child: Row(children: [
Text('show more',
style: Theme.of(context).textTheme.caption),
const Padding(
padding: EdgeInsets.only(top: 2),
child: Icon(
Icons.keyboard_arrow_left_sharp,
size: 20,
),
),
]),
),
],
)),
),
FutureBuilder<DataModel>(
future: InfoAPI('assets/itemsData.json').getDataLocally(context),
builder: (context, snapshot) {
final data = snapshot.data;
final List<String> list = getnames(data);
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
color: Theme.of(context).primaryColor,
),
height: SizeConfig.blockSizeVertical * 20,
// width: double.infinity,
child: GridView.count(
crossAxisCount: 1,
padding: const EdgeInsets.symmetric(
vertical: 8.0, horizontal: 10.0),
scrollDirection: Axis.horizontal,
childAspectRatio: 1,
physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true,
children: List.generate(list.length, (index) {
return Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16)),
//clipBehavior: Clip.antiAlias,
color: Theme.of(context).cardColor,
child: Center(
child: Text(
list[index],
style: Theme.of(context).textTheme.bodyText1,
)));
},
)),
);
})
],
),
);
}```
So, after long research I discovered that the 'horizontal scrolling' is working just fine on mobile, the problem was because I used chrome.
You can refer to [Horizontal listview not scrolling on web but scrolling on mobile] for more information.
Try wrapping your ListView/GridView with a Flexible Widget. Also Youve set physics to NeverScrollableScrollPhysics() so change it to some other scroll physics(like AlwaysScrollableScrollPhysics()) if you want it to be scrollable

GridView in SimpleDialog is not updating

I am trying to implement a search function in GridView, which is on SimpleDialog. When I perform a search, the GridView is not updating. But when I scroll through GridView, It updates.
Below is my code
Widget _showParentCategory() {
return SimpleDialog(
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 10.0),
child: TextField(
onChanged: (value) {
finalList = myProducts
.where((product) => product["name"]
.toString()
.toLowerCase()
.contains(value.toLowerCase()))
.toList();
setState(() {
finalList;
});
},
decoration: const InputDecoration(suffixIcon: Icon(Icons.close)),
),
),
const SizedBox(
height: 20,
),
SizedBox(
height: MediaQuery.of(context).size.height * 0.7,
width: MediaQuery.of(context).size.width,
child: Container(
color: Colors.white,
child: GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
crossAxisSpacing: 5.0,
mainAxisSpacing: 5.0),
itemCount: finalList.length,
itemBuilder: (BuildContext context, index) {
return Column(
children: [
const CircleAvatar(
backgroundColor: Colors.red,
radius: 20.0,
),
Text(finalList[index]["name"])
],
);
},
),
),
)
],
);
}
I tried to print my finalist which is getting updated but it seems setState is not working.
How can I redraw/ refresh GridView?

Staggered Grid View in Flutter - How to give tiles different widths

I've been trying to create the following screen in Flutter:
https://i.imgur.com/meBdNFz.png
So far I've made this with the package "Staggered Grid View":
https://i.imgur.com/mR6pQ3A.png
Sorry for not being able to upload the images..
However, I still can't figure out how to use different widths for the tiles. The first tile needs to fill around 70% of the containers size and the right one the rest.
At the moment I have the following code:
new StaggeredGridView.countBuilder(
padding: EdgeInsets.all(30),
crossAxisCount: 2,
itemCount: 7,
itemBuilder: (BuildContext context, int index) => new Container(
margin: EdgeInsets.all(4),
color: Colors.green,
child: new Center(
child: new CircleAvatar(
backgroundColor: Colors.white,
child: new Text('$index'),
),
)),
staggeredTileBuilder: (int index) => (index == 0)
? new StaggeredTile.count(2, 1)
: (index % 2 == 0)
? new StaggeredTile.count(1, 0.8)
: new StaggeredTile.count(1, 0.8),
mainAxisSpacing: 4.0,
crossAxisSpacing: 4.0,
)
You don't need to have
itemCount: 7
instead, just use
staggeredTiles: [StaggeredTile.extent(width in crossAxisCount, height in units)]
Here's my code. Haven't tested it but it should be close to what you want.
body: StaggeredGridView.count(
crossAxisCount: 10, //this indicates how much elements (or cards) you want to put in a row
crossAxisSpacing: 6.0,
mainAxisSpacing: 6.0,
padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
children: [
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
Card(
child: ListTile(
//your code to customize here,
),
),
],
staggeredTiles: [
StaggeredTile.extent(10, 200), //second parameter is for adjusting the height of the element
StaggeredTile.extent(6, 150),
StaggeredTile.extent(4, 150),
StaggeredTile.extent(3, 150),
StaggeredTile.extent(5, 150),
StaggeredTile.extent(2, 150),
StaggeredTile.extent(6, 150),
StaggeredTile.extent(4, 150),
],
),
I hope my answer satisfies you :)

Setting width and height to child of GridView not working

I am trying to set a custom width and height to the children of a GridView. I tried the following, but it's not working:
GridView.count(
shrinkWrap: true,
crossAxisCount: 5,
children: coolArray.map((String currentItem) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 5),
width: 17,
height: 10,
color: Colors.red,
child: Text('currentItem'),
);
}).toList(),
)
The items have their own height which I don't know how they're calculated.
You can use childAspectRatio, example
GridView.count(
shrinkWrap: true,
crossAxisCount: 5,
childAspectRatio:width/height,
children: coolArray.map((String currentItem) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 5),
color: Colors.red,
child: Text('currentItem'),
);
}).toList());