Photo Grid
Loading preview
Starting the Flutter app
A showcase of Flutter's photo grid functionality
import 'package:flutter/material.dart';
class PhotoGrid extends StatefulWidget {
const PhotoGrid({super.key});
@override
State<PhotoGrid> createState() => _PhotoGridState();
}
class _PhotoGridState extends State<PhotoGrid> {
var _columns = 3;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Photos'),
actions: [
IconButton(tooltip: 'Change layout', icon: const Icon(Icons.grid_view), onPressed: () => setState(() => _columns = _columns == 3 ? 2 : 3)),
],
),
body: GridView.builder(
padding: const EdgeInsets.all(4),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: _columns,
crossAxisSpacing: 4,
mainAxisSpacing: 4,
childAspectRatio: 1,
),
itemCount: 12,
itemBuilder: (context, i) {
return Container(
color: Colors.primaries[i % Colors.primaries.length].withValues(alpha: 0.5),
child: Icon(
Icons.photo,
color: Colors.white.withValues(alpha: 0.8),
size: 32,
),
);
},
),
);
}
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
There is no backing data at all: itemCount: 12 is a literal, and each cell is a tinted Container with a photo icon, colored by a Colors.primaries modulo. The grid has square cells with 4px gaps. The grid_view action switches _columns between three and two, and tapping a tile does nothing.
Adapting it
- Introduce a list of URLs, set
itemCountfrom it, and return an image withBoxFit.coverfromitemBuilder. - Save
_columnsso the layout choice persists. - Wrap the
Containerin aGestureDetectorto open a full-screen viewer.