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
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.
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.
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.
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.
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?
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.
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,
],
),
),
);
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
],
),
),
],
);
Try going to to your Scaffold (or use one) and set
resizeToAvoidBottomPadding = false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With