flutter_staggered_grid_view icon indicating copy to clipboard operation
flutter_staggered_grid_view copied to clipboard

Allow to change crossAxisCount dynamically.

Open harshalv-irys opened this issue 5 years ago • 8 comments

Hello Team, I'm working with provided StaggeredGridView widget and trying to switch in between 3 to 2 column grid view. Initially loaded data with the 3 column view but after on dynamic change state to 2 columns view it's changing view but showing just first 2 columns data from 3 column gridview. (Example: Suppose there are total 15 items in 3 column view then after switch in 2 column view it's just showing 10 items.)

Please check the code I've written as below: ``` Expanded( child: StaggeredGridView.countBuilder( crossAxisCount: ViewStateHelper.getColumnCount(_viewState), itemCount: _catalogItems.length, itemBuilder: (BuildContext context, int index) => CatalogItem(_catalogItems[index], _viewState), staggeredTileBuilder: (int index) => StaggeredTile.fit(1)));


enum ViewState { TWO_COLUMN_VIEW, THREE_COLUMN_VIEW }

class ViewStateHelper { static int getColumnCount(ViewState viewState) => viewState == ViewState.TWO_COLUMN_VIEW ? 2 : 3; }


Please suggest & provide the appropriate solution.

harshalv-irys avatar Feb 29 '20 11:02 harshalv-irys

@letsar @dereklakin @brianegan Please help me to resolve this issue, thanks in advance.

harshalv-irys avatar Feb 29 '20 11:02 harshalv-irys

I'm having the same problem. On resize the screen and swapping the crossAxisCount from 3 to 2 it will cut off items and just not showing them. On a full refresh of the page the items appear again in the correct way.

Is there a way to "rebuild" on resize?

Aerofluxx avatar May 27 '20 07:05 Aerofluxx

Append the value of the crossAxisCount to the Key of the StaggeredGridView. When changes the existing widget will not be reused.

apps-transround avatar Jul 28 '20 08:07 apps-transround

I followed @apps-transround suggestion and my issue is now fixed,

Code example:
StaggeredGridView.count(
          primary: false,
          key: ObjectKey(gridViewColumns),
          addAutomaticKeepAlives: false,
          crossAxisCount: gridViewColumns.toInt(),
          padding: const EdgeInsets.all(1.0),
          children: <Widget>[], // add here your items
          staggeredTiles: snapshot.data
              .map<StaggeredTile>((_) => StaggeredTile.fit(1))
              .toList(),
          mainAxisSpacing: 3.0,
          crossAxisSpacing: 0.0, // add some space
        ),

edwin-alvarez avatar Jul 31 '20 15:07 edwin-alvarez

Thanks for the suggestion. Here is a code example that works without any changes needed.

SliverStaggeredGrid.countBuilder(
              itemCount: 200,
              key: ObjectKey(columns),
              crossAxisCount: columns,
              mainAxisSpacing: 20,
              crossAxisSpacing: 10,
              itemBuilder: (BuildContext context, int index) {
                return Container(
                  color: Colors.red,
                  height: 200,
                );
              },
              staggeredTileBuilder: (int index) {
                return StaggeredTile.fit(1);
              },
            ),

I am renaming this issue to make the problem easier to understand.

Right now the only available option is to use a different key when the crossAxisCount changes. This makes the Flutter Framework think it is a different widget and builds it form scratch ignoring any previous state.

jamesblasco avatar Oct 11 '20 10:10 jamesblasco

Can you test with the latest 0.5.0 preview? https://pub.dev/packages/flutter_staggered_grid_view/versions/0.5.0-dev.1

letsar avatar Dec 12 '21 18:12 letsar

well if you are using staggered grid view for the infinite scrolling then it won't work.

umairali4433 avatar Jan 11 '22 15:01 umairali4433

@umairali4433 are you sure? Code sample, please.

apps-transround avatar Aug 07 '22 11:08 apps-transround