Skip to main content

Container

Struct Container 

Source
pub struct Container {
Show 13 fields pub key: Option<KeyRef>, pub alignment: Option<AlignmentGeometry>, pub padding: Option<EdgeInsetsGeometry>, pub color: Option<AnyColor>, pub is_anti_alias: bool, pub decoration: Option<DecorationRef>, pub foreground_decoration: Option<DecorationRef>, pub constraints: Option<BoxConstraints>, pub margin: Option<EdgeInsetsGeometry>, pub clip_behavior: Clip, pub transform: Option<Matrix4>, pub transform_alignment: Option<AlignmentGeometry>, pub child: Option<WidgetRef>, /* private fields */
}
Expand description

A convenience widget that combines common painting, positioning, and sizing widgets.

A container first surrounds the child with padding (inflated by any borders present in the decoration) and then applies additional constraints to the padded extent (incorporating the width and height as constraints, if either is set). The container is then surrounded by additional empty space described from the margin.

During painting, the container first applies the given transform, then paints the decoration to fill the padded extent, then it paints the child, and finally paints the foreground_decoration, also filling the padded extent.

Containers with no children try to be as big as possible unless the incoming constraints are unbounded, in which case they try to be as small as possible. Containers with children size themselves to their children. The width, height, and constraints arguments to the constructor override this.

By default, containers return false for all hit tests. If the color property is specified, the hit testing is handled by ColoredBox, which always returns true. If the decoration or foreground_decoration properties are specified, hit testing is handled by Decoration::hit_test.

§Layout behavior

See BoxConstraints for an introduction to box layout models.

Since Container combines a number of other widgets each with their own layout behavior, Container’s layout behavior is somewhat complicated.

Summary: Container tries, in order: to honor alignment, to size itself to the child, to honor the width, height, and constraints, to expand to fit the parent, to be as small as possible.

More specifically:

If the widget has no child, no height, no width, no constraints, and the parent provides unbounded constraints, then Container tries to size as small as possible.

If the widget has no child and no alignment, but a height, width, or constraints are provided, then the Container tries to be as small as possible given the combination of those constraints and the parent’s constraints.

If the widget has no child, no height, no width, no constraints, and no alignment, but the parent provides bounded constraints, then Container expands to fit the constraints provided by the parent.

If the widget has an alignment, and the parent provides unbounded constraints, then the Container tries to size itself around the child.

If the widget has an alignment, and the parent provides bounded constraints, then the Container tries to expand to fit the parent, and then positions the child within itself as per the alignment.

Otherwise, 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.

The margin and padding properties also affect the layout, as described in the documentation for those properties. (Their effects merely augment the rules described above.) The decoration can implicitly increase the padding (e.g. borders in a BoxDecoration contribute to the padding); see Decoration::padding.

§Example

This example shows a 48x48 amber square (placed inside a Center widget in case the parent widget has its own opinions regarding the size that the Container should take), with a margin so that it stays away from neighboring widgets:

Center::new().child(
    Container::new()
        .margin(EdgeInsetsGeometry::all(10.0))
        .color(AMBER_600)
        .width(48.0)
        .height(48.0),
)

See also:

  • AnimatedContainer, a variant that smoothly animates the properties when they change.
  • Border, which has a sample which uses Container heavily.
  • Ink, which paints a [Decoration] on a Material, allowing InkResponse and InkWell splashes to paint over them.
  • The catalog of layout widgets.

Flutter’s clipBehavior waits on ClipPath.

Fields§

§key: Option<KeyRef>§alignment: Option<AlignmentGeometry>

Align the child within the container.

If non-None, the container will expand to fill its parent and position its child within itself according to the given value. If the incoming constraints are unbounded, then the child will be shrink-wrapped instead.

Ignored if child is None.

See also:

  • Alignment, a class with convenient constants typically used to specify an AlignmentGeometry.
  • AlignmentDirectional, like Alignment for specifying alignments relative to text direction.
§padding: Option<EdgeInsetsGeometry>

Empty space to inscribe inside the decoration. The child, if any, is placed inside this padding.

This padding is in addition to any padding inherent in the decoration; see Decoration::padding.

§color: Option<AnyColor>

The color to paint behind the child.

This property should be preferred when the background is a simple color. For other cases, such as gradients or images, use the decoration property.

If the decoration is used, this property must be None. A background color may still be painted by the decoration even if this property is None.

§is_anti_alias: bool§decoration: Option<DecorationRef>

The decoration to paint behind the child.

Use the color property to specify a simple solid color.

The child is not clipped to the decoration. To clip a child to the shape of a particular ShapeDecoration, consider using a ClipPath widget.

§foreground_decoration: Option<DecorationRef>

The decoration to paint in front of the child.

§constraints: Option<BoxConstraints>

Additional constraints to apply to the child.

The width and height setters are combined with the given constraints: this is Dart’s folded field.

The padding goes inside the constraints.

§margin: Option<EdgeInsetsGeometry>

Empty space to surround the decoration and child.

§clip_behavior: Clip

The clip behavior when decoration is not None.

Defaults to Clip::None. Must be Clip::None if decoration is None.

If a clip is to be applied, the Decoration::get_clip_path method for the provided decoration must return a clip path. (This is not supported by all decorations; the default implementation of that method throws an UnsupportedError.)

§transform: Option<Matrix4>

The transformation matrix to apply before painting the container.

§transform_alignment: Option<AlignmentGeometry>

The alignment of the origin, relative to the size of the container, if transform is specified.

When transform is None, the value of this property is ignored.

See also:

§child: Option<WidgetRef>

The child contained by the container.

If None, and if the constraints are unbounded or also None, the container will expand to fill all available space in its parent, unless the parent provides unbounded constraints, in which case the container will attempt to be as small as possible.

Implementations§

Source§

impl Container

Source

pub fn new() -> Container

Creates a widget that combines common painting, positioning, and sizing widgets; Dart’s named arguments are the setters.

The height and width values include the padding.

The color and decoration arguments cannot both be supplied, since it would potentially result in the decoration drawing over the background color. To supply a decoration with a color, use .decoration(BoxDecoration::new().color(color)).

Source

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

Dart Container(key:).

Source

pub fn alignment(self, alignment: AlignmentGeometry) -> Container

Dart Container(alignment:).

Source

pub fn padding(self, padding: impl Into<EdgeInsetsGeometry>) -> Container

Dart Container(padding:); must be non-negative.

Source

pub fn color(self, color: impl Into<AnyColor>) -> Container

Dart Container(color:); cannot be combined with decoration.

Source

pub fn is_anti_alias(self, is_anti_alias: bool) -> Container

Dart Container(isAntiAlias:).

Source

pub fn decoration(self, decoration: impl Into<DecorationRef>) -> Container

Dart Container(decoration:); cannot be combined with color.

Source

pub fn foreground_decoration( self, foreground_decoration: impl Into<DecorationRef>, ) -> Container

Dart Container(foregroundDecoration:).

Source

pub fn width(self, width: f64) -> Container

Dart Container(width:): tightens constraints.

Source

pub fn height(self, height: f64) -> Container

Dart Container(height:): tightens constraints.

Source

pub fn constraints(self, constraints: BoxConstraints) -> Container

Dart Container(constraints:); must be valid.

Source

pub fn margin(self, margin: impl Into<EdgeInsetsGeometry>) -> Container

Dart Container(margin:); must be non-negative.

Source

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

Dart Container(clipBehavior:); must be Clip::None without a decoration.

Source

pub fn transform(self, transform: Matrix4) -> Container

Dart Container(transform:).

Source

pub fn transform_alignment( self, transform_alignment: AlignmentGeometry, ) -> Container

Dart Container(transformAlignment:).

Source

pub fn child<K>(self, child: impl IntoWidget<K>) -> Container

Dart Container(child:).

Trait Implementations§

Source§

impl Debug for Container

Source§

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

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

impl Default for Container

Source§

fn default() -> Container

Returns the “default value” for a type. Read more
Source§

impl StatelessWidget for Container

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 build(&self, app: &mut App, context: BuildContext) -> WidgetRef

Describes the part of the user interface represented by this widget. Read more
Source§

fn debug_check_constructor(&self)

Dart’s constructor asserts that relate two or more arguments. Read more
Source§

fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>

Dart’s this is PreferredSizeWidget: a widget that implements PreferredSizeWidget answers with itself, which is what a parent asking an erased child for its preferred size reads.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Container

§

impl !Send for Container

§

impl !Sync for Container

§

impl !UnwindSafe for Container

§

impl Forget for Container

§

impl Freeze for Container
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, &'static dyn ColorExtension: Freeze, *const RcInner<dyn ColorExtension> is !null: Freeze, PhantomData<RcInner<dyn ColorExtension>>: Freeze, *const RcInner<dyn Decoration> is !null: Freeze, PhantomData<RcInner<dyn Decoration>>: Freeze, *const RcInner<dyn Widget> is !null: Freeze, PhantomData<RcInner<dyn Widget>>: Freeze,

§

impl Move for Container

§

impl Unpin for Container
where Rc<dyn Key>: Unpin, &'static dyn ColorExtension: Unpin, Rc<dyn ColorExtension>: Unpin, Rc<dyn Decoration>: Unpin, Rc<dyn Widget>: Unpin,

§

impl UnsafeUnpin for Container
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, &'static dyn ColorExtension: UnsafeUnpin, *const RcInner<dyn ColorExtension> is !null: UnsafeUnpin, PhantomData<RcInner<dyn ColorExtension>>: UnsafeUnpin, *const RcInner<dyn Decoration> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Decoration>>: UnsafeUnpin, *const RcInner<dyn Widget> is !null: UnsafeUnpin, PhantomData<RcInner<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<StatelessKind> for W
where W: StatelessWidget,

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>