How to set Background Image to Scaffold in Flutter

How to set Background Image to Screen in Flutter.

Hello Guys How are you all ? Hope You All Are Fine. Today We Are Going To Learn How to set Background Image to Scaffold in Flutter. ?with This Tutorial. Somethimes we have to use background image in pericular screen in flutter. Flutter Add Set Full Screen Background Image to Scaffold Container

In very Easy way We are going to learn How to set Background Image to Scaffold in Flutter. So Without wasting your time lets start this article.

BoxDecoration has image property and we can easily set background image. so without wasting your time lets start this article.

How to set Background Image to Scaffold in Flutter
  • Import material.dart package in your app’s main.dart file.
import 'package:flutter/material.dart';
  • Create Stateless widget and Define in runApp.
void main() => runApp(MyApp());
  •  Now we would make Scaffold widget and put a Center Widget in it.
        home: Scaffold(
        appBar: AppBar(
          title: Text("Flutter Background Image - FlutterCorner"),
        ),
        body: Container()
  • Now in body’s Container add decoration with BoxDecoration. BoxDecoration Is usually Used to decorate the Container widget. We would use the image property of box decoration to set background image from URL resource.
        body: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: NetworkImage(
                  "https://cdn.pixabay.com/photo/2015/08/28/16/38/stars-912134_960_720.jpg"),
              fit: BoxFit.cover,
            ),
          ),
  • Complete source code for my main.dart file:
import 'dart:ui';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.black),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text("Flutter Background Image - FlutterCorner"),
        ),
        body: Container(
          decoration: BoxDecoration(
            image: DecorationImage(
              image: NetworkImage(
                  "https://cdn.pixabay.com/photo/2015/08/28/16/38/stars-912134_960_720.jpg"),
              fit: BoxFit.cover,
            ),
          ),
          child: Center(
            child: Text(
              "Flutter Background Image - FlutterCorner",
              style: TextStyle(
                color: Colors.white,
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

So Guys That’s it For This Tutorial, hope you like our tutorial. Comment below Your thoghts and your quries. And Also Comment below your suggestion here.

Read Also How to create Toast in Flutter With Example

Milan Dhameliya is an Information Technology Engineer. By profession, he is a Full Stack developer with knowledge of multiple back-end platforms (e.g., PHP, Node.js, Python) and frontend Flutter frameworks.

1 thought on “How to set Background Image to Scaffold in Flutter”

Leave a Comment