Intermediate · Layouts · topic 21 of 50 · 30 min
Images, icons, fonts and themes
Explanation
Asset images
An asset is a file bundled inside your app: images, icons, fonts, JSON data. Put images in a folder like assets/images/ and declare the folder in pubspec.yaml. Flutter only bundles assets you declare.
flutter:
uses-material-design: true
assets:
- assets/images/
fonts:
- family: Poppins
fonts:
- asset: assets/fonts/Poppins-Regular.ttf
- asset: assets/fonts/Poppins-Bold.ttf
weight: 700Image.asset('assets/images/logo.png', height: 64)Network images
Image.network(
'https://example.com/products/42.jpg',
height: 160,
fit: BoxFit.cover,
loadingBuilder: (context, child, progress) =>
progress == null ? child : const Center(child: CircularProgressIndicator()),
errorBuilder: (context, error, stack) => const Icon(Icons.broken_image, size: 48),
)BoxFit decides how an image fills its box: cover fills and crops, contain shows the whole image with possible empty space, fill stretches (usually ugly). Large photos waste memory on small widgets; cacheWidth/cacheHeight (or resizing on the server) help (module 9).
Product photos usually come from a server, so most shopping and food apps use network images for content and asset images only for logos, illustrations and empty-state pictures.
Icons
Material icons are built in: Icon(Icons.shopping_cart), Icon(Icons.home, size: 28, color: Colors.teal). Cupertino (iOS-style) icons come from the cupertino_icons package. For custom icons, use SVGs with a package like flutter_svg, or an icon font.
Images often come in different resolutions for different screens. Flutter supports variants in folders such as assets/images/2.0x/logo.png and 3.0x/logo.png; Image.asset picks the best one for the device. For simple logos and icons, SVG files (with the flutter_svg package) stay sharp at every size.
For accessibility, give meaningful images a semanticLabel (for example Image.asset(..., semanticLabel: 'Shop logo')) so screen readers can describe them. Purely decorative images can be excluded from semantics.
Themes
A theme defines colours, text styles and component styles for the whole app in one place. Material 3 is the default design in current Flutter. ColorScheme.fromSeed generates a full, accessible colour scheme from one brand colour.
MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF00695C)),
fontFamily: 'Poppins',
),
darkTheme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF00695C), brightness: Brightness.dark),
fontFamily: 'Poppins',
),
themeMode: ThemeMode.system, // follow the phone's light/dark setting
home: const HomePage(),
)final theme = Theme.of(context);
Text('Today\'s offers', style: theme.textTheme.titleLarge);
Container(color: theme.colorScheme.primaryContainer);Common mistakes
- "Unable to load asset": the asset is not declared in pubspec.yaml, the path or case is wrong, or you hot reloaded instead of restarting.
- Image.network without errorBuilder: a broken image icon or exception on slow networks.
- Hard-coded Colors.blue everywhere: dark mode and rebranding break.
- Huge images (4000 x 3000) shown in 100-pixel thumbnails: memory and jank.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Images, icons, fonts and themes · 1/6
Assets are files bundled with your app. Declare them in pubspec dot yaml.
Prefer a video by a teacher? Find free videos on “flutter images assets network image custom fonts theme colorscheme fromseed dark mode” on YouTube ↗
In real life: Branding a local business app
A sweet shop chain wants its app in its brand colours with its Devanagari-friendly font. The developer adds the font files and logo as assets, sets ColorScheme.fromSeed with the brand's maroon, defines a dark theme, and uses Theme.of(context) in every widget. When the brand later refreshes its colour to gold, one line changes.
ThemeData brandTheme(Brightness b) => ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF7B1E3A), brightness: b),
fontFamily: 'Mukta', // a font with good Devanagari support, declared in pubspec
);
// MaterialApp(theme: brandTheme(Brightness.light), darkTheme: brandTheme(Brightness.dark))Practice (0/3 done)
You get "Unable to load asset: assets/images/logo.png". What is the most likely cause?
Which BoxFit fills the box completely and crops the image if needed?
Add a logo asset and a custom font to a project, show the logo with Image.asset, show a product photo with Image.network including loading and error builders, and set a theme with ColorScheme.fromSeed plus a dark theme following the system. Replace every hard-coded colour with colours from Theme.of(context).
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Stack, Positioned and AlignLayouts · same module
- Container, Padding, SizedBox, Expanded and FlexibleLayouts · same module
- Material 3 themingKotlin and Android App Development · Permissions, Material 3 and polish
- Preparing a releasePublishing to the stores and the web
- Accessibility and adaptive layoutsKotlin and Android App Development · Permissions, Material 3 and polish
- Everything is a widgetFlutter setup and your first app
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What must you do after adding a new font in pubspec.yaml?
Answer: A full restart (hot reload is not enough)
Assets are bundled at build time.
What does ColorScheme.fromSeed do?
Answer: Generates a full colour scheme from one seed colour
Material 3 tonal palettes from a seed.
How do you make the app follow the phone's dark mode setting?
Answer: themeMode: ThemeMode.system with a darkTheme
Provide darkTheme and use ThemeMode.system.
Why use Theme.of(context).textTheme.titleLarge instead of TextStyle(fontSize: 22)?
Answer: Consistency across the app and automatic adaptation to theme changes
One source of truth for styles.
Where do text styles like titleLarge come from?Related
Which command downloads the packages listed in pubspec.yaml?Related
Which package generates launcher icons for all sizes from one image?Related
What is analysis_options.yaml for?Related
Answer: Lint rules and static analysis settings
It configures the analyser.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- This is the last topic of “Layouts”. The module exam comes next.
- Module 5 exam: Layouts · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Lists, navigation and forms. 50 topics left in the course before the final exam.