flutterdartflutter-widget

The equivalent of wrap_content and match_parent in flutter?


In Android match_parent and wrap_content are used to resize the widgets automatically relative to their parent to the content the widget contains.

In Flutter it seems by default all widgets are set to wrap_content, how would I change it such that I can fill its width and height to that of its parent?


Solution

  • Note: Default behaviour of parent widget is to fill all the space available with its child. Scafold>body:Container(sized) is different the Scafold>body:Center>Container(Sized) You can read more about it https://docs.flutter.dev/ui/layout/constraints

    Understanding This will will clear all you concept how widget works : Constrains go down sizes go up parent set position

    You can do with little Trick: Suppose you have requirement of : ( Width,Height )

    Wrap_content ,Wrap_content :

     //use this as child
     Wrap(
      children: <Widget>[*your_child*])
    

    Match_parent,Match_parent:

     //use this as child
    Container(
            height: double.infinity,
        width: double.infinity,child:*your_child*)
    

    Match_parent,Wrap_content :

     //use this as child
    Row(
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[*your_child*],
    );
    

    Wrap_content ,Match_parent:

     //use this as child
    Column(
      mainAxisSize: MainAxisSize.max,
      children: <Widget>[your_child],
    );
    

    Note: In order to understand how a widget lay its children && repaint you can look into package be_widgets source code I developed it to reduce the nesting widget tree & improve performance.