CoderUni
CoderUni

Reputation: 6164

Horizontally Center Last Odd Item of Gridview Flutter

I am using a Gridview builder to lazily display my items. My gridview's itemcount is 5 and crossAxiscount is 2. Is there a way to horizontally center the 5th item? Thank you for trying to help me.

Here is how my Gridview looks like:

GridView.builder(
 shrinkWrap: true,
 itemCount: list.length, //the value is 5
 gridDelegate:
  SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
   itemBuilder: (BuildContext context, int i) {
     return card(i); //widget that returns a card
  },
),

Upvotes: 0

Views: 1521

Answers (1)

CoderUni
CoderUni

Reputation: 6164

I finally found out what I should do. Since my GridView's itemcount is static and is known which is five, I could simply wrap card(i) using the Wrap widget and set its alignment as well as its crossAxisAlignment to center.

Here is how I was able to solve it:

Wrap(
 spacing: 8, //vertical spacing
 runSpacing: 8, //horizontal spacing
 alignment: WrapAlignment.center,
 crossAxisAlignment: WrapCrossAlignment.center,
  children: <Widget>[
   card(0),
   card(1),
   card(2),
   card(3),
   card(4),
  ],
),

Upvotes: 1

Related Questions