Skip to main content

MaterialColor

Struct MaterialColor 

Source
pub struct MaterialColor { /* private fields */ }
Expand description

Defines a single color as well a color swatch with ten shades of the color.

The color’s shades are referred to by index. The greater the index, the darker the color. There are 10 valid indices: 50, 100, 200, …, 900. The value of this color should the same the value of index 500 and shade500.

§Updating to ColorScheme

The ColorScheme is preferred for representing colors in applications that are configured for Material 3 (see ThemeData.useMaterial3). For more information on colors in Material 3 see the spec at https://m3.material.io/styles/color/the-color-system.

{@template material_ui.colors.colorRoles} In Material 3, colors are represented using color roles and corresponding tokens. Each property in the ColorScheme class represents one color role as defined in the spec above. {@endtemplate}

§Material 3 Colors in Flutter

{@template material_ui.colors.settingColors} Flutter’s Material widgets can be assigned colors at the widget level using widget properties, or at the app level using theme classes.

For example, you can set the background of the AppBar by setting the AppBar.backgroundColor to a specific Color value.

To globally set the AppBar background color for your app, you can set the ThemeData.appBarTheme property for your MaterialApp using the ThemeData class. You can also override the default appearance of all the AppBars in a widget subtree by placing the AppBarTheme at the root of the subtree.

Alternatively, you can set the ThemeData.colorScheme property to a custom ColorScheme. This creates a unified ColorScheme to be used across the app. The AppBar.backgroundColor uses the ColorScheme.surface by default. {@endtemplate}

§Migrating from MaterialColor to ColorScheme

In most cases, there are new properties in Flutter widgets that accept a ColorScheme instead of a MaterialColor.

For example, you may have previously constructed a ThemeData using a primarySwatch:

ThemeData(
  primarySwatch: Colors.amber,
)

In Material 3, you can use the ColorScheme class to construct a ThemeData with the same color palette by using the ColorScheme.fromSeed constructor:

ThemeData(
 colorScheme: ColorScheme.fromSeed(seedColor: Colors.amber),
)

The ColorScheme.fromSeed constructor will generate a set of tonal palettes, which are used to create the color scheme.

Alternatively you can use the ColorScheme.fromSwatch constructor:

ThemeData(
 colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.amber),
)

The ColorScheme.fromSwatch constructor will create the color scheme directly from the specific color values used in the MaterialColor.

See also:

  • Colors, which defines all of the standard material colors.

Implementations§

Source§

impl MaterialColor

Source

pub fn new( primary: Color, swatch: impl IntoIterator<Item = (i32, Color)>, ) -> MaterialColor

Creates a color swatch with a variety of shades.

The primary argument should be the 32 bit ARGB value of one of the values in the swatch, as would be passed to the Color.new constructor for that same color, and as is exposed by value. (This is distinct from the specific index of the color in the swatch.)

Source

pub const fn from_static( primary: Color, swatch: &'static [(i32, Color)], ) -> MaterialColor

Dart’s const MaterialColor(primary, {...}), for a table written in the source.

Source

pub fn shade50(&self) -> Color

The lightest shade.

Source

pub fn shade100(&self) -> Color

The second lightest shade.

Source

pub fn shade200(&self) -> Color

The third lightest shade.

Source

pub fn shade300(&self) -> Color

The fourth lightest shade.

Source

pub fn shade400(&self) -> Color

The fifth lightest shade.

Source

pub fn shade500(&self) -> Color

The default shade.

Source

pub fn shade600(&self) -> Color

The fourth darkest shade.

Source

pub fn shade700(&self) -> Color

The third darkest shade.

Source

pub fn shade800(&self) -> Color

The second darkest shade.

Source

pub fn shade900(&self) -> Color

The darkest shade.

Source

pub fn into_any(self) -> AnyColor

The swatch as the Color it is in Dart.

Methods from Deref<Target = ColorSwatch<i32>>§

Source

pub fn primary(&self) -> Color

The primary color: what Dart’s swatch is, as a Color.

Source

pub fn get(&self, key: &T) -> Option<Color>

Returns an element of the swatch table.

Source

pub fn keys(&self) -> impl Iterator<Item = &T>

Returns the valid keys for accessing operator[].

Trait Implementations§

Source§

impl Clone for MaterialColor

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 ColorExtension for MaterialColor

Source§

fn as_any(&self) -> &dyn Any

The extension as Any, for Dart’s is checks (AnyColor::extension).
Source§

fn eq_extension(&self, other: &dyn ColorExtension) -> bool

Dart’s == override on the subclass, called once the values compared equal. other is the same runtime type when it downcasts; any other type is unequal, as Dart’s runtimeType check makes it.
Source§

impl Debug for MaterialColor

Source§

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

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

impl Deref for MaterialColor

Source§

type Target = ColorSwatch<i32>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &ColorSwatch<i32>

Dereferences the value.
Source§

impl From<MaterialColor> for AnyColor

Source§

fn from(color: MaterialColor) -> AnyColor

Converts to this type from the input type.
Source§

impl PartialEq for MaterialColor

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for MaterialColor

Auto Trait Implementations§

§

impl Forget for MaterialColor

§

impl Freeze for MaterialColor
where &'static [(i32, Color)]: Freeze, Vec<(i32, Color)>: Freeze,

§

impl Move for MaterialColor

§

impl RefUnwindSafe for MaterialColor

§

impl Send for MaterialColor
where &'static [(i32, Color)]: Send, Vec<(i32, Color)>: Send,

§

impl Sync for MaterialColor
where Vec<(i32, Color)>: Sync,

§

impl Unpin for MaterialColor
where &'static [(i32, Color)]: Unpin, Vec<(i32, Color)>: Unpin,

§

impl UnsafeUnpin for MaterialColor
where &'static [(i32, Color)]: UnsafeUnpin, Vec<(i32, Color)>: UnsafeUnpin,

§

impl UnwindSafe for MaterialColor
where &'static [(i32, Color)]: UnwindSafe, Vec<(i32, Color)>: UnwindSafe,

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<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,