Skip to content

Instantly share code, notes, and snippets.

@appcypher
Last active October 7, 2023 11:14
Show Gist options
  • Save appcypher/5b606975b8dfc8707212b2ba77b4f385 to your computer and use it in GitHub Desktop.
Save appcypher/5b606975b8dfc8707212b2ba77b4f385 to your computer and use it in GitHub Desktop.
Flutter Notes

Flutter Notes

Flex (Columns & Rows)

When you have a Flex widget, where the child with the maximum cross-axis size determines the cross-axis size of the rest.

If you try to use CrossAxisAlignment.stretch with a Flex whose parent is unbounded along the cross-axis, you will get an error.

To solve this, you should use Intrinsic* widgets. They do speculative pass on the Flex widget tree to determine the max size along the cross-axis and that becomes the constraint they pass to the Flex.

When you have a Flex widget, where one of the child is unbounded along the main axis

A Flex resolves its main axis size based on its children. It is able to do that by having children that are either sized or have non-zero flex factor. So it is unable to handle children like another Flex with the same direction, ScrollableView along the same axis, Container with no max value in the same axis, etc.

You need to wrap those in a widget with a flex factor like Expanded, Flexible or just give them a size with SizedBox.

When you have a Flexible or Expanded child in a Flex whose parent gives it unconstrained value for its main axis

When a Flex is inside a parent that is sends it unconstrained value for its main axis, the Flex tries to shrink wrap to its children. This cannot work with a child that tries to expand to as much space as possible. In this case one would think Flexible should work but it doesn't.

Stacks

Stacks and Positioned widget with unbounded axis.

Say you have a Positioned widget that is unbounded along a particular axis. Let's say the width. If you need to put that in a Stack, you need to set the left and right of the widget. They act as margins in that sense.

Stacks must contain at least a non-positional widget to use Positioned children

If your stack does not have a non-positional widget and you decide to put positional widgets, the stack won't have a reference frame to work with. In which case it is going to throw an error.

TextField and TextFormField

Remove padding from TextField and TextFormField

Set the decoration of the TextField or TextFormField to the following:

decoration: const InputDecoration(isDense: true, contentPadding: EdgeInsets.zero)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment