How do I center items inside a GridView.Builder in Flutter
Asked Answered
C

1

6

I have a GridView.Builder with a crossAxisCount of six it works well when there 12 items but if the items are 11 or less or just uneven it doesn't look so well... 11 Items do not look so nice

this is what it looks now NOT SO NICE with just eleven items

This is what I would like it to look like when there is less items

The items that are less should fall on the center

the items that are less should fall on the center, this way when it is maybe just one item it should not show on the far left but it should just be on the center like this...single item apear on the center

and here is my code

GridView.builder(
              shrinkWrap: true,
              physics: BouncingScrollPhysics(),
              itemCount: staffs == null ? 0 : staffs.length,
              gridDelegate:
              SliverGridDelegateWithFixedCrossAxisCount(
                  childAspectRatio: 1.0, crossAxisCount: 6),
              itemBuilder: (BuildContext context, int index) {
                return InkWell(
                    onTap: () {
                      showPinDialog(staffs[index]);
                    },

                    child: Container(
                      // padding: EdgeInsets.only(left: 40, right: 40),
                      margin:
                      EdgeInsets.only(right: 5, bottom: 5),
                      decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10.0),
                          border: Border.all(
                              color:
                              Colors.white)),
                      child: Column(
                        mainAxisAlignment:
                        MainAxisAlignment.spaceEvenly,
                        children: <Widget>[
                          Image.asset(
                              'assets/icons/black/user.png',
                              width: iconSize,
                              height: iconSize,
                              fit: BoxFit.contain
                          ),
                          Center(
                            child: Text(
                              '${staffs[index]['name'][0].toUpperCase()}${staffs[index]['name'].substring(1)}',
                              // staffs[index]['name'],
                              style: TextStyle(
                                fontSize: staffNameFontSize,
                                color: Color(0xff000000),
                                fontWeight: FontWeight.bold,

                              ),
                              overflow: TextOverflow.ellipsis,
                              maxLines: 2,
                            ),
                          ),
                        ],
                      ),
                    )
                );
              },
            )
Cycad answered 21/5, 2020 at 0:59 Comment(1)
It's May help https://mcmap.net/q/1396252/-how-to-center-gridview-items-in-flutterDefalcate
S
0

GridView doesn't seem to have an alignment property for its items. A workaround for this use case is to use a ListView and use Wrap for its rows to help them align in center. A sample is demonstrated in this post.

Scissure answered 18/9, 2023 at 14:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.