Skip to main content

Flex

Struct Flex 

Source
pub struct Flex {
    pub key: Option<KeyRef>,
    pub direction: Axis,
    pub main_axis_alignment: MainAxisAlignment,
    pub main_axis_size: MainAxisSize,
    pub cross_axis_alignment: CrossAxisAlignment,
    pub text_direction: Option<TextDirection>,
    pub vertical_direction: VerticalDirection,
    pub text_baseline: Option<TextBaseline>,
    pub clip_behavior: Clip,
    pub spacing: f64,
    pub children: Vec<WidgetRef>,
}
Expand description

A widget that displays its children in a one-dimensional array.

The Flex widget allows you to control the axis along which the children are placed (horizontal or vertical). This is referred to as the main axis. If you know the main axis in advance, then consider using a Row (if it’s horizontal) or Column (if it’s vertical) instead, because that will be less verbose.

To cause a child to expand to fill the available space in the direction of this widget’s main axis, wrap the child in an Expanded widget.

The Flex widget does not scroll (and in general it is considered an error to have more children in a Flex than will fit in the available room). If you have some widgets and want them to be able to scroll if there is insufficient room, consider using a ListView.

If you only have one child, then rather than using Flex, Row or Column, consider using Align or Center to position the child.

§Layout algorithm

This section describes how a Flex is rendered by the framework.

Layout for a Flex proceeds in six steps:

  1. Layout each child with a None or zero flex factor with unbounded main axis constraints and the incoming cross axis constraints. If the cross_axis_alignment is CrossAxisAlignment::Stretch, instead use tight cross axis constraints that match the incoming max extent in the cross axis.
  2. Divide the remaining main axis space among the children with non-zero flex factors according to their flex factor.
  3. Layout each of the remaining children with the same cross axis constraints as in step 1, but instead of using unbounded main axis constraints, use max axis constraints based on the amount of space allocated in step 2.
  4. The width of the Flex is determined by the main_axis_size property.
  5. Determine the position for each child according to the main_axis_alignment and the cross_axis_alignment.

See also:

  • Row, for a version of this widget that is always horizontal.
  • Column, for a version of this widget that is always vertical.
  • Expanded, to indicate children that should take all the remaining room.
  • Flexible, to indicate children that should share the remaining room.
  • Spacer, a widget that takes up space proportional to its flex value.

Fields§

§key: Option<KeyRef>§direction: Axis

The direction to use as the main axis.

If you know the axis in advance, then consider using a Row (if it’s horizontal) or Column (if it’s vertical) instead of a Flex, since that will be less verbose.

§main_axis_alignment: MainAxisAlignment

How the children should be placed along the main axis.

For example, MainAxisAlignment::Start, the default, places the children at the start (i.e., the left for a Row or the top for a Column) of the main axis.

§main_axis_size: MainAxisSize

How much space should be occupied in the main axis.

After allocating space to children, there might be some remaining free space. This value controls whether to maximize or minimize the amount of free space, subject to the incoming layout constraints.

§cross_axis_alignment: CrossAxisAlignment

How the children should be placed along the cross axis.

For example, CrossAxisAlignment::Center, the default, centers the children in the cross axis (e.g., horizontally for a Column).

§text_direction: Option<TextDirection>

Determines the order to lay children out horizontally and how to interpret start and end in the horizontal direction.

Defaults to the ambient Directionality.

If the direction is Axis::Horizontal, this controls the order in which the children are positioned (left-to-right or right-to-left), and the meaning of the main_axis_alignment property’s MainAxisAlignment::Start and MainAxisAlignment::End values.

If the direction is Axis::Vertical, this controls the meaning of the cross_axis_alignment property’s CrossAxisAlignment::Start and CrossAxisAlignment::End values.

§vertical_direction: VerticalDirection

Determines the order to lay children out vertically and how to interpret start and end in the vertical direction.

Defaults to VerticalDirection::Down.

§text_baseline: Option<TextBaseline>

If aligning items according to their baseline, which baseline to use.

This must be set if using baseline alignment. There is no default because there is no way for the framework to know the correct baseline a priori.

§clip_behavior: Clip

Defaults to Clip::None.

§spacing: f64

How much space to place between children in the main axis.

§children: Vec<WidgetRef>

Implementations§

Source§

impl Flex

Source

pub fn new(direction: Axis) -> Flex

Creates a flex layout; Dart’s optional named arguments are the setters.

The direction is required.

The text_direction defaults to the ambient Directionality, if any. If there is no ambient directionality, and a text direction is going to be necessary to decide which direction to lay the children in or to disambiguate start or end values for the main or cross axis directions, the text_direction must not be None.

Source

pub fn key(self, key: KeyRef) -> Flex

Dart Flex(key:).

Source

pub fn main_axis_alignment(self, main_axis_alignment: MainAxisAlignment) -> Flex

Dart Flex(mainAxisAlignment:).

Source

pub fn main_axis_size(self, main_axis_size: MainAxisSize) -> Flex

Dart Flex(mainAxisSize:).

Source

pub fn cross_axis_alignment( self, cross_axis_alignment: CrossAxisAlignment, ) -> Flex

Dart Flex(crossAxisAlignment:).

Source

pub fn text_direction(self, text_direction: TextDirection) -> Flex

Dart Flex(textDirection:).

Source

pub fn vertical_direction(self, vertical_direction: VerticalDirection) -> Flex

Dart Flex(verticalDirection:).

Source

pub fn text_baseline(self, text_baseline: TextBaseline) -> Flex

Dart Flex(textBaseline:).

Source

pub fn clip_behavior(self, clip_behavior: Clip) -> Flex

Dart Flex(clipBehavior:).

Source

pub fn spacing(self, spacing: f64) -> Flex

Dart Flex(spacing:).

Source

pub fn children(self, children: impl IntoIterator<Item = WidgetRef>) -> Flex

Dart Flex(children:).

Source

pub fn get_effective_text_direction( &self, app: &mut App, context: BuildContext, ) -> Option<TextDirection>

The value to pass to RenderFlex::set_text_direction.

This value is derived from the text_direction property and the ambient Directionality. The value is None if there is no need to specify the text direction. In practice there’s always a need to specify the direction except for vertical flexes (e.g. Columns) whose cross_axis_alignment is not dependent on the text direction (not start or end). In particular, a Row always needs a text direction because the text direction controls its layout order. (For Columns, the layout order is controlled by vertical_direction, which is always specified as it does not depend on an inherited widget and defaults to VerticalDirection::Down.)

Trait Implementations§

Source§

impl Debug for Flex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl MultiChildRenderObjectWidget for Flex

Source§

fn children(&self) -> &[WidgetRef]

The widgets below this widget in the tree. Read more
Source§

fn create_element(&self, app: &mut App, this: WidgetRef) -> AnyElement
where Self: Sized,

Creates the element that manages this widget’s children. Read more
Source§

impl RenderObjectWidget for Flex

Source§

type RenderObject = RenderFlex

The render object this widget creates; Dart’s covariant RenderObject parameter type.
Source§

fn key(&self) -> Option<&KeyRef>

Source§

fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )

Dart’s debugFillProperties: the properties a dump shows for this widget, in Dart’s order. Dart’s Widget base only sets the dense tree style, which the framework does.
Source§

fn create_render_object( &self, app: &mut App, context: BuildContext, ) -> AnyRenderObject

Creates an instance of the RenderObject class that this widget represents, using the configuration described by this widget. Read more
Source§

fn update_render_object( &self, app: &mut App, context: BuildContext, render_object: RenderHandle<RenderFlex>, )

Copies the configuration described by this widget to the given render object, which will be of the same type as returned by this object’s create_render_object. Read more
Source§

fn to_string_short(&self) -> String

Dart’s toStringShort, for a widget that names itself apart from its type, as SizedBox.expand does; see Widget::to_string_short.
Source§

fn debug_check_constructor(&self)

Source§

fn debug_visit_onstage_children( &self, app: &App, children: &[AnyElement], visitor: &mut dyn FnMut(AnyElement), )

Dart’s debugVisitOnstageChildren of the element this widget makes, for a widget whose Dart element subclass overrides it only to hide children (_OffstageElement, _IndexedStackElement): calls visitor for each of children, in order, that is onstage. Waft’s generic single- and multi-child elements stand in for those subclasses and ask their widget here; an element of a widget’s own overrides Element::debug_visit_onstage_children instead.
Source§

fn did_unmount_render_object( &self, app: &mut App, render_object: RenderHandle<Self::RenderObject>, )

A render object previously associated with this widget has been removed from the tree. The given RenderObject will be of the same type as returned by this object’s create_render_object.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Flex

§

impl !Send for Flex

§

impl !Sync for Flex

§

impl !UnwindSafe for Flex

§

impl Forget for Flex

§

impl Freeze for Flex
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const u8 is !null: Freeze, PhantomData<Rc<dyn Widget>>: Freeze,

§

impl Move for Flex

§

impl Unpin for Flex
where Rc<dyn Key>: Unpin, *const u8 is !null: Unpin, Rc<dyn Widget>: Unpin,

§

impl UnsafeUnpin for Flex
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<Rc<dyn Widget>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<W> IntoWidget<MultiChildRenderObjectKind> for W

Source§

fn into_widget(self) -> Rc<dyn Widget>

The widget as a tree node, made where this is called: its creation_location.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>