letsar / flutter_staggered_grid_view

A Flutter staggered grid view
MIT License
3.15k stars 510 forks source link

Add items dynamically to the staggered grid view #71

Closed mpigorini closed 4 years ago

mpigorini commented 5 years ago

Is possible add items dynamically with out refreshing all the grid view ?

jamesblasco commented 4 years ago

Yess, you can update the list and give a key value to each child. This way the Flutter framework will understand what Widgets have been changed are which ones are new.

I suggest you use the countBuilder constructor if you have a lot of items.

StaggeredGridView.countBuilder(
  crossAxisCount: 4,
  itemCount: 8,
  itemBuilder: (BuildContext context, int index) => new Container(
      key: ValueKey('Item$indext'),
      color: Colors.green,
      child: new Center(
        child: new CircleAvatar(
          backgroundColor: Colors.white,
          child: new Text('$index'),
        ),
      )),
  staggeredTileBuilder: (int index) =>
      new StaggeredTile.count(2, index.isEven ? 2 : 1),
  mainAxisSpacing: 4.0,
  crossAxisSpacing: 4.0,
)