Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I layout widgets based on the size of the parent?

Let's say you have a parent widget that might have a variable size.

For example:

var container = new Container(    height: 200.0, // Imagine this might change    width: 200.0, // Imagine this might change    // Imagine content in this container will     // depend on the parent container    child: new Container(),  ); 

And maybe you want to have the child of the parent container render something different based on the size that it's given.

Think of responsive design breakpoints, if the width is over X use this layout, if the width is under X use that layout.

What's the best way to do this in Flutter?

like image 866
Dvdwasibi Avatar asked Jan 09 '17 22:01

Dvdwasibi


People also ask

How do I find the size of a widget?

To get the size/position of a widget on screen, you can use GlobalKey to get its BuildContext to then find the RenderBox of that specific widget, which will contain its global position and rendered size.

What is widget layout?

The Layout Widget is a responsive container that allows you to separate a responsive container into sections. Layouts can be placed inside the responsive containers of other layouts to create more even sections.


2 Answers

You will want to use the LayoutBuilder widget which will build at layout time and provides the parent widget's constraints.

The LayoutBuilder takes in a build() function which has the the standard BuildContext along with the BoxConstraints as parameters that can be used to help dynamically render widgets based on size.

Let's build a simple example of widget that renders "LARGE" if the parent width is greater than 200px and "SMALL" if the parent width is less or equal to that.

var container = new Container(   // Toggling width from 100 to 300 will change what is rendered   // in the child container   width: 100.0,   // width: 300.0   child: new LayoutBuilder(     builder: (BuildContext context, BoxConstraints constraints) {       if(constraints.maxWidth > 200.0) {         return new Text('BIG');       } else {         return new Text('SMALL');       }     }   ), ); 
like image 146
Dvdwasibi Avatar answered Sep 20 '22 17:09

Dvdwasibi


Want to have a container that takes part of the available space in its percentage (meaning half the parent widget etc...)? then use FractionallySizedBox

Sizing a widget relative to its parent

FractionallySizedBox is a widget (a container) that lets its child have a widthFactor and a heightFactor. You can see this as a kind of scale value. So if we want to have a container that takes half of the available space horizontally and vertically, we would do the following:

@override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: Text("FractionallySizedBox"),       ),       body: FractionallySizedBox(         widthFactor: 0.5,         heightFactor: 0.5,         child: Container(           // this container won't be larger than           // half of its parent size         ),       ),     );   } 

A widget that sizes its child to a fraction of the total available space. For more details about the layout algorithm, see RenderFractionallySizedOverflowBox.

like image 36
Paresh Mangukiya Avatar answered Sep 22 '22 17:09

Paresh Mangukiya