Skip to content

Intermediate · Layouts · topic 21 of 50 · 30 min

Images, icons, fonts and themes

Explanation

Show images from your app with Image.asset after declaring them in pubspec.yaml, or from the internet with Image.network (with loading and error builders). Icons come from Icons.*. Custom fonts are declared in pubspec.yaml. ThemeData with ColorScheme.fromSeed sets colours for the whole app, Theme.of(context).textTheme gives consistent text styles, and darkTheme with themeMode supports dark mode.

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.

yaml
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: 700
Indentation matters in YAML. After changing assets or fonts, do a full restart.
dart
Image.asset('assets/images/logo.png', height: 64)
The path must match the file exactly, including upper and lower case.

Network images

dart
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),
)
Always handle loading and errors: mobile networks fail. The cached_network_image package adds disk caching.

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.

dart
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(),
)
Change the seed colour once and every button, app bar and chip follows.
dart
final theme = Theme.of(context);
Text('Today\'s offers', style: theme.textTheme.titleLarge);
Container(color: theme.colorScheme.primaryContainer);
Read colours and text styles from the theme instead of hard-coding them.
Think of it like this: A theme is like a school uniform rule. Instead of every student choosing colours, the school sets them once: shirts sky blue, trousers navy, ties maroon. When the school changes the uniform, every student's outfit changes together. Hard-coded colours are students ignoring the rule.

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: - assets/images/

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.

dart
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))
One function builds both themes.

Practice (0/3 done)

Task 1 · Question

You get "Unable to load asset: assets/images/logo.png". What is the most likely cause?

Task 2 · Question

Which BoxFit fills the box completely and crops the image if needed?

Task 3 · Practice

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

Question 1

1.What must you do after adding a new font in pubspec.yaml?

Question 2

2.What does ColorScheme.fromSeed do?

Question 3

3.How do you make the app follow the phone's dark mode setting?

Question 4

4.Why use Theme.of(context).textTheme.titleLarge instead of TextStyle(fontSize: 22)?

Read more (free)

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

Answer: MaterialTheme.typography

Use the type scale from the theme.

From Material 3 theming

Which command downloads the packages listed in pubspec.yaml?Related

Answer: flutter pub get

pub get resolves and downloads dependencies.

From Packages and pub

Which package generates launcher icons for all sizes from one image?Related

Answer: flutter_launcher_icons

It creates Android and iOS icons.

From Preparing a release

What is analysis_options.yaml for?Related

Answer: Lint rules and static analysis settings

It configures the analyser.

From Project structure and hot reload

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

  1. This is the last topic of “Layouts”. The module exam comes next.
  2. Module 5 exam: Layouts · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Lists, navigation and forms. 50 topics left in the course before the final exam.

← Previous: Understanding constraints and fixing overflow