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 thestylefield to configure the appearance of the button and its segments.ToggleButtons, a similar widget that was built for Material 2.SegmentedButtonshould be considered as a replacement forToggleButtons.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>See Widget::key.
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: AxisThe 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: boolDetermines 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: boolDetermines 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:
ButtonStyle::shadow_colorButtonStyle::elevationButtonStyle::side- which is used for both the outer shape and dividers between segments.ButtonStyle::shape
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:
ButtonStyle::text_styleButtonStyle::background_colorButtonStyle::foreground_colorButtonStyle::overlay_colorButtonStyle::surface_tint_colorButtonStyle::elevationButtonStyle::paddingButtonStyle::icon_colorButtonStyle::icon_sizeButtonStyle::mouse_cursorButtonStyle::visual_densityButtonStyle::tap_target_sizeButtonStyle::animation_durationButtonStyle::enable_feedbackButtonStyle::alignmentButtonStyle::splash_factory
If ButtonStyle::side is provided, WidgetStateProperty::resolve is used
for the following WidgetStates:
WidgetState::Focused.WidgetState::Hovered.WidgetState::Disabled.WidgetState::Selected.
show_selected_icon: boolDetermines 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>
impl<T: Clone + PartialEq + 'static> SegmentedButton<T>
Sourcepub fn new(
segments: Vec<ButtonSegment<T>>,
selected: Vec<T>,
) -> SegmentedButton<T>
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.
Sourcepub fn key(self, key: KeyRef) -> SegmentedButton<T>
pub fn key(self, key: KeyRef) -> SegmentedButton<T>
Dart SegmentedButton(key:).
Sourcepub fn direction(self, direction: Axis) -> SegmentedButton<T>
pub fn direction(self, direction: Axis) -> SegmentedButton<T>
Dart SegmentedButton(direction:).
Sourcepub fn on_selection_changed(
self,
on_selection_changed: impl Fn(&mut App, Vec<T>) + 'static,
) -> SegmentedButton<T>
pub fn on_selection_changed( self, on_selection_changed: impl Fn(&mut App, Vec<T>) + 'static, ) -> SegmentedButton<T>
Dart SegmentedButton(onSelectionChanged:).
Sourcepub fn multi_selection_enabled(
self,
multi_selection_enabled: bool,
) -> SegmentedButton<T>
pub fn multi_selection_enabled( self, multi_selection_enabled: bool, ) -> SegmentedButton<T>
Dart SegmentedButton(multiSelectionEnabled:).
Sourcepub fn empty_selection_allowed(
self,
empty_selection_allowed: bool,
) -> SegmentedButton<T>
pub fn empty_selection_allowed( self, empty_selection_allowed: bool, ) -> SegmentedButton<T>
Dart SegmentedButton(emptySelectionAllowed:).
Sourcepub fn expanded_insets(self, expanded_insets: EdgeInsets) -> SegmentedButton<T>
pub fn expanded_insets(self, expanded_insets: EdgeInsets) -> SegmentedButton<T>
Dart SegmentedButton(expandedInsets:).
Sourcepub fn style(self, style: ButtonStyle) -> SegmentedButton<T>
pub fn style(self, style: ButtonStyle) -> SegmentedButton<T>
Dart SegmentedButton(style:).
Sourcepub fn show_selected_icon(self, show_selected_icon: bool) -> SegmentedButton<T>
pub fn show_selected_icon(self, show_selected_icon: bool) -> SegmentedButton<T>
Dart SegmentedButton(showSelectedIcon:).
Sourcepub fn selected_icon<K>(
self,
selected_icon: impl IntoWidget<K>,
) -> SegmentedButton<T>
pub fn selected_icon<K>( self, selected_icon: impl IntoWidget<K>, ) -> SegmentedButton<T>
Dart SegmentedButton(selectedIcon:).
Source§impl<T> SegmentedButton<T>
impl<T> SegmentedButton<T>
Sourcepub fn style_from() -> SegmentedButtonStyleFromBuilder
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,
impl<T> Clone for SegmentedButton<T>where
T: Clone,
Source§fn clone(&self) -> SegmentedButton<T>
fn clone(&self) -> SegmentedButton<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Debug> Debug for SegmentedButton<T>
impl<T: Debug> Debug for SegmentedButton<T>
Source§impl<T: Clone + PartialEq + Debug + 'static> StatefulWidget for SegmentedButton<T>
impl<T: Clone + PartialEq + Debug + 'static> StatefulWidget for SegmentedButton<T>
Source§type State = SegmentedButtonState<T>
type State = SegmentedButtonState<T>
Source§fn create_state(&self) -> SegmentedButtonState<T>
fn create_state(&self) -> SegmentedButtonState<T>
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 as_preferred_size_widget(
&self,
) -> Option<&(dyn PreferredSizeWidget + 'static)>
fn as_preferred_size_widget( &self, ) -> Option<&(dyn PreferredSizeWidget + 'static)>
Source§fn type_name(&self) -> &'static str
fn type_name(&self) -> &'static str
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>where
T: Unpin,
Rc<dyn Key>: Unpin,
*const u8 is !null: Unpin,
Rc<dyn Widget>: Unpin,
Rc<dyn Fn(&mut App, Vec<T>)>: Unpin,
Rc<dyn WidgetStateProperty<Option<TextStyle>>>: Unpin,
Rc<dyn WidgetStateProperty<Option<Color>>>: Unpin,
Rc<dyn WidgetStateProperty<Option<f64>>>: Unpin,
Rc<dyn WidgetStateProperty<Option<EdgeInsetsGeometry>>>: Unpin,
Rc<dyn WidgetStateProperty<Option<Size>>>: Unpin,
Rc<dyn WidgetStateProperty<Option<BorderSide>>>: Unpin,
Rc<dyn WidgetStateProperty<Option<OutlinedBorderRef>>>: Unpin,
Rc<dyn WidgetStateProperty<Option<Rc<dyn MouseCursor>>>>: Unpin,
Rc<dyn InteractiveInkFeatureFactory>: Unpin,
Rc<dyn Fn(&mut App, AnyElement, &HashSet<WidgetState>, Option<Rc<dyn Widget>>) -> Rc<dyn Widget>>: Unpin,
*const (): Unpin,
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> 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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<W> IntoWidget<StatefulKind> for Wwhere
W: StatefulWidget,
impl<W> IntoWidget<StatefulKind> for Wwhere
W: StatefulWidget,
Source§fn into_widget(self) -> Rc<dyn Widget>
fn into_widget(self) -> Rc<dyn Widget>
creation_location.