Flutter responsiveness problem on various mobile devices - flutter

I am a beginner to Flutter and I tried creating a dummy login page. When I tried to run the app it was flawless on Pixel 3 XL but it was not good in Pixel 2 XL. How would I manage the responsiveness? I came across the MediaQuery widget, but how could I create a completely responsive app for each device? There are many variations among today's mobile devices (like notched display, fullscreen, etc) and thereafter comes the tablets. How could I manage this? Should I write code for each and every resolution?
Here is my design on Pixel 3 XL and Pixel 2 XL

You can use SingleChildScrollView as parent,
SingleChildScrollView(
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
child: Stack(
),
),
);
Edit: Also remove fit: StackFit.expand, from Stack widget.

The reason behind this problem is the sizedbox widget with height 100px.
removing that and using mainaxisalignment as spaceevenly gives the required output.
Here is the modified code:
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
body: Stack(
fit: StackFit.expand,
children: <Widget>[
bgimage,
Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
logoimage,
userName(),
password(),
forgotPassword(),
loginButton(),
googleSignIn(),
signUp()
],
),
],
),
);
}
This solves the problem.

Best easiest way to make responsive UI for different screen size is Sizer plugin.
Responsive UI in any screen size device also tablet.
Check it this plugin ⬇️
https://pub.dev/packages/sizer
.h  - for widget height
.w  - for widget width
.sp - for font size
Use .h, .w, .sp after value like this ⬇️
Example:
Container(
height: 10.0.h, //10% of screen height
width: 80.0.w, //80% of screen width
child: Text('Sizer', style: TextStyle(fontSize: 12.0.sp)),
);
I have build many responsive UI with this plugin.

Use this package flutter_next using this you design responsive UI even for desktop web and everything and they have an demo also so you can check there

Related

How to locate the bottom widget absolutely?

Please refer to the Widget Text("HERE") and corresponding ScreenShot below.
When a keyboard appears from the bottom of device, the Widget Text("HERE") relatively moved to upper-side, hence I should care about overflow of whole widget size as well as size of user devices.
How can I locate this Widget absolutely, or should I always make all things (widget) scrollable to corresponds to any devices and also to avoid overflow problem ?
Stack(
children:[
,//omit
const Align(
alignment: Alignment.bottomCenter,
child: Text("HERE"),
)
]
)
first, make sure you constrained your Scaffold widget to the full-screen height, with MediaQuery:
ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.of(context).size.height,
),
child: Scaffold(// your screen code),
),
then wrap your Text with a Positioned widget, then set the bottom property to 0 or the value you want to bottom with:
// ...
child: Stack(
children:[
Positioned(
bottom: 0,
child: Text("HERE"),
)
]
)
now even the keyboard is on, the screen will not resize and the Text widget will stay forcelly in the bottom of screen.
just add this line in Scaffold
Scaffold(
...
resizeToAvoidBottomInset : ture
);
hope it helps :)

How to stop resizing of widgets when resizing browser page in Flutter Web

How do you stop the automatic resizing/responsiveness of widgets in Flutter Web.
Is there a way to use a Widget wrapped around the Scaffold, for example, that stops widgets from resizing when page window is resized?
I have a unique use case where responsiveness is not required (although I am aware that responsive design is best usually).
You can give a fix height and width of that particular widget like this
SizedBox(
height: 300,
width: 300,
child: your_widget,
);
And wrap parent widget with SingleChildScrollView
You can use kIsWeb this will identify if you are running on mobile or web app. You still have the same output for responsive layout on mobile and web but it will differ on running either web or mobile.
import 'package:flutter/foundation.dart';
kIsWeb ? WebWidget : MobileWidget
I know it is a bit late, but this is what worked for me:
return Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 1000, //fixed width that you need
child: <your widget that you don't want to resize>,
),
]
),
)

Flutter Row not taking full Scaffold height

I am working on the skeleton of a tablet app with flutter and I am facing, probably a very simple issue, but it is getting tricky for me to figure out what is going on here.
Basically the skeleton of the app should look something like this:
For that, I have written a simple Stateless class like this:
class BaseDetailsRoute extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
body: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(flex: 1, child: _selectionColumn()),
Flexible(flex: 7, child: _contentArea())
],
),
);
}
Widget _contentArea() => Container(color: Colors.orange,);
Column _selectionColumn() => Column(
children: <Widget>[
Expanded(child: Container(color: Colors.blue,))
],
);
}
Which is giving me ALMOST the expected result:
The little issue here is on the bottom of the screen, as you can see, in the Scaffold, I have put backgroundColor: Colors.grey, and, after rendering the Row in the screen, it seems like for some reason I cannot make it take the whole available height, and I still see a bit of the grey background color of the Scaffold on the bottom.
I have tried wrapping the whole Scaffold inside a SafeArea widget, but it did not work.
How can I fix this in an easy way, meaning not having to add a bunch of other widgets?
It's kind of weird but your code is working as intended on my side (For both Portrait and Landscape). What emulator are you using?
See Picture: Pixel 2 API 29 Emulator Portrait
Pixel 2 API 29 Emulator Lanscape
could you Please try
child: Column(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
and let me know is it working

How do I place a widget in the center of the screen while ignoring column?

There is a widget (for example text). In the real example it comes after another widget in Column. How to put it in the center of the screen? As I understand it, the Column does not. How can this be done? Or are there other ways to specify the location of items?
Column takes all the available space of the parent and by default aligns the items to start
You can change fix this changes the MainAxisAlignment to center.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("test"),
],
),
or changing parents height and move it to the center (be careful if you add many items it will cause overflow)
child: Center(
child: Container(
height: 25,
child: Column(
children: <Widget>[
Text("test"),
],
),
),
),
Using Stack widget you can do it:
var textEmpty = Text("Some Text");
Stack(children: <Widget>[
Column(children: <Widget>[
Container(
,margin: EdgeInsets.only(top: 100.0)
,child: Center(child: textEmpty, ),)],),
listView,
]);
Use Center Wigdet instead of Container. Hope this helps!
No worries where it comes from. It depends on the view where you have added it.
If you want to show in the center then just use Center widget
If you want to use Container widget then, you have to add the width and height both to double.infinite
If you want to use it in the same widget then you can use the Stack widget as well.
It totally depends on the requirement of the view and in which way you are tried to implement it.

Screen compatible to all devices in flutter

How to Adjust the screen to all android devices screens in flutter. I have checked in stackoverflow but unfortunately i haven't got any satisfying answer.
Basically, Flutter automatically sets the height and width depending on the device sizes wherever possible. Example - if you have use a list view having 100 items then some devices may show 5 items at a time and some may show 6 items. It is automatically done by Flutter.
The problem comes only when you specify an absolute value for height and/or width. Let's say you've a widget and you specify width as 450. Now, it may fit on the bigger screens but on the small screen (e.g. width 400 points)then you'll see pixel overflow error in the UI.
Now, to solve this problem, you can use MediaQuery.of(context).size.height/width as suggested by LGM.
I think you have some questions about responsive layouts, so i will give you some examples with MediaQuery:
With MediaQuery you can get the device screen width:
double width = MediaQuery.of(context).size.width;
The height:
double height = MediaQuery.of(context).size.height;
as well as the orientation:
Orientation orientation = MediaQuery.of(context).orientation;
and also various information about the device, and then, you can base the layout on orientation, size etc.
Here's an example:
Container(
width: MediaQuery.of(context).orientation == Orientation.portrait ?
MediaQuery.of(context).size.width / 1.3 : MediaQuery.of(context).size.width / 2.1,
child: RaisedButton(
elevation: 0.0,
color: Colors.green,
child: Text("CONTINUE", style: TextStyle(color: Colors.white),),
onPressed: (){
//code of onPressed
);
}
),
),
You can use too Align, to define where the widget should be, Positioned, the properties of Column, Center etc.
One more example, using FittedBox to deal with texts:
#override
Widget build(BuildContext context) {
return Material(
child: SafeArea(
child: Scaffold(
body: ListView(
children: <Widget>[
FittedBox(child: Text("Test"),),
FittedBox(child: Text("Flutter Flutter Flutter"),)
],
),
),
),
);
}
The result:
Portrait:
Landscape: