Skip to main content

SegmentedButton

Struct SegmentedButton 

Source
pub struct SegmentedButton<T> {
    pub key: Option<KeyRef>,
    pub segments: Vec<ButtonSegment<T>>,
    pub direction: Axis,
    pub selected: Vec<T>,
    pub on_selection_changed: Option<ValueChanged<Vec<T>>>,
    pub multi_selection_enabled: bool,
    pub empty_selection_allowed: bool,
    pub expanded_insets: Option<EdgeInsets>,
    pub style: Option<ButtonStyle>,
    pub show_selected_icon: bool,
    pub selected_icon: Option<WidgetRef>,
}
Expand description

A Material button that allows the user to select from limited set of options.

Segmented buttons are used to help people select options, switch views, or sort elements. They are typically used in cases where there are only 2-5 options.

The options are represented by segments described with ButtonSegment entries in the segments field. Each segment has a ButtonSegment::value that is used to indicate which segments are selected.

The selected field is a set of selected ButtonSegment::values, each once. This should be updated by the app in response to on_selection_changed updates.

By default, only a single segment can be selected (for mutually exclusive choices). This can be relaxed with the multi_selection_enabled field.

Like ButtonStyleButtons, the SegmentedButton’s visuals can be configured with a ButtonStyle style field. However, unlike other buttons, some of the style parameters are applied to the entire segmented button, and others are used for each of the segments.

By default, a checkmark icon is used to show selected items. To configure this behavior, you can use the show_selected_icon and selected_icon fields.

Individual segments can be enabled or disabled with their ButtonSegment::enabled flag. If the on_selection_changed field is null, then the entire segmented button will be disabled, regardless of the individual segment settings.

See also:

  • Material Design spec: https://m3.material.io/components/segmented-buttons/overview
  • ButtonStyle, which can be used in the style field to configure the appearance of the button and its segments.
  • ToggleButtons, a similar widget that was built for Material 2. SegmentedButton should be considered as a replacement for ToggleButtons.
  • Radio, an alternative way to present the user with a mutually exclusive set of options.
  • FilterChip, ChoiceChip, which can be used when you need to show more than five options.

Fields§

§key: Option<KeyRef>§segments: Vec<ButtonSegment<T>>

Descriptions of the segments in the button.

This a required parameter and must contain at least one segment, but it is recommended to contain two to five segments. If you need only a single segment, consider using a Checkbox or Radio widget instead. If you need more than five options, consider using FilterChip or ChoiceChip widgets.

§direction: Axis

The orientation of the button’s segments.

If this is Axis::Vertical, the segments will be aligned vertically and the first item in segments will be on the top.

Defaults to Axis::Horizontal.

§selected: Vec<T>

The set of ButtonSegment::values that indicate which segments are selected, in insertion order, each once.

As the SegmentedButton does not maintain the state of the selection, you will need to update this in response to on_selection_changed calls.

This is a required parameter.

§on_selection_changed: Option<ValueChanged<Vec<T>>>

The function that is called when the selection changes.

The callback’s parameter indicates which of the segments are selected.

When the callback is null, the entire SegmentedButton is disabled, and will not respond to input.

The default is null.

§multi_selection_enabled: bool

Determines if multiple segments can be selected at one time.

If true, more than one segment can be selected. When selecting a segment, the other selected segments will stay selected. Selecting an already selected segment will unselect it.

If false, only one segment may be selected at a time. When a segment is selected, any previously selected segment will be unselected.

The default is false, so only a single segment may be selected at one time.

§empty_selection_allowed: bool

Determines if having no selected segments is allowed.

If true, then it is acceptable for none of the segments to be selected. This means that selected can be empty. If the user taps on a selected segment, it will be removed from the selection set passed into on_selection_changed.

If false (the default), there must be at least one segment selected. If the user taps on the only selected segment it will not be deselected, and on_selection_changed will not be called.

§expanded_insets: Option<EdgeInsets>

Determines the segmented button’s size and padding based on expanded_insets.

If null (default), the button adopts its intrinsic content size. When specified, the button expands to fill its parent’s space, with the EdgeInsets defining the padding.

§style: Option<ButtonStyle>

Customizes this button’s appearance.

The following style properties apply to the entire segmented button:

The following style properties are applied to each of the individual button segments. For properties that are a WidgetStateProperty, they will be resolved with the current state of the segment:

If ButtonStyle::side is provided, WidgetStateProperty::resolve is used for the following WidgetStates:

  • WidgetState::Focused.
  • WidgetState::Hovered.
  • WidgetState::Disabled.
  • WidgetState::Selected.
§show_selected_icon: bool

Determines if the selected_icon (usually an icon using Icons::check) is displayed on the selected segments.

If true, the selected_icon will be displayed at the start of the segment. If both the ButtonSegment::label and ButtonSegment::icon are provided, then the icon will be replaced with the selected_icon. If only the icon or the label is present then the selected_icon will be shown at the start of the segment.

If false, then the selected_icon is not used and will not be displayed on selected segments.

The default is true, meaning the selected_icon will be shown on selected segments.

§selected_icon: Option<WidgetRef>

An icon that is used to indicate a segment is selected.

If show_selected_icon is true then for selected segments this icon will be shown before the ButtonSegment::label, replacing the ButtonSegment::icon if it is specified.

Defaults to an Icon with Icons::check.

Implementations§

Source§

impl<T: Clone + PartialEq + 'static> SegmentedButton<T>

Source

pub fn new( segments: Vec<ButtonSegment<T>>, selected: Vec<T>, ) -> SegmentedButton<T>

Creates a SegmentedButton.

segments must contain at least one segment, but it is recommended to have two to five segments. If you need only single segment, consider using a Checkbox or Radio widget instead. If you need more than five options, consider using FilterChip or ChoiceChip widgets.

If on_selection_changed is null, then the entire segmented button will be disabled.

By default selected must only contain one entry. However, if multi_selection_enabled is true, then selected can contain multiple entries. If empty_selection_allowed is true, then selected can be empty.

Source

pub fn key(self, key: KeyRef) -> SegmentedButton<T>

Dart SegmentedButton(key:).

Source

pub fn direction(self, direction: Axis) -> SegmentedButton<T>

Dart SegmentedButton(direction:).

Source

pub fn on_selection_changed( self, on_selection_changed: impl Fn(&mut App, Vec<T>) + 'static, ) -> SegmentedButton<T>

Dart SegmentedButton(onSelectionChanged:).

Source

pub fn multi_selection_enabled( self, multi_selection_enabled: bool, ) -> SegmentedButton<T>

Dart SegmentedButton(multiSelectionEnabled:).

Source

pub fn empty_selection_allowed( self, empty_selection_allowed: bool, ) -> SegmentedButton<T>

Dart SegmentedButton(emptySelectionAllowed:).

Source

pub fn expanded_insets(self, expanded_insets: EdgeInsets) -> SegmentedButton<T>

Dart SegmentedButton(expandedInsets:).

Source

pub fn style(self, style: ButtonStyle) -> SegmentedButton<T>

Dart SegmentedButton(style:).

Source

pub fn show_selected_icon(self, show_selected_icon: bool) -> SegmentedButton<T>

Dart SegmentedButton(showSelectedIcon:).

Source

pub fn selected_icon<K>( self, selected_icon: impl IntoWidget<K>, ) -> SegmentedButton<T>

Dart SegmentedButton(selectedIcon:).

Source§

impl<T> SegmentedButton<T>

Source

pub fn style_from() -> SegmentedButtonStyleFromBuilder

A static convenience method that constructs a segmented button ButtonStyle given simple values.

The foreground_color, selected_foreground_color, and disabled_foreground_color colors are used to create a WidgetStateProperty ButtonStyle::foreground_color, and a derived ButtonStyle::overlay_color if overlay_color isn’t specified.

If overlay_color is specified and its value is Colors::TRANSPARENT then the pressed/focused/hovered highlights are effectively defeated. Otherwise a WidgetStateProperty with the same opacities as the default is created.

The background_color, selected_background_color and disabled_background_color colors are used to create a WidgetStateProperty ButtonStyle::background_color.

Similarly, the enabled_mouse_cursor and disabled_mouse_cursor parameters are used to construct ButtonStyle::mouse_cursor.

The icon_color, disabled_icon_color are used to construct ButtonStyle::icon_color and icon_size is used to construct ButtonStyle::icon_size.

All of the other parameters are either used directly or used to create a WidgetStateProperty with a single value for all states.

All parameters default to null. By default this method returns a ButtonStyle that doesn’t override anything.

For example, to override the default text and icon colors for a SegmentedButton, as well as its overlay color, with all of the standard opacity adjustments for the pressed, focused, and hovered states, one could write:

SegmentedButton::new(segments, selected).style(
    SegmentedButton::<i32>::style_from()
        .foreground_color(Colors::BLACK)
        .selected_foreground_color(Colors::WHITE)
        .background_color(Colors::AMBER.primary())
        .selected_background_color(Colors::RED.primary())
        .build(),
)

Trait Implementations§

Source§

impl<T> Clone for SegmentedButton<T>
where T: Clone,

Source§

fn clone(&self) -> SegmentedButton<T>

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<T: Debug> Debug for SegmentedButton<T>

Source§

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

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

impl<T: Clone + PartialEq + Debug + 'static> StatefulWidget for SegmentedButton<T>

Source§

type State = SegmentedButtonState<T>

The state this widget creates.
Source§

fn key(&self) -> Option<&KeyRef>

Source§

fn debug_check_constructor(&self)

Source§

fn create_state(&self) -> SegmentedButtonState<T>

Creates the mutable state for this widget at a given location in the tree. Read more
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 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<T> !RefUnwindSafe for SegmentedButton<T>

§

impl<T> !Send for SegmentedButton<T>

§

impl<T> !Sync for SegmentedButton<T>

§

impl<T> !UnwindSafe for SegmentedButton<T>

§

impl<T> Forget for SegmentedButton<T>

§

impl<T> Freeze for SegmentedButton<T>
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const u8 is !null: Freeze, PhantomData<ButtonSegment<T>>: Freeze, PhantomData<T>: Freeze, *const RcInner<dyn Fn(&mut App, Vec<T>)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, Vec<T>)>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<TextStyle>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<TextStyle>>>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<Color>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<Color>>>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<f64>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<f64>>>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<EdgeInsetsGeometry>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<EdgeInsetsGeometry>>>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<Size>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<Size>>>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<BorderSide>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<BorderSide>>>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<OutlinedBorderRef>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<OutlinedBorderRef>>>>: Freeze, *const RcInner<dyn WidgetStateProperty<Option<Rc<dyn MouseCursor>>>> is !null: Freeze, PhantomData<RcInner<dyn WidgetStateProperty<Option<Rc<dyn MouseCursor>>>>>: Freeze, *const RcInner<dyn InteractiveInkFeatureFactory> is !null: Freeze, PhantomData<RcInner<dyn InteractiveInkFeatureFactory>>: Freeze, *const RcInner<dyn Fn(&mut App, AnyElement, &HashSet<WidgetState>, Option<Rc<dyn Widget>>) -> Rc<dyn Widget>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, &HashSet<WidgetState>, Option<Rc<dyn Widget>>) -> Rc<dyn Widget>>>: Freeze, *const (): Freeze, *const RcInner<dyn Widget> is !null: Freeze, PhantomData<RcInner<dyn Widget>>: Freeze,

§

impl<T> Move for SegmentedButton<T>

§

impl<T> Unpin for SegmentedButton<T>

§

impl<T> UnsafeUnpin for SegmentedButton<T>
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<ButtonSegment<T>>: UnsafeUnpin, PhantomData<T>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, Vec<T>)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, Vec<T>)>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<TextStyle>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<TextStyle>>>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<Color>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<Color>>>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<f64>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<f64>>>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<EdgeInsetsGeometry>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<EdgeInsetsGeometry>>>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<Size>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<Size>>>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<BorderSide>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<BorderSide>>>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<OutlinedBorderRef>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<OutlinedBorderRef>>>>: UnsafeUnpin, *const RcInner<dyn WidgetStateProperty<Option<Rc<dyn MouseCursor>>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetStateProperty<Option<Rc<dyn MouseCursor>>>>>: UnsafeUnpin, *const RcInner<dyn InteractiveInkFeatureFactory> is !null: UnsafeUnpin, PhantomData<RcInner<dyn InteractiveInkFeatureFactory>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, AnyElement, &HashSet<WidgetState>, Option<Rc<dyn Widget>>) -> Rc<dyn Widget>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, AnyElement, &HashSet<WidgetState>, Option<Rc<dyn Widget>>) -> Rc<dyn Widget>>>: UnsafeUnpin, *const (): 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> 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>