Which widget would be best to use? If listview.builder then can you please share a sample code? This is the UI.
CodePudding user response:
You can implement it in two ways:
- GridView Widget
- flutter_staggered_grid_view Package
CodePudding user response:
you can achieve that with a GridView widget
like so
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
appBar: AppBar(
title: const Text("Flutter GridView Demo"),
),
body: GridView.count(
primary: false,
padding: const EdgeInsets.all(30),
crossAxisSpacing: 10,
mainAxisSpacing: 10,
crossAxisCount: 2,
children: List.generate(choices.length, (index) {
return Center(
child: SelectCard(choice: choices[index]),
);
}))),
));
}
}
class Choice {
const Choice({required this.title, required this.icon});
final String title;
final IconData icon;
}
const List<Choice> choices = <Choice>[
Choice(title: 'Home', icon: Icons.home),
Choice(title: 'Contact', icon: Icons.contacts),
Choice(title: 'Map', icon: Icons.map),
Choice(title: 'Phone', icon: Icons.phone),
Choice(title: 'Camera', icon: Icons.camera_alt),
Choice(title: 'Setting', icon: Icons.settings),
Choice(title: 'Album', icon: Icons.photo_album),
Choice(title: 'WiFi', icon: Icons.wifi),
];
class SelectCard extends StatelessWidget {
const SelectCard({Key? key, required this.choice}) : super(key: key);
final Choice choice;
@override
Widget build(BuildContext context) {
return Card(
color: Colors.orange,
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Expanded(child: Icon(choice.icon, size: 50.0)),
Text(choice.title),
]),
));
}
}