user16304169
user16304169

Reputation:

flutter I'm using gridview.count, but I can't set the height

I am using GridView.count and list.generate.

I want to set the height on the image.

I tried setting the height where needed, but there is no change

It seems my code is stupid.

It's a low level question, but I would appreciate it if you could answer it :)

SingleChildScrollView(
                      scrollDirection: Axis.vertical,
                      child: Container(
                        height: MediaQuery.of(context).size.height,
                        child: GridView.count(
                          mainAxisSpacing: 5.0,
                          crossAxisSpacing: 5.0,
                          crossAxisCount: 3,
                          children: List.generate(
                            snapshot.data!.length,
                            (index) => ClipRRect(
                              borderRadius: BorderRadius.circular(15.0),
                              child: snapshot.data![index].posterPath.isEmpty
                                  ? Container()
                                  : CachedNetworkImage(
                                      fit: BoxFit.cover,
                                      imageUrl:
                                          'https://image.tmdb.org/t/p/original/${snapshot.data![index].posterPath}',
                                    ),
                            ),
                          ),
                        ),
                      ),
                    );

Upvotes: 2

Views: 457

Answers (2)

Mr N
Mr N

Reputation: 25

Using

https://pub.dev/packages/flutter_staggered_grid_view

It worked for me

Ex:

StaggeredGrid.count(
                crossAxisSpacing: 4.w,
                mainAxisSpacing: 4.h,
                crossAxisCount: 4,
                children: [
                  _itemBusiness(getIconAssets('ic_gr_1.svg'), 'import_purchase'.tr, onTap: () {
                    Get.toNamed(Routes.businessImportWarehouse);
                  }),
                  _itemBusiness(getIconAssets('ic_gr_5.svg'), 'import_other'.tr, onTap: () {
                    Get.toNamed(Routes.businessExportWarehouse);
                  }),
                ],
              )

Upvotes: 0

lenz
lenz

Reputation: 2425

Try using childAspectRatio for the size of the children of GridView.

It is a ratio of width / height.

// As tall as it is wide
childAspectRatio:1

// 3 parts width for 2 parts height (1.5 times wider than it is tall) 
childAspectRatio:3/2

// 1 part width for 2 parts height (2 times taller than it is wide) 
childAspectRatio: 1/2

Upvotes: 2

Related Questions