Skip to main content

MaterialChip

Struct MaterialChip 

Source
pub struct MaterialChip<'a> { /* private fields */ }
Expand description

Material Design chip component following Material Design 3 specifications

Chips are compact elements that represent an input, attribute, or action. They allow users to enter information, make selections, filter content, or trigger actions.

§Usage Examples

// Assist chip - helps users with contextual actions
if ui.add(MaterialChip::assist("Settings")).clicked() {
    // Open settings
}

// Filter chip - for filtering content
let mut filter_active = false;
ui.add(MaterialChip::filter("Photos")
    .selected(&mut filter_active));

// Input chip - represents entered data
ui.add(MaterialChip::input("john@example.com")
    .removable(true));

// Suggestion chip - suggests actions or content
ui.add(MaterialChip::suggestion("Try this feature"));

§Material Design Spec

  • Height: 32dp
  • Corner radius: 8dp
  • Text: Label Large (14sp/500 weight)
  • Touch target: Minimum 48x48dp

Implementations§

Source§

impl<'a> MaterialChip<'a>

Source

pub fn new(text: impl Into<String>, variant: ChipVariant) -> Self

Create a new chip with specified text and variant

§Parameters
  • text: Text to display on the chip
  • variant: Type of chip (Assist, Filter, Input, Suggestion)
Source

pub fn assist(text: impl Into<String>) -> Self

Create an assist chip for contextual actions

Assist chips help users take actions or get information about their current context. They should appear dynamically and contextually in the UI.

§Material Design Usage
  • Display contextually relevant actions
  • Usually triggered by user actions or context changes
  • Should not be persistent in the interface
Source

pub fn filter(text: impl Into<String>, selected: &'a mut bool) -> Self

Create a filter chip for content filtering

Filter chips are used for filtering content and are typically displayed in a set. They can be selected/deselected to refine displayed content.

§Parameters
  • text: Label for the filter option
  • selected: Mutable reference to selection state
§Material Design Usage
  • Group related filter options together
  • Allow multiple selections for broad filtering
  • Provide clear visual feedback for selected state
Source

pub fn input(text: impl Into<String>) -> Self

Create an input chip representing user-entered data

Input chips represent discrete pieces of information entered by a user, such as tags, contacts, or other structured data.

§Material Design Usage
  • Represent complex entities in a compact form
  • Often removable to allow editing of input data
  • Used in forms and data entry interfaces
Source

pub fn suggestion(text: impl Into<String>) -> Self

Create a suggestion chip that provides actionable content suggestions

Suggestion chips are used to help users discover relevant actions or content. They can be used in conjunction with dynamic features like autocomplete or content recommendations.

Source

pub fn elevated(self, elevated: bool) -> Self

Set whether the chip should have elevation (shadow) effect

Elevated chips have a surface-container-high background and a shadow to indicate elevation. This is typically used for assist and suggestion chips.

Source

pub fn enabled(self, enabled: bool) -> Self

Enable or disable the chip

Disabled chips have a different visual treatment and do not respond to user interactions. Soft-disabled chips are still visible but appear with reduced opacity.

Source

pub fn soft_disabled(self, soft_disabled: bool) -> Self

Set the chip as soft-disabled

Soft-disabled chips have a different visual treatment (e.g., lighter opacity) compared to hard-disabled chips. They are still interactive but indicate that the action is unavailable.

Source

pub fn removable(self, removable: bool) -> Self

Set whether the chip can be removed

Removable chips show an X icon that allows users to remove the chip from the UI. This is useful for input and filter chips.

Source

pub fn leading_icon(self, icon: impl Into<String>) -> Self

Set a leading icon for the chip using a Material icon name

The icon will be displayed on the left side of the chip’s text. This is commonly used for assist and filter chips.

Source

pub fn leading_icon_svg(self, svg_data: impl Into<String>) -> Self

Set a leading icon for the chip using SVG data

The SVG data will be converted to a texture and displayed on the left side of the chip’s text. This allows for custom icons with scalable vector graphics.

Source

pub fn leading_icon_png(self, png_bytes: Vec<u8>) -> Self

Set a leading icon for the chip using PNG image data

The PNG image data will be converted to a texture and displayed on the left side of the chip’s text. This is useful for using raster images as icons.

Source

pub fn leading_icon_texture(self, texture: TextureHandle) -> Self

Set a pre-loaded texture as the leading icon for the chip

This allows using any texture as an icon, without the need to convert from image data. The texture should be created and managed externally.

Source

pub fn avatar(self, avatar: bool) -> Self

Set whether to use avatar-style rounded appearance for the chip

Avatar-style chips have a more pronounced roundness, making them suitable for representing users or profile-related content. Regular chips are more rectangular.

Source

pub fn on_click<F>(self, f: F) -> Self
where F: Fn() + 'a,

Set a callback function to be called when the chip is clicked

This allows defining custom actions for each chip, such as navigating to a different view, opening a dialog, or triggering any other behavior.

Trait Implementations§

Source§

impl<'a> Widget for MaterialChip<'a>

Source§

fn ui(self, ui: &mut Ui) -> Response

Allocate space, interact, paint, and return a Response. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for MaterialChip<'a>

§

impl<'a> !RefUnwindSafe for MaterialChip<'a>

§

impl<'a> !Send for MaterialChip<'a>

§

impl<'a> !Sync for MaterialChip<'a>

§

impl<'a> Unpin for MaterialChip<'a>

§

impl<'a> UnsafeUnpin for MaterialChip<'a>

§

impl<'a> !UnwindSafe for MaterialChip<'a>

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more