How to constraint the width of a GridView widget in flutter - 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))

Related

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

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

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

Flutter web scrollview with mouse outside of listview

I have a GridView that I want to make scrollable even when the mouse is outside of the GridView. I have tried to add the GridView inside a SingleChildScrollView and added shrinkWrap: true to the GridView. I have also tried adding a Column around the GridView and Expanded as well. Here is the code:
return SingleChildScrollView(
child: Column(
children: [
Container(
constraints: BoxConstraints(maxWidth: 750),
child: GridView.builder(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
padding:
EdgeInsets.symmetric(horizontal: 30, vertical: 10),
itemCount: course.subjects.length,
itemBuilder: (context, idx) {
return CourseSubjectListEl(course, idx, data);
},
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisSpacing: 10,
childAspectRatio: 0.7,
mainAxisSpacing: 0,
crossAxisCount: 2),
),
),
],
),
);
As you can see on the screenshot, you can only scroll when the mouse is over the GridView. I think this problem can cause confusion for web users, where you normally can scroll with the mouse everywhere.
You can add
primary: false,
Property inside GridView. That will make the GridView scroll physics false and will act on how the parent scrolls (which in case is your SingleChildScrollView).
Probably you might want to remove physics: NeverScrollableScrollPhysics(), and add the above property.
Note: Make sure your scrollview contains the whole width of the screen you want to scroll

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