0

I am new here so I apologise in advance for any mistakes that might violate the rules of this platform.

I have a problem with filling my android app with a background image. I've tried to follow this, but I got a rendering exception.

The code, which doesn't use the [SizedBox] class looks like this:

return Scaffold(
      appBar: new AppBar(title: Text("Progresso nel edilizia")),

      body: SingleChildScrollView(
          child: Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/login_background.jpg'),
                fit: BoxFit.cover
              )
            ),
            child: LoginBody(),
        ) 
      ),
    );

The output: https://ibb.co/0srf6dN

3 Answers3

0

Try below code

   return Scaffold(
  body: Container(
    decoration: BoxDecoration(
      image: DecorationImage(
        image: AssetImage("assets/login_background.jpg"),
        fit: BoxFit.cover,
      ),
    ),
  ),
);
Dharmender Manral
  • 1,504
  • 1
  • 6
  • 7
  • It works, but if I get rid of [SingleChildScrollView] I get a rendering exception whenever I press the password textfield ( image of the UI in the post ). – justanewbie Jun 14 '19 at 12:17
0

Try this but make sure the image you are using has a correct aspect ratio to fit the height.

@override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(title: Text("Progresso nel edilizia")),
      body: SingleChildScrollView(
          child: Container(
        height: MediaQuery.of(context).size.height,
        decoration: const BoxDecoration(
            image: DecorationImage(
                image: AssetImage('assets/login_background.jpg'),
                fit: BoxFit.fitHeight)),
        child: Container(),
      )),
    );
  }
Sergio Bernal
  • 2,126
  • 9
  • 20
0

Use a Stack with a container using the full width and height of the screen on the background. Like this:

return Scaffold(
  appBar: AppBar(title: Text("Progresso nel edilizia")),
  body: Stack(
    children: <Widget>[
      Container(
        width: double.infinity,
        height: double.infinity,
        decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/login_background.jpg'),
            fit: BoxFit.cover,
          ),
        ),
      ),
      SingleChildScrollView(
        child: LoginBody(),
      ),
    ],
  ),
);