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 usesContainerheavily.Ink, which paints a [Decoration] on aMaterial, allowingInkResponseandInkWellsplashes 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 anAlignmentGeometry.AlignmentDirectional, likeAlignmentfor 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: ClipThe 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:
Transform::alignment, which is set by this property.
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
impl Container
Sourcepub fn new() -> Container
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)).
Sourcepub fn alignment(self, alignment: AlignmentGeometry) -> Container
pub fn alignment(self, alignment: AlignmentGeometry) -> Container
Dart Container(alignment:).
Sourcepub fn padding(self, padding: impl Into<EdgeInsetsGeometry>) -> Container
pub fn padding(self, padding: impl Into<EdgeInsetsGeometry>) -> Container
Dart Container(padding:); must be non-negative.
Sourcepub fn color(self, color: impl Into<AnyColor>) -> Container
pub fn color(self, color: impl Into<AnyColor>) -> Container
Dart Container(color:); cannot be combined with
decoration.
Sourcepub fn is_anti_alias(self, is_anti_alias: bool) -> Container
pub fn is_anti_alias(self, is_anti_alias: bool) -> Container
Dart Container(isAntiAlias:).
Sourcepub fn decoration(self, decoration: impl Into<DecorationRef>) -> Container
pub fn decoration(self, decoration: impl Into<DecorationRef>) -> Container
Dart Container(decoration:); cannot be combined with color.
Sourcepub fn foreground_decoration(
self,
foreground_decoration: impl Into<DecorationRef>,
) -> Container
pub fn foreground_decoration( self, foreground_decoration: impl Into<DecorationRef>, ) -> Container
Dart Container(foregroundDecoration:).
Sourcepub fn width(self, width: f64) -> Container
pub fn width(self, width: f64) -> Container
Dart Container(width:): tightens constraints.
Sourcepub fn height(self, height: f64) -> Container
pub fn height(self, height: f64) -> Container
Dart Container(height:): tightens constraints.
Sourcepub fn constraints(self, constraints: BoxConstraints) -> Container
pub fn constraints(self, constraints: BoxConstraints) -> Container
Dart Container(constraints:); must be valid.
Sourcepub fn margin(self, margin: impl Into<EdgeInsetsGeometry>) -> Container
pub fn margin(self, margin: impl Into<EdgeInsetsGeometry>) -> Container
Dart Container(margin:); must be non-negative.
Sourcepub fn clip_behavior(self, clip_behavior: Clip) -> Container
pub fn clip_behavior(self, clip_behavior: Clip) -> Container
Dart Container(clipBehavior:); must be Clip::None without a decoration.
Sourcepub fn transform_alignment(
self,
transform_alignment: AlignmentGeometry,
) -> Container
pub fn transform_alignment( self, transform_alignment: AlignmentGeometry, ) -> Container
Dart Container(transformAlignment:).
Sourcepub fn child<K>(self, child: impl IntoWidget<K>) -> Container
pub fn child<K>(self, child: impl IntoWidget<K>) -> Container
Dart Container(child:).
Trait Implementations§
Source§impl StatelessWidget for Container
impl StatelessWidget for Container
Source§fn debug_fill_properties(
&self,
_app: &App,
properties: &mut DiagnosticPropertiesBuilder,
)
fn debug_fill_properties( &self, _app: &App, properties: &mut DiagnosticPropertiesBuilder, )
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
fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef
Source§fn debug_check_constructor(&self)
fn debug_check_constructor(&self)
Source§fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>
fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>
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 Containerwhere
*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 Containerwhere
Rc<dyn Key>: Unpin,
&'static dyn ColorExtension: Unpin,
Rc<dyn ColorExtension>: Unpin,
Rc<dyn Decoration>: Unpin,
Rc<dyn Widget>: Unpin,
impl UnsafeUnpin for Containerwhere
*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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<W> IntoWidget<StatelessKind> for Wwhere
W: StatelessWidget,
impl<W> IntoWidget<StatelessKind> for Wwhere
W: StatelessWidget,
Source§fn into_widget(self) -> Rc<dyn Widget>
fn into_widget(self) -> Rc<dyn Widget>
creation_location.