Skip to main content

SizedBox

Struct SizedBox 

Source
pub struct SizedBox {
    pub key: Option<KeyRef>,
    pub width: Option<f64>,
    pub height: Option<f64>,
    pub child: Option<WidgetRef>,
}
Expand description

A box with a specified size.

If given a child, this widget forces it to have a specific width and/or height. These values will be ignored if this widget’s parent does not permit them. For example, this happens if the parent is the screen (forces the child to be the same size as the parent), or another SizedBox (forces its child to have a specific width and/or height). This can be remedied by wrapping the child SizedBox in a widget that does permit it to be any size up to the size of the parent, such as Center or Align.

If either the width or height is None, this widget will try to size itself to match the child’s size in that dimension. If the child’s size depends on the size of its parent, the height and width must be provided.

If not given a child, SizedBox will try to size itself as close to the specified height and width as possible given the parent’s constraints. If height or width is None or unspecified, it will be treated as zero.

The expand constructor can be used to make a SizedBox that sizes itself to fit the parent. It is equivalent to setting width and height to f64::INFINITY.

This snippet makes the child widget (a Card with some Text) have the exact size 200x300, parental constraints permitting:

SizedBox {
  width: Some(200.0),
  height: Some(300.0),
  child: Some(Card(child: Text("Hello World!"))),
  ..Default::default()
}

See also:

  • ConstrainedBox, a more generic version of this class that takes arbitrary BoxConstraints instead of an explicit width and height.
  • UnconstrainedBox, a container that tries to let its child draw without constraints.
  • FractionallySizedBox, a widget that sizes its child to a fraction of the total available space.
  • AspectRatio, a widget that attempts to fit within the parent’s constraints while also sizing its child to match a given aspect ratio.
  • FittedBox, which sizes and positions its child widget to fit the parent according to a given BoxFit discipline.
  • The catalog of layout widgets.

Dart’s named constructors are the associated functions expand, shrink, from_size, and square; set a key through struct update syntax: SizedBox { key: Some(key), ..SizedBox::expand(child) }.

Fields§

§key: Option<KeyRef>§width: Option<f64>

If non-None, requires the child to have exactly this width.

§height: Option<f64>

If non-None, requires the child to have exactly this height.

§child: Option<WidgetRef>

Implementations§

Source§

impl SizedBox

Source

pub fn new() -> SizedBox

Creates a SizedBox; Dart’s named arguments are the setters.

Source

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

Dart SizedBox(key:).

Source

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

Dart SizedBox(width:).

Source

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

Dart SizedBox(height:).

Source

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

Dart SizedBox(child:).

Source

pub fn expand() -> SizedBox

Creates a box that will become as large as its parent allows.

Source

pub fn shrink() -> SizedBox

Creates a box that will become as small as its parent allows.

Source

pub fn from_size(size: Option<Size>) -> SizedBox

Creates a box with the specified size.

Source

pub fn square(dimension: Option<f64>) -> SizedBox

Creates a box whose width and height are equal.

Trait Implementations§

Source§

impl Debug for SizedBox

Source§

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

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

impl Default for SizedBox

Source§

fn default() -> Self

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

impl RenderObjectWidget for SizedBox

Source§

fn to_string_short(&self) -> String

Dart’s SizedBox.toStringShort: SizedBox.expand and SizedBox.shrink by the sizes those constructors give.

Source§

type RenderObject = RenderConstrainedBox

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<RenderConstrainedBox>, )

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 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.
Source§

impl SingleChildRenderObjectWidget for SizedBox

Source§

fn child(&self) -> Option<&WidgetRef>

The widget below this widget in the tree.
Source§

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

Creates the element that manages this widget’s child. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for SizedBox

§

impl !Send for SizedBox

§

impl !Sync for SizedBox

§

impl !UnwindSafe for SizedBox

§

impl Forget for SizedBox

§

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

§

impl Move for SizedBox

§

impl Unpin for SizedBox
where Rc<dyn Key>: Unpin, Rc<dyn Widget>: Unpin,

§

impl UnsafeUnpin for SizedBox
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: 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<SingleChildRenderObjectKind> 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>