Использование flutter_staggered_grid_view плагин.
КОД:
class MyHomeScreen extends StatefulWidget {
@override
_MyHomeScreenState createState() => _MyHomeScreenState();
}
class _MyHomeScreenState extends State<MyHomeScreen> {
List<ItemDetailsModel> itemDetails = [
ItemDetailsModel(
name: "Hello 1",
color: Colors.teal
),
ItemDetailsModel(
name: "Hello 2",
color: Colors.teal[100]
),
ItemDetailsModel(
name: "Hello 3",
color: Colors.teal[200]
),
ItemDetailsModel(
name: "Hello 4",
color: Colors.teal[300]
),
ItemDetailsModel(
name: "Hello 5",
color: Colors.teal
),
ItemDetailsModel(
name: "Hello 6",
color: Colors.tealAccent
),
ItemDetailsModel(
name: "Hello 7",
color: Colors.tealAccent[200]
),
ItemDetailsModel(
name: "Hello 8",
color: Colors.tealAccent[300]
),
ItemDetailsModel(
name: "Hello 9",
color: Colors.green
),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Staggered Grid View"),),
body: Center(
child: sliverGridWidget(context),
),
);
}
Widget sliverGridWidget(BuildContext context){
return StaggeredGridView.countBuilder(
padding: const EdgeInsets.all(8.0),
crossAxisCount: 4,
itemCount: itemDetails.length,
itemBuilder: (context, index){
return Container(
color:itemDetails[index].color,
child:Text(itemDetails[index].name)
);
},
staggeredTileBuilder: (index) => StaggeredTile.count(2,3), //Make size as you want.
mainAxisSpacing: 8.0,
crossAxisSpacing:8.0,
);
}
}
Класс модели:
class ItemDetailsModel{
String name;
Color color;
ItemDetailsModel({this.name, this.color});
}
Вывод: