Flutter
Background Image
Flutter UI
Mobile Development
Dart Programming

How do I Set Background image in Flutter?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Flutter, the usual way to set a background image is to use a Container with BoxDecoration, or a Stack when you want content layered above the image. The right choice depends on whether the image is purely decorative or part of a more complex layout.

The Common Asset-Based Approach

For a full-screen background image from your app's assets, combine Container and BoxDecoration.

dart
1import 'package:flutter/material.dart';
2
3void main() {
4  runApp(const MyApp());
5}
6
7class MyApp extends StatelessWidget {
8  const MyApp({super.key});
9
10  
11  Widget build(BuildContext context) {
12    return MaterialApp(
13      home: Scaffold(
14        body: Container(
15          decoration: const BoxDecoration(
16            image: DecorationImage(
17              image: AssetImage('assets/background.jpg'),
18              fit: BoxFit.cover,
19            ),
20          ),
21          child: const Center(
22            child: Text(
23              'Hello Flutter',
24              style: TextStyle(color: Colors.white, fontSize: 28),
25            ),
26          ),
27        ),
28      ),
29    );
30  }
31}

This works well because the image belongs to the decoration layer and the child widgets render on top of it.

Remember The Asset Declaration

The image must also be listed in pubspec.yaml.

yaml
flutter:
  assets:
    - assets/background.jpg

If you forget this step, Flutter cannot bundle the asset and the background will fail to load.

When Stack Is A Better Fit

If you want more explicit control over layering, Stack is often clearer than a decorated Container.

dart
1import 'package:flutter/material.dart';
2
3class BackgroundExample extends StatelessWidget {
4  const BackgroundExample({super.key});
5
6  
7  Widget build(BuildContext context) {
8    return Scaffold(
9      body: Stack(
10        fit: StackFit.expand,
11        children: [
12          Image.asset(
13            'assets/background.jpg',
14            fit: BoxFit.cover,
15          ),
16          Container(color: Colors.black.withOpacity(0.35)),
17          const Center(
18            child: Text(
19              'Overlay content',
20              style: TextStyle(color: Colors.white, fontSize: 24),
21            ),
22          ),
23        ],
24      ),
25    );
26  }
27}

This pattern is useful when you want:

  • a semi-transparent overlay
  • separate animated foreground layers
  • a background image that behaves like a normal widget rather than a decoration

Asset Image Versus Network Image

If the background comes from the network, use Image.network or NetworkImage instead of AssetImage.

dart
1Container(
2  decoration: const BoxDecoration(
3    image: DecorationImage(
4      image: NetworkImage('https://picsum.photos/800/1200'),
5      fit: BoxFit.cover,
6    ),
7  ),
8)

For remote images, think about loading states and failed requests. A decorative background that arrives late can make the screen flash from blank to full image if you do not design for it.

Choosing The Right BoxFit

The most common fit modes are:

  • 'BoxFit.cover fills the area and may crop the image'
  • 'BoxFit.contain keeps the whole image visible and may leave empty space'
  • 'BoxFit.fill stretches the image and may distort it'

For screen backgrounds, BoxFit.cover is usually the right default because it looks intentional on different device sizes.

Full-Screen Layout Details

If the background should fill the whole screen behind safe areas and content, use a layout that expands fully.

A Scaffold body naturally fills available space, and StackFit.expand or a full-size Container ensures the image covers the screen. You generally do not need to calculate screen dimensions manually for this.

If content must respect the notch or system UI, wrap the foreground content in SafeArea instead of shrinking the background itself.

Decorative Backgrounds Versus Semantic Images

If the image is purely visual, decoration or a background Image widget is appropriate. If the image conveys important information, make it part of the visible widget tree with proper semantics instead of hiding it in decoration.

For ordinary splash screens, onboarding pages, and hero sections, decoration is usually fine.

Common Pitfalls

  • Forgetting to declare the asset in pubspec.yaml.
  • Using BoxFit.fill and unintentionally distorting the image.
  • Putting a bright image behind text without an overlay, making the UI hard to read.
  • Using a huge image asset and increasing app size and memory pressure unnecessarily.
  • Treating the background as content when it is really just decoration, or vice versa.

Summary

  • Use Container plus BoxDecoration for a simple background image.
  • Use Stack when you want explicit layering and overlays.
  • Declare local assets in pubspec.yaml.
  • 'BoxFit.cover is usually the best fit for full-screen backgrounds.'
  • Keep readability, asset size, and image semantics in mind when choosing the approach.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.