How to reduce the with of GridViewItems - flutter

Example:
I want to reduce the width of this picture with childAspectRatio without knowing the values. The image above shows my layout. Is there a way to reduce the with of the GridViewItems, when I am not having the exact values of for using the child-aspect ratio properly?
Here is my code:
Widget build(BuildContext context) {
final width = MediaQuery.of(context).size.width;
final height = MediaQuery.of(context).size.height;
logger("width => $width");
logger("height => $height");
return GridView.count(
childAspectRatio: .9/.2,
// childAspectRatio: 6,
// padding:EdgeInsets.only(right: 250),
mainAxisSpacing: 10,
crossAxisSpacing: 25,
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
crossAxisCount: width < 1510
? 2
: width < 1537
? 3
: 1,
children: <Widget>[
SelectCargoDropDown(),
CargoCampaignDropDown(),
FreeCargoNumberField(),
IdSelectionDropDown(),
AuthSellingDropDown(),
PharmacyReferenceDropDown(),
InformationPersonReferenceDropDown(),
],);

I think it's useful to insert your GridView.count in an Expanded widget.
Then it will automatically take the suitable width for the screen, even it's the same widget in the screen's width or there is another.

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.

GridView without the scrolling?

I need a simple grid of widgets but without scrolling, similar to how I coded GridView below.
GridView.count(
padding: const EdgeInsets.all(20.0),
crossAxisCount: 2,
crossAxisSpacing: 20,
shrinkWrap: true,
// mainAxisSpacing: 20,
children: [
_TextField0(focusNode: focusNode),
_TextField1(focusNode: focusNode),
_TextField2(focusNode: focusNode),
_TextField3(focusNode: focusNode),
],
),
)
Is there a way to disable scrolling in GridView or is there another widget I should be looking at?
Yes, GridView (or ListView) has a parameter for scroll behavior. In your case, simply pass in:
physics: const NeverScrollableScrollPhysics()

RenderFlex overflow using Flutter GridView

I used a GridView.count() to display dynamic data in my app, but each of my GridView elements doesn't fully display.
Here is what it looks like :
Here is my code :
Column(
children: [
buildSubheadingText('Mes projets'),
buildVerticalSpace(5.0),
GridView.count(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
//physics: NeverScrollableScrollPhysics(),
crossAxisCount: 2,
children: ContextManager.projects.map((value) {
return ProjectCard(project: value);
}).toList()
)
],
);
I have added the attributes shrinkWrap at true and physics at BouncingScrollPhysics or NeverScrollableScrollPhysics as advised in the answers of this topic but as you can see it doesn't work for me.
Also, I don't want to use a fixed height because data is dynamic here.
Thanks for helping.
You can set custom height for grid using childAspectRatio attribute
Example:-
Column(
children: [
buildSubheadingText('Mes projets'),
buildVerticalSpace(5.0),
GridView.count(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
childAspectRatio: 2/3, //gives custom height to your grid element
crossAxisCount: 2,
children: ContextManager.projects.map((value) {
return ProjectCard(project: value);
}).toList()
)
],
);
GridView children's size depends on childAspectRatio, default it is 1.0, you can increase height by changing default childAspectRatio. like
child: GridView.count(
crossAxisCount: 2,
childAspectRatio: 1 / 1.2, //width /height
Please try using this library,
staggered_grid_view

How to constraint the width of a GridView widget in flutter

I've been trying for two days to add a fixed width to a Grid View in flutter but I couldn't.
I tried adding width or Box Constraint to a parent container but it didn't work.
I am building a web app so the screen width is often wider than a smartphone's.
this is how I want it to look like:
fixed width for GridView in flutter example
Objective:
constraint the width of the GridView Widget to have a max width of 1000 as an example.
Edit:
if you face any issue with centering the parent container just rap it with a Center widget and that should work
Here you can use crossAxisCount property to add no of items in row. Then give the fixed to its child by wrapping it in a container.
Container(
width: 100, // HERE YOU CAN ADD THE WIDTH
child: GridView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: list.length,
scrollDirection: Axis.vertical,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
childAspectRatio: 1,
crossAxisCount: 4, // HERE YOU CAN ADD THE NO OF ITEMS PER LINE
mainAxisSpacing: 5,
crossAxisSpacing: 5,
),
itemBuilder: _getListItemTile))

Set height to container in Gridview.builder

I am working with the following code and got struck. I wanted to set the height to the container so that the images which are displayed inside it will be fitted well. In the present condition the image gets fitted inside the container and it does not look good. I have used a grid view to display images from firebase.
class Catview extends StatefulWidget {
#override
_CatviewState createState() => _CatviewState();
}
class _CatviewState extends State<Catview> {
StreamSubscription<QuerySnapshot> subscription;
List<DocumentSnapshot> wallpaperList;
final CollectionReference collectionReference =
Firestore.instance.collection("pubg");
#override
void initState() {
super.initState();
subscription = collectionReference.snapshots().listen((datasnapshot) {
setState(() {
wallpaperList = datasnapshot.documents;
});
});
}
#override
void dispose() {
super.dispose();
subscription?.cancel();
}
#override
Widget build(BuildContext context) {
return (SafeArea(
child: Scaffold(
body: wallpaperList != null
? GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 2,
mainAxisSpacing: 4,
),
itemCount: wallpaperList.length,
itemBuilder: (context, index) {
String image = wallpaperList[index].data['image'];
return Container(
color: Colors.red,
child: Image.network(
image,
),
);
},
)
: Center(
child: CircularProgressIndicator(),
),
),
));
}
}
thank you!
You need to add preperty childAspectRatio in SliverGridDelegateWithFixedCrossAxisCount. childAspectRatio as aspectRatio, because in gridview can't support height dan width. Unless you use this library Staggered Grid View . https://pub.dev/packages/flutter_staggered_grid_view.
this is example if you use gridview without library.
Example:
GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 2,
mainAxisSpacing: 4,
childAspectRatio: 0.7,
),
....
You'll have to use the childAspectRatio property present in the GridView.builder class
GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 8.0,
mainAxisSpacing: 8.0,
childAspectRatio: 2/3,
),
....
I like to use the popular aspect ratios present in photography which are the 2/3 (2:3) or the 3/4 (3:4) to get the kind of look I normally like.
If you want a wider width in relation to the height, use the aspect ratio in reverse (i.e. 3/2 or 4/3) depending on your need.
Hope it helps! :)