Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep widget from filling ancestor Expanded in Flutter

How do I keep a RaisedButton from expanding to fill an Expanded that contains it? I want to create three columns of widths proportional to the space available, but I want the child in each column to be its natural size, without consuming the entire width of its parent Expanded.

  Widget _controls(BuildContext cx) {
    return Row(
      children: [
        Expanded(
          flex: 1,
          child: player.isOnFirstItem
              ? Container()
              : IconButton(
                  icon: Icon(Icons.arrow_back),
                  onPressed: () => control.gotoPreviousItem(),
                ),
        ),
        Expanded(
          flex: 4,
          child: player.isComplete()
              ? Text(player.currentItem.status) // I'll be adding more here
              : RaisedButton(
                  child: Text(player.currentItem.actionText),
                  onPressed: () => player.currentItem.action(),
                ),
        ),
        Expanded(
          flex: 1,
          child: player.isOnLastItem
              ? Container()
              : IconButton(
                  icon: Icon(Icons.arrow_forward),
                  onPressed: () => player.gotoNextItem(),
                ),
        ),
      ],
    );

The RaisedButton also fills the flex space when I further nest it within a Container. I can't figure out which properties might prevent this.

Searching around for answers, it seems that everyone is trying to accomplish just the opposite.

like image 280
Joe Lapp Avatar asked Oct 26 '19 13:10

Joe Lapp


People also ask

How do I stop my screen from overflowing my Flutter?

Introducing Flexible: Flexible widget can be placed inside Column, Row and similar widgets to surround any of their children. Flexible helps the child to avoid overflow by displaying it only in the available space.

How do you fix incorrect use of ParentDataWidget in Flutter?

The fix for this above issue, is to move the expanded widget inside a flex widget. Example below. Tags: Incorrect use of ParentDataWidget. Click here to check out more details on the Free Flutter Course.

What is the difference between expanded and flexible widgets in Flutter?

Flexible is a built-in widget in flutter which controls how a child of base flex widgets that are Row, Column, and Flex will fill the space available to it. The Expanded widget in flutter is shorthand of Flexible with the default fit of FlexFit.


Video Answer


1 Answers

Look what the documentation of Expanded says:

A widget that expands a child of a Row, Column, or Flex so that the child fills the available space.

So, the RaisedButton will fill the available space.

You tried to wrap the button with Container, but look what the documentation of Container says in case it has a child:

(in that case) the widget has a child but no height, no width, no constraints, and no alignment, and the Container passes the constraints from the parent to the child and sizes itself to match the child.

So, the RaisedButton will take the constraints from the parent of the Container, which is to fill the available space.

What you need is to wrap the RaisedButton with a widget that doesn't size itself to match the child's size (because it won't expand) and doesn't alter the size of the child (because the child could expand).

So, some widgets to wrap the RaisedButton that meets these conditions could be:

  • Row
  • Wrap
  • UnconstrainedBox
  • Column with mainAxisSize: MainAxisSize.min
  • Center with heightFactor: 1.0.
  • Align with heightFactor: 1.0.
like image 95
Pablo Barrera Avatar answered Oct 11 '22 01:10

Pablo Barrera