Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a persistent background image while staying responsive in Flutter

I'm creating a login screen, and i have this background image, the problem is when the user clicks one of the TextFields and the keyboard pops, the background image changes its size to fit the new screen size (excluding the keyboard).

I want the background to stay persistent and the same size, i would use BoxFit.none, but i'm afraid it will hurt the responsiveness of the app.

Here's the code:

new Container(
      decoration: new BoxDecoration(
          color: Colors.red,
          image: new DecorationImage(
              fit: BoxFit.cover,
              image: new AssetImage(
                  'assets/images/splash_screen/background.png'))),
      child: new Center(
        child: new ListView(
          physics: new PageScrollPhysics(),
          children: <Widget>[ //Login screen content ],
        ),
      ),
    );

I also tried to define BoxConstraints with minHeight of the device screen but it doesn't help, and used Stack as well but with not luck.

Here's what i mean by changing dimensions: No Keyboard / With Keyboard

like image 319
argamanza Avatar asked Mar 26 '18 19:03

argamanza


People also ask

How do you scaffold a background image in Flutter?

To set background image in Flutter, you can use the Container widget. The Container widget has a property called decoration. Then you can use the BoxDecoration class to provide the actual image. Before directly setting the background image in full screen, let's see the basics of adding a background image.

How do I make the background image full screen on Flutter?

You can use Stack to make the image stretch to the full screen. Stack( children: <Widget> [ Positioned. fill( // child: Image( image: AssetImage('assets/placeholder. png'), fit : BoxFit.

How to set a background image in flutter?

A common way to set a background image in Flutter applications is by using DecorationImage. Below are the examples which include how to set the fit mode, transparency, and prevent image resizing when the keyboard is shown. You may already be familiar with Container widget.

What is flutter responsive design and how to use it?

Flutter allows you to create apps that self-adapt to the device’s screen size and orientation. There are two basic approaches to creating Flutter apps with responsive design: From its builder property, you get a BoxConstraints object. Examine the constraint’s properties to decide what to display.

What is flutteragency?

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide , Flutter Projects , Code libs and etc. How to set up an Emulator For VSCode? (Updated) How to Format DateTime In Flutter ? How to Solve Unable to load asset in Flutter?

What is flutter and why should I use it?

One of Flutter’s primary goals is to create a framework that allows you to develop apps from a single codebase that look and feel great on any platform. This means that your app may appear on screens of many different sizes, from a watch, to a foldable phone with two screens, to a high def monitor.


3 Answers

Put your Scaffold as a child of a Container and make it transparent

final emailField = TextFormField(
  decoration: InputDecoration(
    hintText: "Email",
  ),
);

return Container(
  decoration: BoxDecoration(
    image: DecorationImage(
      image: AssetImage('assets/bg.png'),
      fit: BoxFit.cover,
    ),
  ),
  child: Scaffold(
    backgroundColor: Colors.transparent,
    body: ListView(
      children: <Widget>[
        emailField,
      ],
    ),
  ),
);
like image 190
Sinan Baymammadli Avatar answered Oct 10 '22 12:10

Sinan Baymammadli


Try using a Stack, with your image in a Positioned, with a BoxFit of fill. Then, set top: 0.0. This way, its height shouldn't be influenced by the height of the bottom of the screen (i.e. it shouldn't change when the keyboard comes up), and its size should remain the same. Example:

return Stack(
  children: <Widget>[
    Positioned(
      top: 0.0,
      child: Image.asset(
        'assets/images/splash_screen/background.png',
        fit: BoxFit.fill,
      ),
    ),
    Center(
      child: ListView(
        physics: PageScrollPhysics(),
        children: <Widget>[ 
           //Login screen content
        ],
      ),
    ),
  ],
);
like image 6
Mary Avatar answered Oct 10 '22 11:10

Mary


Try going to to your Scaffold (or use one) and set resizeToAvoidBottomPadding = false

like image 4
zuko Avatar answered Oct 10 '22 11:10

zuko