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.
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.
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.
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.
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.
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.coverfills the area and may crop the image' - '
BoxFit.containkeeps the whole image visible and may leave empty space' - '
BoxFit.fillstretches 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.filland 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
ContainerplusBoxDecorationfor a simple background image. - Use
Stackwhen you want explicit layering and overlays. - Declare local assets in
pubspec.yaml. - '
BoxFit.coveris usually the best fit for full-screen backgrounds.' - Keep readability, asset size, and image semantics in mind when choosing the approach.
Related reading
- How do I set bold and italic on UILabel of iPhone/iPad?
- How do I set bold and italic on UILabel of iPhone/iPad?
- How do I set the height of tableHeaderView UITableView with autolayout?
- How do I set the size of a SF Symbol in SwiftUI?
- How do I set up a simple delegate to communicate between two view controllers?
- How do I set up IntelliJ IDEA for Android applications?
- How do I set up IntelliJ IDEA for Android applications?
- How do I shake an Android device within the Android emulator to bring up the dev menu to debug my React Native app
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.