Skip to main content

Material

Struct Material 

Source
pub struct Material {
Show 14 fields pub key: Option<KeyRef>, pub child: Option<WidgetRef>, pub type: MaterialType, pub animate_color: bool, pub elevation: f64, pub color: Option<Color>, pub shadow_color: Option<Color>, pub surface_tint_color: Option<Color>, pub text_style: Option<TextStyle>, pub shape: Option<ShapeBorderRef>, pub border_on_foreground: bool, pub clip_behavior: Clip, pub animation_duration: Duration, pub border_radius: Option<BorderRadiusGeometry>,
}
Expand description

A piece of material.

The Material widget is responsible for:

  1. Clipping: If clip_behavior is not Clip::None, Material clips its widget sub-tree to the shape specified by shape, type, and border_radius. By default, clip_behavior is Clip::None for performance considerations. See Ink for an example of how this affects clipping Ink widgets.
  2. Elevation: Material elevates its widget sub-tree on the Z axis by elevation pixels, and draws the appropriate shadow.
  3. Ink effects: Material shows ink effects implemented by InkFeatures like InkSplash and InkHighlight below its children.

§The Material Metaphor

Material is the central metaphor in Material Design. Each piece of material exists at a given elevation, which influences how that piece of material visually relates to other pieces of material and how that material casts shadows.

Most user interface elements are either conceptually printed on a piece of material or themselves made of material. Material reacts to user input using InkSplash and InkHighlight effects. To trigger a reaction on the material, use a MaterialInkController obtained via Material::of.

In general, the features of a Material should not change over time (e.g. a Material should not change its color, shadow_color or type). Changes to elevation, shadow_color and surface_tint_color are animated for animation_duration. Changes to shape are animated if type is not MaterialType::Transparency and ShapeBorder::lerp between the previous and next shape values is supported. Shape changes are also animated for animation_duration.

§Shape

The shape for material is determined by shape, type, and border_radius.

§Border

If shape is not null, then its border will also be painted (if any).

§Layout change notifications

If the layout changes (e.g. because there’s a list on the material, and it’s been scrolled), a LayoutChangedNotification must be dispatched at the relevant subtree. This in particular means that transitions (e.g. SlideTransition) should not be placed inside Material widgets so as to move subtrees that contain InkResponses, InkWells, Inks, or other widgets that use the InkFeature mechanism. Otherwise, in-progress ink features (e.g., ink splashes and ink highlights) won’t move to account for the new layout.

§Painting over the material

Material widgets will often trigger reactions on their nearest material ancestor. For example, ListTile.hoverColor triggers a reaction on the tile’s material when a pointer is hovering over it. These reactions will be obscured if any widget in between them and the material paints in such a way as to obscure the material (such as setting a BoxDecoration.color on a DecoratedBox). To avoid this behavior, use InkDecoration to decorate the material itself.

See also:

Fields§

§key: Option<KeyRef>§child: Option<WidgetRef>

The widget below this widget in the tree.

§type: MaterialType

The kind of material to show (e.g., card or canvas). This affects the shape of the widget, the roundness of its corners if the shape is rectangular, and the default color.

§animate_color: bool

Whether the color should be animated.

§elevation: f64

The z-coordinate at which to place this material relative to its parent.

This controls the size of the shadow below the material and the opacity of the elevation overlay color if it is applied.

If this is non-zero, the contents of the material are clipped, because the widget conceptually defines an independent printed piece of material.

Defaults to 0. Changing this value will cause the shadow and the elevation overlay or surface tint to animate over Material::animation_duration.

The value is non-negative.

See also:

  • ThemeData::use_material3 which defines whether a surface tint or elevation overlay is used to indicate elevation.
  • ThemeData::apply_elevation_overlay_color which controls the whether an overlay color will be applied to indicate elevation.
  • Material::color which may have an elevation overlay applied.
  • Material::shadow_color which will be used for the color of a drop shadow.
  • Material::surface_tint_color which will be used as the overlay tint to show elevation.
§color: Option<Color>

The color to paint the material.

Must be opaque. To create a transparent piece of material, use MaterialType::Transparency.

If ThemeData::use_material3 is true then an optional surface_tint_color overlay may be applied on top of this color to indicate elevation.

If ThemeData::use_material3 is false and ThemeData::apply_elevation_overlay_color is true and ThemeData::brightness is Brightness::Dark then a semi-transparent overlay color will be composited on top of this color to indicate the elevation. This is no longer needed for Material Design 3, which uses surface_tint_color.

By default, the color is derived from the type of material.

§shadow_color: Option<Color>

The color to paint the shadow below the material.

If null and ThemeData::use_material3 is true then ThemeData’s ColorScheme::shadow will be used. If ThemeData::use_material3 is false then ThemeData::shadow_color will be used.

To remove the drop shadow when elevation is greater than 0, set shadow_color to Colors::TRANSPARENT.

See also:

  • ThemeData::use_material3, which determines the default value for this property if it is null.
  • ThemeData::apply_elevation_overlay_color, which turns elevation overlay on or off for dark themes.
§surface_tint_color: Option<Color>

The color of the surface tint overlay applied to the material color to indicate elevation.

Material Design 3 introduced a new way for some components to indicate their elevation by using a surface tint color overlay on top of the base material color. This overlay is painted with an opacity that is related to the elevation of the material.

If ThemeData::use_material3 is false, then this property is not used.

If ThemeData::use_material3 is true and surface_tint_color is not null and not Colors::TRANSPARENT, then it will be used to overlay the base color with an opacity based on the elevation.

Otherwise, no surface tint will be applied.

See also:

§text_style: Option<TextStyle>

The typographical style to use for text within this material.

§shape: Option<ShapeBorderRef>

Defines the material’s shape as well its shadow.

If shape is non null, the border_radius is ignored and the material’s clip boundary and shadow are defined by the shape.

A shadow is only displayed if the elevation is greater than zero.

§border_on_foreground: bool

Whether to paint the shape border in front of the child.

The default value is true. If false, the border will be painted behind the child.

§clip_behavior: Clip

The content will be clipped (or not) according to this option.

Defaults to Clip::None.

§animation_duration: Duration

Defines the duration of animated changes for shape, elevation, shadow_color, surface_tint_color and the elevation overlay if it is applied.

The default value is K_THEME_CHANGE_DURATION.

§border_radius: Option<BorderRadiusGeometry>

If non-null, the corners of this box are rounded by this BorderRadiusGeometry value.

Otherwise, the corners specified for the current type of material are used.

If shape is non null then the border radius is ignored.

Must be null if type is MaterialType::Circle.

Implementations§

Source§

impl Material

Source

pub const DEFAULT_SPLASH_RADIUS: f64 = 35.0

The default radius of an ink splash in logical pixels.

Source

pub fn new() -> Material

Creates a piece of material; Dart’s named arguments are the setters.

The elevation must be non-negative.

If a shape is specified, then the border_radius property must be null and the type property must not be MaterialType::Circle. If the border_radius is specified, then the type property must not be MaterialType::Circle. In both cases, these restrictions are intended to catch likely errors.

Source

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

Dart Material(key:).

Source

pub fn type(self, type: MaterialType) -> Material

Dart Material(type:).

Source

pub fn elevation(self, elevation: f64) -> Material

Dart Material(elevation:).

Source

pub fn color(self, color: Color) -> Material

Dart Material(color:).

Source

pub fn shadow_color(self, shadow_color: Color) -> Material

Dart Material(shadowColor:).

Source

pub fn surface_tint_color(self, surface_tint_color: Color) -> Material

Dart Material(surfaceTintColor:).

Source

pub fn text_style(self, text_style: TextStyle) -> Material

Dart Material(textStyle:).

Source

pub fn border_radius( self, border_radius: impl Into<BorderRadiusGeometry>, ) -> Material

Dart Material(borderRadius:).

Source

pub fn shape(self, shape: impl Into<ShapeBorderRef>) -> Material

Dart Material(shape:).

Source

pub fn border_on_foreground(self, border_on_foreground: bool) -> Material

Dart Material(borderOnForeground:).

Source

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

Dart Material(clipBehavior:).

Source

pub fn animation_duration(self, animation_duration: Duration) -> Material

Dart Material(animationDuration:).

Source

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

Dart Material(child:).

Source

pub fn animate_color(self, animate_color: bool) -> Material

Dart Material(animateColor:).

Source

pub fn maybe_of( app: &App, context: BuildContext, ) -> Option<AnyMaterialInkController>

The ink controller from the closest instance of this class that encloses the given context within the closest LookupBoundary.

This method can be expensive (it walks the element tree).

See also:

Source

pub fn of(app: &App, context: BuildContext) -> AnyMaterialInkController

The ink controller from the closest instance of Material that encloses the given context within the closest LookupBoundary.

If no Material widget ancestor can be found then this method panics.

This method can be expensive (it walks the element tree).

See also:

Trait Implementations§

Source§

impl Clone for Material

Source§

fn clone(&self) -> Self

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 Material

Source§

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

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

impl Default for Material

Source§

fn default() -> Material

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

impl StatefulWidget for Material

Source§

type State = MaterialWidgetState

The state this widget creates.
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_state(&self) -> MaterialWidgetState

Creates the mutable state for this widget at a given location in the tree. Read more
Source§

fn debug_check_constructor(&self)

Source§

fn as_preferred_size_widget( &self, ) -> Option<&(dyn PreferredSizeWidget + 'static)>

Source§

fn type_name(&self) -> &'static str

See Widget::type_name. A stateful form another widget kind becomes (an animated or status transition widget) names the widget it wraps.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Material

§

impl !Send for Material

§

impl !Sync for Material

§

impl !UnwindSafe for Material

§

impl Forget for Material

§

impl Freeze for Material
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, &'static dyn ColorExtension: Freeze, *const RcInner<dyn ColorExtension> is !null: Freeze, PhantomData<RcInner<dyn ColorExtension>>: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze, PhantomData<String>: Freeze, *const RcInner<Paint> is !null: Freeze, PhantomData<RcInner<Paint>>: Freeze, PhantomData<Shadow>: Freeze, PhantomData<FontFeature>: Freeze, PhantomData<FontVariation>: Freeze, *const RcInner<dyn ShapeBorder> is !null: Freeze, PhantomData<RcInner<dyn ShapeBorder>>: Freeze,

§

impl Move for Material

§

impl Unpin for Material
where Rc<dyn Key>: Unpin, Rc<dyn Widget>: Unpin, &'static dyn ColorExtension: Unpin, Rc<dyn ColorExtension>: Unpin, *const u8 is !null: Unpin, Rc<Paint>: Unpin, Rc<dyn ShapeBorder>: Unpin,

§

impl UnsafeUnpin for Material

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> 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> 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<StatefulKind> for W
where W: StatefulWidget,

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> 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 = !

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>