LayoutBuilder (Flutter Widget of the Week)
Why Stack Overflow recommends it
Stack Overflow users link it when answering Flutter questions about reading a parent's size or building responsive layouts, as a quick official intro to LayoutBuilder and its constraints.
Recommended by 3 people across 3 different months, 2019 to 2023.
What it is
A 47-second entry in Google's Flutter Widget of the Week series covering LayoutBuilder. It explains that the widget's builder receives a BuildContext and the incoming BoxConstraints, so you can see how much space a widget has before deciding how to lay it out.
What people said
Recommends Fellipe Malta · 2019-05 · Stack Overflow · on “How to resize column in row for responsive screen?”
You need to know the LayoutBuilder component, which is what you want and fixes your needs. LayoutBuilder takes two parameters, one is a context , the second is a constraints . With those constraints you can set show different layouts for different screens. Check this example: LayoutBuilder( builder: (context, constraints) { if(constraints.maxHeight < 768 && constraints.maxWidth < 1270) { return smallDesignContent(); } else { return bigDefaultDesignContent(); } }, ) Flutter Widget of the Week - Layout Builder [this video] Documentation [LINK]
Recommends Sanal Kv · 2019-12 · Stack Overflow · on “How to get Screen size in Flutter when my app running in Split screen mode”
You can use LayoutBuilder inside Scaffold and print the size using constraints property. It will print whenever the screen size changes. For more visit LayoutBuilder [this video] LayoutBuilder(builder: (builder, constraints) { print(constraints.maxWidth); }
Recommends MickaelHrndz · 2021-07 · Stack Overflow · on “Structure of a flutter project”
You might want to place that MaterialApp into a separate parent widget (I think it will cause issues when using MediaQuery and Theme inside the same build method). It also might be cleaner down the line to extract every part (menu, body, ListProducts) into separate widgets. Also, I would advise you to take a look at the LayoutBuilder [this video] widget, and the ressources on this page [LINK] if the app is meant to work on narrower screens. Oh and if you don't know about state management, definitely check this out [LINK] .
Cites Doc · 2019-04 · Stack Overflow · on “proper way to get widget height in SafeArea”
You can always use LayoutBuilder for such cases. child: SafeArea( child: new LayoutBuilder( builder: (BuildContext context, BoxConstraints constraints) { // constraints variable has the size info return Container(); } ), ), for more info: [this video]
Cites Doc · 2019-04 · Stack Overflow · on “How to get the height for the system navigation bar?”
If your Widget s need to know the size available to them then you can wrap them in LayoutBuilder which will have the width and height available to you Widget Watch official sample here: [this video] Some examples: How can I layout widgets based on the size of the parent? [LINK] [LINK] But if want to know the AppBar height then AppBar height in Flutter [LINK]
Cites CoderUni · 2021-03 · Stack Overflow · on “How to make Flutter web and desktop app look exactly the same scale”
I don't get what you mean by "something that doesn't require changes to every place sizes are used". Are you looking for something like this: [this video]
Cites MaNDOOoo · 2022-04 · Stack Overflow · on “MediaQuery.of() Flutter”
The MediaQuery.of(context) returns the device's screen size, not the parent widget. You can get the size of the parent widget by using the LayoutBuilder . [this video] LayoutBuilder( builder: (context, constraints) { constraints.maxWidth; ... } );
Cites MindStudio · 2022-09 · Stack Overflow · on “I need to set my box height flexible to fit any size of screen”
LayoutBuilder() Provides a way to get the actual screen size dynamically and allows you to calculate relative sizes: [this video] Generally when using Rows and Columns you can use Expanded widgets to have relative instead of absolute sizes: Row( children: [ Expanded(flex: 1, child: Container()), // This will take up 1 third of the screen Expanded(flex: 2, child: Container()), // This will take up 2 thirds of the screen ] ),
Cites Tim Brückner · 2023-07 · Stack Overflow · on “getting " 'hasSize': RenderBox was not laid out:" . wraping listview builder with expanded doesn't work. how to fix it?”
In case the number of children is not too big, replace the ListView by a Column . The layout of the column gets calculated beforehand and therefore should have a known height. If the number of children is indeed very large, then continue using the ListView, but you have to use constraints at some point, you may try to use a combination of LayoutBuilder and SizedBox . [this video]