Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Image for Scaffold

I want to set the image as the background color for Scaffold. When setting an AppBar and bottom bar, using the decoration of the Container as the body of the scaffold doesn't cover the complete screen.

I want to show background for full screen. Below is my Scaffold code:

Scaffold(
      backgroundColor: Image.asset('images/background.png').color,
      body: Container(
        decoration: defaultAppBoxDecoration(),
      ),
      appBar: AppBar(
        elevation: 0.0,
        backgroundColor: Colors.transparent,
        title: Text('Title here', style: TextStyle(color: Colors.teal),),
        leading: IconButton(
          icon: Image.asset('images/star.png'),
          onPressed: () {},
        ),
        actions: <Widget>[
          IconButton(icon: Image.asset('images/star.png')),
                  //  IconButton(icon: Image.asset('images/grid.png')),

        ],
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: FloatingActionButton(
        child:           IconButton(icon: Image.asset('images/star.png')),
      ),
      bottomNavigationBar: Container(
        padding: EdgeInsets.only(left: 4.0, right: 4.0),
        height: 44.0 + MediaQuery.of(context).padding.bottom,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
                      IconButton(icon: Image.asset('images/star.png')),
          IconButton(icon: Image.asset('images/star.png')),

          ],
        ),
      ),
    );

Please refer to the image

like image 309
Ankur Prakash Avatar asked Jan 17 '19 18:01

Ankur Prakash


People also ask

How do you make a background image in scaffold flutter?

Steps to set the background image: Step 1: Add the Container widget. Step 2: Add the decoration parameter (inside Container) and assign the BoxDecoration class. Step 3: Add the image parameter (inside BoxDecoration) and assign the DecorationImage class.


Video Answer


3 Answers

Try using Stack check the following sample:

  @override
    Widget build(BuildContext context) {
      return Stack(
        children: <Widget>[
          Image.asset(
            "resources/background.png",
            height: MediaQuery.of(context).size.height,
            width: MediaQuery.of(context).size.width,
            fit: BoxFit.cover,
          ),
          Scaffold(
              backgroundColor: Colors.transparent,
              appBar: AppBar(
                backgroundColor: Colors.transparent,
                elevation: 0.0,
              ),
              bottomNavigationBar: Container(
                padding: EdgeInsets.only(left: 4.0, right: 4.0),
                height: 44.0 + MediaQuery.of(context).padding.bottom,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    IconButton(icon: Icon(Icons.star)),
                    IconButton(icon: Icon(Icons.star)),
                  ],
                ),
              ),
              body: Text("Hello world"))
        ],
      );
    }
like image 96
diegoveloper Avatar answered Nov 18 '22 18:11

diegoveloper


You can place your Scaffold inside Container with background image and use transparent color for Scaffold's body like this:

Container(
  decoration: BoxDecoration(
    image: DecorationImage(
      image: AssetImage("assets/bg.jpg"),
      fit: BoxFit.cover,
    ),
  ),
  child: Scaffold(
    appBar: AppBar(
      title: Text(widget.title),
    ),
    backgroundColor: Colors.transparent,
    body: Container(),
),);
like image 26
Valentina Konyukhova Avatar answered Nov 18 '22 18:11

Valentina Konyukhova


Setting the backgroundColor:Colors.transparent is the key you are missing. enter image description here

like image 39
Rohit Mandiwal Avatar answered Nov 18 '22 17:11

Rohit Mandiwal