I created a simple web app with Flutter.
The app layout is divided in two vertically zones: on the left there is a small top-bottom menu, on the right there is the main content inside a DataTable.
I wrapped the DataTable inside a SingleChildScrollView and everything worked fine.
The left side, on the other hand, keeps giving me a vertical overflow.
The tree on the left side is as follows:
Row(
children: [
//Left side menu
Flexible(
Container(
Column()
), //Container
), //Flexible
//Right side
Flexible(...),
],
), //Row
Obviously it is a very simplified version.
I've tried to insert a SingleChildScrollView in almost all places (Column, Container, Flexible), but it doesn't work and it always returns the error.
A RenderFlex overflowed by 174 pixels on the bottom.
Any solutions?
Thank you!
My approach would be to use MediaQuery to get current height and width of the web window. Then spilt the available width in the ratio of 2:7 maybe? You can play around with the ratio.
Now, lets keep the body of Scaffold to be Container with width as MediaQuery.of(context).size.width and height as ... .height. Now Row with childrens be 2 Expanded widgets with flex 2 and flex 7 respectively. Now we have to portion in the screen in the ratio 2:7.
On the left side, as you said, you need a menu, let it be
Expanded(
flex:2,
child: Column())
And on the other side be-
Expanded(
flex:7,
child: Container(
child: SingleChildScrollView() ///then add your stuff for scrolling purposes.
)
)
learn more about MediaQuery here
learn more about Expanded here
Related
I want to put my Text widget at the upper left side of my Homepage.
In your home page you are making the Column mainAxisSize min. This means the column takes up as little room as possible. I believe it is in the row widget of your main.dart file. Rows by default have cross axis alignment set to center. Try to remove mainAxisSize in your homepage Column or add CrossAxisAlignment.start to the main.dart Row.
Try the following code:
Align(
alignment: Alignment.topLeft,
child: Text(
…
),
),
I want to locate each widgets across the height of the screen without scroll widget (It means the total height of screen is always fixed), but do not know to implement those widget generally.
For example, height of Iphone 13 is 2532 pixel but 13 pro max is 2778.
If total height of screen is fixed (non allowed to scroll), all widgets should be more shorten (like 70%-80%)than former for pro max.
Should I use MediaQuery class or other responsive libraries generally?
Making the size of height of a button dynamic is not recommended. You can check telegram signup button the height will stay but the width will be dynamic and reduced x pixel from left or right using margin.
if you are trying to make a floating button like whatsapp which is no matter size of the screen the height and width will stay, you can customize it on floatingactionbutton()
but if you still want to customize the height you can use this logic:
var heightOfMyPhone = (MediaQuery.of(context).size.height);
InkWell(
onTap: (){
//code here
},
child: Container(
height: heightOfMyPhone > 300 ? (heightOfMyPhone*0.5) : 100,
width: MediaQuery.of(context).size.width,
padding:const EdgeInsets.fromLTRB(10, 10, 10, 10),
)),
I have a column with 2 children. The first child has a fixed height, and the second child has dynamic content.
I want the first child to be at the head of the screen, and the second child to be in the vertical middle.
So i added an empty third child and made the first and third child Flexible so that they shared vertical space equally:
Column(
children: [
Flexible(
child: Column(
children: [_firstChild()],
),
),
_secondChild(),
Flexible(child: Container()),
],
)
This works when the content of the second child is short.
But if the second child gets tall, it clips the first child:
Isn't the Flexible supposed to distribute only the empty space when fit: FlexFit.loose? I tried both the fit possibilities. I've tried to put the first child inside a SizedBox and an Align
I've tried to make the third child a Spacer. Nothing has worked so far. The empty third child is taking half of the remaining vertical space.
EDIT:
When the second child's height is too much to make it vertically centered without clipping the first child, i want it to just behave like a default Column with MainAxisAlignment.start (like the first image)
What about this:
Stack(children:[
Center(child: SecondChild()),
Align(alignment: Alignment.topCenter, child: FirstChild()),
])
What is best practice for creating a responsive layout? I know that you can use media queries to get sizing information to dictate what to do; e.g. different screens for desktop, tablet, phone.
However, is it common practice to use Expanded or Flex properties to ensure widgets grow or fill the appropriate screen sizes? As a new Flutter developer, trying to understand how the balance is struck on typical use cases.
There are multiple ways to make your UI responsive in Flutter, but just to name a few rules of thumb that will mostly get the job done:
MediaQuery
You can set some widget height or width based on a portion of the screen, for example, creating a SizedBox that will fill 50% of the screen both vertically and horizontally:
SizedBox(
height: MediaQuery.of(context).size.height * 0.5,
width: MediaQuery.of(context).size.width * 0.5,
)
There are other properties that might interest you in the MediaQuery such as the padding from the safe area viewport and so on. You can also find a good article about it here.
LayoutBuilder
One of the most interesting widgets when it comes to build layouts. It will provide you with the parent constraints so you can use it to dynamically adapt your UI.
For example, this will make your child (SizedBox) widget take the parent's maxWidth.
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints){
return SizedBox(
width: constraints.maxWidth
);
}
)
Some interesting article about LayoutBuilder can be found here.
Flex
By using Flex widgets such as Expanded and Flexible. When used in a Row or Column they'll dynamically adapt based on the constraints imposed by them and along with Column and Row alignments/size they are quite powerful enough to make your UI responsive.
For example, you can have a two Containers in one Row where one uses 1/4 of the view and the other takes 3/4 of the space available.
Row(
children: <Widget>[
Expanded(
flex: 1,
child: Container(),
),
Expanded(
flex: 3,
child: Container(),
),
]
)
Another great article about it can be found here.
Platform
Also, you can always lookup for the underlying platform to make some decisions by using the Platform class getters.
import 'dart:io';
if(Platform.isAndroid) {
print('Running on Android');
}
TL;DR: There are a lot of options that can be played together, you should always look for the best approach for each scenario.
In responsive UI we don’t use hard-coded values for dimension and positions. Use Sizer plugin to get the real time size of the window.
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.
I still think it is better to get the screen size using this commands. You have more control and and you can make your design responsive.
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
There is another package called size_configure that helps you to make your Flutter app responsive. Just import the package and then use it to make your app responsive.
use textSizeMultiplier to set Text size
use imageSizeMultiplier to set Image size
use heightMultiplier to set height size
use weightMultiplier to set weight size
For example:
If you want to set the text size to 28
Divide 28 to 7.9 (because it's text, so we use 'Vertical Block Size')
and then multiply it textSize Multiplier
var width = MediaQuery.of(context).size.width; //width of device
// now we can find the number x% which will return value 10 on multiplying
//by the width of device
var x = (1000/(width))/100;
var ten = width * x; //now ten contains value 10.
//now we can use this value ten like:
ten * 1.5 //returns 15
ten * 3.5 //returns 35
ten * 4 //returns 40
Container(
heigth: ten * 3.7, //height = 37
)
I'm trying to build a "semi-responsive" widget. It has a fixed number of relatively large children that always fit on the screen horizontally (A, B and C) and a variable list of small children that not always fit horizontally together with A, B, and C. My idea is to have the widget look like the first picture when the screen is wide, and as a second when the screen is more narrow, and as a third when there's no space for smaller widgets at all. What I'm getting is the third only. As if Wrap always tries to put as many children horizontally as possible. I want it to minimize width instead.
My pseudocode:
Card(
child: Wrap(
children: [
Row(children: [A(), B(), C()]),
Wrap(children: [1(), 2(), 3(), 4(), 5()]),
]
),
);
Any idea how to "squeeze" Wrap vertically? Using standard layout widgets if possible.
You can archive that using the alignment property in the Wrap widget.
Card(
child: Wrap(
alignment: WrapAlignment.end,
children: [
Row(children: [A(), B(), C()]),
1(), 2(), 3(), 4(), 5(),
],),
);
check it out in dartpad gist