Skip to main content

Container

Struct Container 

Source
pub struct Container {
Show 21 fields pub id: Option<WidgetId>, pub child: Option<Widget>, pub width: Option<f32>, pub height: Option<f32>, pub min_width: Option<f32>, pub max_width: Option<f32>, pub min_height: Option<f32>, pub max_height: Option<f32>, pub padding: [f32; 4], pub flex_grow: f32, pub flex_shrink: f32, pub box_style: BoxStyle, pub margin: [f32; 4], pub background_fill: Option<Fill>, pub background_color: Option<Color>, pub border_color: Option<Color>, pub border_width: f32, pub border_radius: f32, pub shadow: Option<BoxShadow>, pub shadows: Vec<BoxShadow>, pub backdrop_filter: Option<BackdropFilter>,
}
Expand description

The universal wrapper widget: typed box layout, background fill, border, padding, margin, overflow, aspect ratio, positioning, and shadow on one child.

Container is the workhorse of layout composition. Use it whenever you need to add visual decoration or spacing around a child widget.

§Example

const CARD_MIN_WIDTH: f32 = 280.0;
const CARD_MAX_WIDTH: f32 = 520.0;

Container::new(Text::new("Card body"))
    .bg(theme.tokens.colors.surface)
    .border(theme.tokens.colors.border, 1.0)
    .border_radius(theme.tokens.radii.large)
    .padding_lengths(Length::all(Length::points(theme.tokens.spacing.l)))
    .width_length(Length::clamp(
        Length::points(CARD_MIN_WIDTH),
        Length::percent(42.0),
        Length::points(CARD_MAX_WIDTH),
    ))
    .flex_grow(1.0)

Fields§

§id: Option<WidgetId>

Explicit node identity.

§child: Option<Widget>

The single child widget.

§width: Option<f32>

Fixed width in layout points.

§height: Option<f32>

Fixed height in layout points.

§min_width: Option<f32>

Minimum width constraint.

§max_width: Option<f32>

Maximum width constraint.

§min_height: Option<f32>

Minimum height constraint.

§max_height: Option<f32>

Maximum height constraint.

§padding: [f32; 4]

Padding [left, right, top, bottom].

§flex_grow: f32

Flex grow factor (how much extra space this container absorbs).

§flex_shrink: f32

Flex shrink factor (how much this container shrinks when space is tight).

§box_style: BoxStyle

Declarative sizing, padding, aspect ratio, and overflow.

§margin: [f32; 4]

Outer spacing [left, right, top, bottom].

§background_fill: Option<Fill>

Background fill.

§background_color: Option<Color>

Legacy background fill colour.

§border_color: Option<Color>

Border stroke colour.

§border_width: f32

Border stroke width in layout points.

§border_radius: f32

Corner radius for rounded corners.

§shadow: Option<BoxShadow>

Optional drop shadow.

§shadows: Vec<BoxShadow>

Additional shadows drawn behind the container in order.

§backdrop_filter: Option<BackdropFilter>

Filter applied to content painted behind this container.

Implementations§

Source§

impl Container

Source

pub fn new(child: impl Into<Widget>) -> Self

Source

pub fn size(self, w: f32, h: f32) -> Self

Source

pub fn width(self, w: f32) -> Self

Source

pub fn width_length(self, width: Length) -> Self

Sets a typed preferred width.

Source

pub fn height_length(self, height: Length) -> Self

Sets a typed preferred height.

Source

pub fn min_width_length(self, width: Length) -> Self

Sets a typed minimum width.

Source

pub fn max_width_length(self, width: Length) -> Self

Sets a typed maximum width.

Source

pub fn min_height_length(self, height: Length) -> Self

Sets a typed minimum height.

Source

pub fn max_height_length(self, height: Length) -> Self

Sets a typed maximum height.

Source

pub fn height(self, h: f32) -> Self

Source

pub fn min_width(self, w: f32) -> Self

Source

pub fn max_width(self, w: f32) -> Self

Source

pub fn min_height(self, h: f32) -> Self

Source

pub fn max_height(self, h: f32) -> Self

Source

pub fn padding_all(self, p: f32) -> Self

Source

pub fn padding(self, padding: [f32; 4]) -> Self

Source

pub fn padding_lengths(self, padding: [Length; 4]) -> Self

Sets typed [left, right, top, bottom] padding.

Source

pub fn margin_all(self, margin: f32) -> Self

Sets equal point-based margin on every edge.

Source

pub fn margin(self, margin: [f32; 4]) -> Self

Sets point-based [left, right, top, bottom] margin.

Source

pub fn margin_lengths(self, margin: [Length; 4]) -> Self

Sets typed [left, right, top, bottom] margin.

Source

pub fn align_child(self, alignment: BoxAlignment) -> Self

Aligns the child inside this container.

Source

pub fn aspect_ratio(self, ratio: f32) -> Self

Sets a non-negative width-to-height ratio.

Source

pub fn positioned( self, left: Option<f32>, top: Option<f32>, right: Option<f32>, bottom: Option<f32>, ) -> Self

Absolutely positions this container with point-based offsets.

Source

pub fn positioned_lengths( self, left: Option<Length>, top: Option<Length>, right: Option<Length>, bottom: Option<Length>, ) -> Self

Positions this box with typed offsets relative to its positioned ancestor.

Source

pub fn grid_cell(self, row: i16, column: i16) -> Self

Places this container at a one-based grid row and column.

Source

pub fn grid_span(self, rows: u16, columns: u16) -> Self

Spans this container across parent grid rows and columns.

Source

pub fn clip_overflow(self, clip: bool) -> Self

Clips painting and descendants to this container’s bounds.

Source

pub fn flex_grow(self, grow: f32) -> Self

Source

pub fn flex_shrink(self, shrink: f32) -> Self

Source

pub fn bg(self, color: Color) -> Self

Source

pub fn bg_fill(self, fill: Fill) -> Self

Source

pub fn border(self, color: Color, width: f32) -> Self

Source

pub fn border_radius(self, radius: f32) -> Self

Source

pub fn shadow(self, shadow: BoxShadow) -> Self

Source

pub fn shadows(self, shadows: Vec<BoxShadow>) -> Self

Source

pub fn backdrop_blur(self, sigma: f32) -> Self

Blurs content behind this container, clipped to its rounded bounds.

Trait Implementations§

Source§

impl Clone for Container

Source§

fn clone(&self) -> Container

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
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() -> Self

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

impl<'de> Deserialize<'de> for Container

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<Container> for Widget

Source§

fn from(w: Container) -> Self

Converts to this type from the input type.
Source§

impl Serialize for Container

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
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<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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> WidgetIdExt for T
where T: Into<Widget>,

Source§

fn id<I>(self, id: I) -> Widget
where I: Into<WidgetId>,