I'm working with a StaggeredGridView within Flutter and I don't seem to see a way to center a tile if a tile is going to be the only tile in that "row".
For example, if I set the crossAxisCount of the StaggeredGridView to something like 6; and then sent the tile's crossAxisCellCount to 4, the tile occupies 4 "cells" starting from the left, leaving 2 "Cells" worth of empty space on the right if there isn't a tile that can occupy 2 cells.
Is it possible to force the tile to be centered? Essentially, making it occupy cells 2 - 5, leaving 1 empty cell on the left and 1 empty cell on the right?
I've tried wrapping the StaggeredGridView in a Center widget, but this didn't appear to make a difference.
Currently I have a stateful widget which has the following build method.
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<ProfileEditScreenViewModel>(
create: (context) => model,
child: Consumer<ProfileEditScreenViewModel>(
builder: (context, model, child) => Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(model.screenTitle),
),
body: Center(
child: StaggeredGridView.count(
crossAxisCount: 6,
shrinkWrap: true,
mainAxisSpacing: 10.0,
crossAxisSpacing: 10.0,
staggeredTiles: model.displayCardTiles,
children: model.displayCards,
),
),
),
),
);
}
In the View model for that widget there are two relevant functions that the StaggeredGridView uses: the displayCardTiles function that creates the StaggeredTiles, and the displayCards function which creates the widgets that go in the tiles. Those two functions are as follows:
List<StaggeredTile> _buildDisplayCardTiles(){
List<StaggeredTile> myList = [];
for(var bioCategory in userProfile.bioCategories!){
myList.add(StaggeredTile.fit(bioCategory.crossAxisCellCount));
}
return myList;
}
List<Widget> _buildDisplayCards(){
List<Widget> myList = [];
for(var bioCategory in userProfile.bioCategories!){
myList.add(ProfileItemCard(bioCategory: bioCategory));
}
return myList;
}
The "ProfileItemCard" is just a Card widget that displays a variety of Text widgets and checkboxes, but it's contents wouldn't impact the position of the Card within the StaggeredGridView.
I've also tried wrapping the ProfileItemCard in a Center widget, but it doesn't have any impact on how the Card is displayed.
Intro: I came into this myself, so I want to thank you for opening the question.
Baseline: calculate the remainder of the model count and the cross axis count (
userProfile.bioCategories
and6
in the original question), then place the "remaining" tiles in a row (spaced evenly) and span the row all over entire grid width.Demo
Repro: Github.
Code (a single-file solution, the only prerequsite is
flutter_staggered_grid_view: 0.4.0
):