How to set background image in flutter?

3.6k Views Asked by At

I have started learning flutter recently, I want to add background image and on it I want to add two flat buttons, But I am not able to do it, I have added background image in code, but it is not working I didn't understand the problem. Could anyone help me out?

    void main() {
      return runApp(
        MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              title: Text('Ani & genny',
                  style: TextStyle(
                    fontFamily: 'Pacifico',
                    color: Colors.white,
                  )),
              backgroundColor: Colors.red,
            ),
            body: MyApp(),
          ),
        ),
      );
    }

    class MyApp extends StatelessWidget {
      void navigateToPage() {
        print('Button pressed!!!!');
      }

      @override
      Widget build(BuildContext context) {
        return Center(
          child: Column(
            children: <Widget>[
              SizedBox(
                height: 50,
              ),
              Container(
                decoration: BoxDecoration(
                  image: DecorationImage(
                image: AssetImage("images/Ani.png"),
                fit: BoxFit.cover

                ),
                ),

              ),
              FlatButton(
                  child: Image.asset(
                    'images/li.png',
                    height: 120,
                    width: 120,
                  ),
                  onPressed: () {
                    navigateToPage();
                  }),
                ),
        ],
      ),
    );
  }
}
2

There are 2 best solutions below

0
On

Here is the sample code which puts a button on the image. You can use Positioned widget to place wherever you need.

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
           Image.network(
              'https://picsum.photos/250?image=9',
           ),
           FlatButton(
              child: Text("Submit"),
              onPressed: () {
                  navigateToPage();
               }),
        ],
     ),
    );
  }
}

You can run this code here on codepen.

0
On

First rap your Center with a Container.

Container(
  decoration:BoxDecoration(
   image:DecorationImage(
     fit:BoxFit.fill
     image: AssetImage("images/Ani.png"),
 )
    )
  child:Center(
  )
)

BoxFit.fill will make the image cover the container fully