Flutter Images & Icons
Pictures make an app feel alive. Flutter can show images bundled with your app (assets), images downloaded from the internet (network), and thousands of built-in Material icons that scale perfectly at any size.
Icons
The Icon widget draws a glyph from an icon font. The Icons class contains the full Material icon set:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: IconRow()))));
class IconRow extends StatelessWidget {
const IconRow({super.key});
@override
Widget build(BuildContext context) {
return const Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Icon(Icons.home),
Icon(Icons.favorite, color: Colors.pink, size: 36),
Icon(Icons.star_rounded, color: Colors.amber, size: 48),
Icon(Icons.settings_outlined, size: 32, semanticLabel: 'Settings'),
],
);
}
}Most icons come in variants: Icons.star, Icons.star_outline, Icons.star_rounded, Icons.star_sharp. semanticLabel is read aloud by screen readers. If you omit color and size, the icon uses the surrounding IconTheme (for example, white inside a coloured app bar).
Tip: For iOS-style icons use
CupertinoIconsfrompackage:flutter/cupertino.dart(included through thecupertino_iconsdependency in new projects).
Network images
Image.network downloads and caches an image from a URL. Because the network is slow and unreliable, give it a loading indicator and an error fallback:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Center(child: Avatar()))));
class Avatar extends StatelessWidget {
const Avatar({super.key});
@override
Widget build(BuildContext context) {
return ClipRRect(
borderRadius: BorderRadius.circular(16),
child: Image.network(
'https://picsum.photos/id/237/300/200',
width: 300,
height: 200,
fit: BoxFit.cover,
loadingBuilder: (context, child, progress) =>
progress == null ? child : const SizedBox(
width: 300, height: 200,
child: Center(child: CircularProgressIndicator()),
),
errorBuilder: (context, error, stack) =>
const Icon(Icons.broken_image, size: 64, color: Colors.grey),
),
);
}
}ClipRRect clips its child to a rounded rectangle — the usual way to round an image's corners.
Note: On Android, network access needs the
INTERNETpermission inandroid/app/src/main/AndroidManifest.xmlfor release builds (debug builds include it automatically).
Asset images
Images shipped inside the app must be registered in pubspec.yaml:
flutter:
assets:
- assets/images/ # every file in this folderThen load them by path with Image.asset:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: WelcomePage()));
class WelcomePage extends StatelessWidget {
const WelcomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.asset('assets/images/logo.png', width: 120),
const SizedBox(height: 16),
const Text('Welcome to Bloom', style: TextStyle(fontSize: 24)),
const SizedBox(height: 24),
const CircleAvatar(
radius: 32,
backgroundImage: AssetImage('assets/images/profile.jpg'),
),
],
),
);
}
}Image.asset is a widget; AssetImage (and NetworkImage) are image providers — use providers where a property expects an ImageProvider, such as CircleAvatar.backgroundImage or DecorationImage.
BoxFit: how an image fills its box
BoxFit |
Behaviour |
|---|---|
contain |
Whole image visible, may leave empty bars |
cover |
Fills the box completely, crops the overflow |
fill |
Stretches to the box, may distort |
fitWidth / fitHeight |
Matches one dimension |
none |
Original size, centred and clipped |
Image as a background
DecorationImage puts an image behind a Container's child:
Container(
height: 180,
alignment: Alignment.bottomLeft,
padding: const EdgeInsets.all(16),
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/mountains.jpg'),
fit: BoxFit.cover,
),
),
child: const Text('Explore', style: TextStyle(color: Colors.white, fontSize: 28)),
)Test yourself
What must you do before using Image.asset('assets/images/logo.png')?
- Nothing, all files are bundled
- Register the file or folder under
flutter: assets:in pubspec.yaml - Upload it to a server
- Add it to AndroidManifest.xml
Which BoxFit fills the whole box and crops whatever does not fit?
- contain
- fill
- cover
- none
Which widget is commonly used to give an image rounded corners?
- Padding
- ClipRRect
- Align
- SizedBox