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

前端 未结 3 1088
粉色の甜心
粉色の甜心 2020-12-13 01:46

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 migh         


        
相关标签:
3条回答
  • 2020-12-13 02:01

    So I had an interesting situation where my parent child was increasing size dynamically based on the content (Text containing description). This parent widget was displaying content in read only mode but this solution might resolve other problems as well where you increase textfield height dynamically.

    I had a height variable and GlobalKey. In initState of widget I am using SchedulerBinding which runs it's addPostFrameCallback after layout is build.

    The globalKey is assigned to any parent widget whose height we need for it's children.

    double height = 0.0;
    GlobalKey _globalKey = GlobalKey();
    
      @override
      void initState() {
        SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
          height = _globalKey.currentContext.size.height;
          print('the new height is $height');
          setState(() {});
        });
        super.initState();
      }
    

    and rest code looks like this

    // this parent having dynamic height based on content 
    Container(
      width: 100.0,
      key: _globalKey,
      child: Container(
        width: 100.0,
        child: Row(children: [  ///  you can have column as well or any other widget. 
          Container(child: ...),
          Container(height: height,), ///this widget will take height from parent 
        ],),
      )
    )
    
    0 讨论(0)
  • 2020-12-13 02:17

    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');
          }
        }
      ),
    );
    
    0 讨论(0)
  • 2020-12-13 02:17

    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.

    0 讨论(0)
提交回复
热议问题