Skip to main content

DropdownButton

Struct DropdownButton 

Source
pub struct DropdownButton<T: 'static> {
Show 32 fields pub key: Option<KeyRef>, pub items: Option<Vec<DropdownMenuItem<T>>>, pub value: Option<T>, pub hint: Option<WidgetRef>, pub disabled_hint: Option<WidgetRef>, pub on_changed: Option<ValueChanged<Option<T>>>, pub on_tap: Option<Listener>, pub selected_item_builder: Option<DropdownButtonBuilder>, pub elevation: i32, pub style: Option<TextStyle>, pub underline: Option<WidgetRef>, pub icon: Option<WidgetRef>, pub icon_disabled_color: Option<Color>, pub icon_enabled_color: Option<Color>, pub icon_size: f64, pub is_dense: bool, pub is_expanded: bool, pub item_height: Option<f64>, pub menu_width: Option<f64>, pub focus_color: Option<Color>, pub focus_node: Option<FocusNodeRef>, pub autofocus: bool, pub dropdown_color: Option<Color>, pub menu_max_height: Option<f64>, pub enable_feedback: Option<bool>, pub alignment: AlignmentGeometry, pub border_radius: Option<BorderRadius>, pub padding: Option<EdgeInsetsGeometry>, pub barrier_dismissible: bool, pub mouse_cursor: Option<MouseCursorRef>, pub dropdown_menu_item_mouse_cursor: Option<MouseCursorRef>, pub is_vertically_expanded: bool, /* private fields */
}
Expand description

A Material Design button for selecting from a list of items.

A dropdown button lets the user select from a number of items. The button shows the currently selected item as well as an arrow that opens a menu for selecting another item.

There is a Material 3 version of this component, DropdownMenu, that is preferred for applications that are configured for Material 3 (see ThemeData::use_material3).

One ancestor must be a Material widget and typically this is provided by the app’s Scaffold.

The type T is the type of the value that each dropdown item represents. All the entries in a given menu must represent values with consistent types. Typically, an enum is used. Each DropdownMenuItem in items must be specialized with that same type argument.

The on_changed callback should update a state variable that defines the dropdown’s value. It should also call State::set_state to rebuild the dropdown with the new value.

If the on_changed callback is null or the list of items is null then the dropdown button will be disabled, i.e. its arrow will be displayed in grey and it will not respond to input. A disabled button will display the disabled_hint widget if it is non-null. However, if disabled_hint is null and hint is non-null, the hint widget will instead be displayed.

See also:

Fields§

§key: Option<KeyRef>§items: Option<Vec<DropdownMenuItem<T>>>

The list of items the user can select.

If the on_changed callback is null or the list of items is null then the dropdown button will be disabled, i.e. its arrow will be displayed in grey and it will not respond to input.

§value: Option<T>

The value of the currently selected DropdownMenuItem.

If value is null and the button is enabled, hint will be displayed if it is non-null.

If value is null and the button is disabled, disabled_hint will be displayed if it is non-null. If disabled_hint is null, then hint will be displayed if it is non-null.

§hint: Option<WidgetRef>

A placeholder widget that is displayed by the dropdown button.

If value is null and the dropdown is enabled (items and on_changed are non-null), this widget is displayed as a placeholder for the dropdown button’s value.

If value is null and the dropdown is disabled and disabled_hint is null, this widget is used as the placeholder.

§disabled_hint: Option<WidgetRef>

A preferred placeholder widget that is displayed when the dropdown is disabled.

If value is null, the dropdown is disabled (items or on_changed is null), this widget is displayed as a placeholder for the dropdown button’s value.

§on_changed: Option<ValueChanged<Option<T>>>

Called when the user selects an item.

If the on_changed callback is null or the list of DropdownButton::items is null then the dropdown button will be disabled, i.e. its arrow will be displayed in grey and it will not respond to input. A disabled button will display the DropdownButton::disabled_hint widget if it is non-null. If DropdownButton::disabled_hint is also null but DropdownButton::hint is non-null, DropdownButton::hint will instead be displayed.

§on_tap: Option<Listener>

Called when the dropdown button is tapped.

This is distinct from on_changed, which is called when the user selects an item from the dropdown.

The callback will not be invoked if the dropdown button is disabled.

§selected_item_builder: Option<DropdownButtonBuilder>

A builder to customize the dropdown buttons corresponding to the DropdownMenuItems in items.

When a DropdownMenuItem is selected, the widget that will be displayed from the list corresponds to the DropdownMenuItem of the same index in items.

If this callback is null, the DropdownMenuItem from items that matches value will be displayed.

The list of widgets returned by this builder must be exactly the same length as the items list.

§elevation: i32

The z-coordinate at which to place the menu when open.

The following elevations have defined shadows: 1, 2, 3, 4, 6, 8, 9, 12, 16, and 24. See k_elevation_to_shadow.

Defaults to 8, the appropriate elevation for dropdown buttons.

§style: Option<TextStyle>

The text style to use for text in the dropdown button and the dropdown menu that appears when you tap the button.

To use a separate text style for selected item when it’s displayed within the dropdown button, consider using selected_item_builder.

Defaults to the TextTheme::title_medium value of the current ThemeData::text_theme of the current Theme.

§underline: Option<WidgetRef>

The widget to use for drawing the drop-down button’s underline.

Defaults to a 0.0 width bottom border with color 0xFFBDBDBD.

§icon: Option<WidgetRef>

The widget to use for the drop-down button’s icon.

Defaults to an Icon with the [Icons::ARROW_DROP_DOWN] glyph.

§icon_disabled_color: Option<Color>

The color of any Icon descendant of icon if this button is disabled, i.e. if on_changed is null.

Defaults to MaterialColor::shade400 of Colors::GREY when the theme’s ThemeData::brightness is Brightness::Light and to Colors::WHITE10 when it is Brightness::Dark.

§icon_enabled_color: Option<Color>

The color of any Icon descendant of icon if this button is enabled, i.e. if on_changed is defined.

Defaults to MaterialColor::shade700 of Colors::GREY when the theme’s ThemeData::brightness is Brightness::Light and to Colors::WHITE70 when it is Brightness::Dark.

§icon_size: f64

The size to use for the drop-down button’s down arrow icon button.

Defaults to 24.0.

§is_dense: bool

Reduce the button’s height.

By default this button’s height is the same as its menu items’ heights. If is_dense is true, the button’s height is reduced by about half. This can be useful when the button is embedded in a container that adds its own decorations, like InputDecorator.

§is_expanded: bool

Set the dropdown’s inner contents to horizontally fill its parent.

By default this button’s inner width is the minimum size of its contents. If is_expanded is true, the inner width is expanded to fill its surrounding container.

See also:

§item_height: Option<f64>

If null, then the menu item heights will vary according to each menu item’s intrinsic height.

The default value is K_MIN_INTERACTIVE_DIMENSION, which is also the minimum height for menu items.

If this value is null and there isn’t enough vertical room for the menu, then the menu’s initial scroll offset may not align the selected item with the dropdown button. That’s because, in this case, the initial scroll offset is computed as if all of the menu item heights were K_MIN_INTERACTIVE_DIMENSION.

§menu_width: Option<f64>

The width of the menu.

If it is not provided, the width of the menu is the width of the dropdown button.

§focus_color: Option<Color>

The color for the button’s Material when it has the input focus.

§focus_node: Option<FocusNodeRef>

{@macro flutter.widgets.Focus.focusNode}

§autofocus: bool

{@macro flutter.widgets.Focus.autofocus}

§dropdown_color: Option<Color>

The background color of the dropdown.

If it is not provided, the theme’s ThemeData::canvas_color will be used instead.

§menu_max_height: Option<f64>

The maximum height of the menu.

The maximum height of the menu must be at least one row shorter than the height of the app’s view. This ensures that a tappable area outside of the simple menu is present so the user can dismiss the menu.

If this property is set above the maximum allowable height threshold mentioned above, then the menu defaults to being padded at the top and bottom of the menu by at one menu item’s height.

§enable_feedback: Option<bool>

Whether detected gestures should provide acoustic and/or haptic feedback.

For example, on Android a tap will produce a clicking sound and a long-press will produce a short vibration, when feedback is enabled.

By default, platform-specific feedback is enabled.

§alignment: AlignmentGeometry

Defines how the hint or the selected item is positioned within the button.

Defaults to AlignmentDirectional::CENTER_START.

§border_radius: Option<BorderRadius>

Defines the corner radii of the menu’s rounded rectangle shape.

§padding: Option<EdgeInsetsGeometry>

Padding around the visible portion of the dropdown widget.

As the padding increases, the size of the DropdownButton will also increase. The padding is included in the clickable area of the dropdown widget, so this can make the widget easier to click.

Padding can be useful when used with a custom border. The clickable area will stay flush with the border, as opposed to an external Padding widget which will leave a non-clickable gap.

§barrier_dismissible: bool

Determines whether tapping outside the dropdown will close it.

Defaults to true.

§mouse_cursor: Option<MouseCursorRef>

The cursor for a mouse pointer when it enters or is hovering over this button.

If this property is null, WidgetStateMouseCursor::adaptive_clickable will be used.

§dropdown_menu_item_mouse_cursor: Option<MouseCursorRef>

The cursor for a mouse pointer when it enters or is hovering over the this button’s items.

If this property is null, WidgetStateMouseCursor::adaptive_clickable will be used.

§is_vertically_expanded: bool

Whether the dropdown’s inner contents expand to fill the available vertical space.

When true, the inner height of the dropdown is expanded to match its parent container. When false, the inner height is determined by the height of the selected item (its intrinsic height).

This is particularly useful when the dropdown is used inside a fixed-height container, ensuring that the underline and items align properly within the allocated space.

Defaults to false for DropdownButtonFormField to ensure the input decorator handles the vertical constraints correctly by default.

See also:

Implementations§

Source§

impl<T: Clone + PartialEq + Debug + 'static> DropdownButton<T>

Source

pub fn new( items: Option<Vec<DropdownMenuItem<T>>>, on_changed: Option<ValueChanged<Option<T>>>, ) -> DropdownButton<T>

Creates a dropdown button.

The items must have distinct values. If value isn’t null then it must be equal to one of the DropdownMenuItem values. If items or on_changed is null, the button will be disabled, the down arrow will be greyed out.

If value is null and the button is enabled, hint will be displayed if it is non-null.

If value is null and the button is disabled, disabled_hint will be displayed if it is non-null. If disabled_hint is null, then hint will be displayed if it is non-null.

The dropdown_color argument specifies the background color of the dropdown when it is open. If it is null, the current theme’s ThemeData::canvas_color will be used instead.

Source

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

Dart DropdownButton(key:).

Source

pub fn selected_item_builder( self, selected_item_builder: impl Fn(&mut App, BuildContext) -> Vec<WidgetRef> + 'static, ) -> DropdownButton<T>

Dart DropdownButton(selectedItemBuilder:).

Source

pub fn value(self, value: Option<T>) -> DropdownButton<T>

Dart DropdownButton(value:).

Source

pub fn hint<K>(self, hint: impl IntoWidget<K>) -> DropdownButton<T>

Dart DropdownButton(hint:).

Source

pub fn disabled_hint<K>( self, disabled_hint: impl IntoWidget<K>, ) -> DropdownButton<T>

Dart DropdownButton(disabledHint:).

Source

pub fn on_tap(self, on_tap: Listener) -> DropdownButton<T>

Dart DropdownButton(onTap:).

Source

pub fn elevation(self, elevation: i32) -> DropdownButton<T>

Dart DropdownButton(elevation:).

Source

pub fn style(self, style: TextStyle) -> DropdownButton<T>

Dart DropdownButton(style:).

Source

pub fn underline<K>(self, underline: impl IntoWidget<K>) -> DropdownButton<T>

Dart DropdownButton(underline:).

Source

pub fn icon<K>(self, icon: impl IntoWidget<K>) -> DropdownButton<T>

Dart DropdownButton(icon:).

Source

pub fn icon_disabled_color( self, icon_disabled_color: Color, ) -> DropdownButton<T>

Dart DropdownButton(iconDisabledColor:).

Source

pub fn icon_enabled_color(self, icon_enabled_color: Color) -> DropdownButton<T>

Dart DropdownButton(iconEnabledColor:).

Source

pub fn icon_size(self, icon_size: f64) -> DropdownButton<T>

Dart DropdownButton(iconSize:).

Source

pub fn is_dense(self, is_dense: bool) -> DropdownButton<T>

Dart DropdownButton(isDense:).

Source

pub fn is_expanded(self, is_expanded: bool) -> DropdownButton<T>

Dart DropdownButton(isExpanded:).

Source

pub fn item_height(self, item_height: Option<f64>) -> DropdownButton<T>

Dart DropdownButton(itemHeight:); None lets each item take its intrinsic height.

Source

pub fn menu_width(self, menu_width: f64) -> DropdownButton<T>

Dart DropdownButton(menuWidth:).

Source

pub fn focus_color(self, focus_color: Color) -> DropdownButton<T>

Dart DropdownButton(focusColor:).

Source

pub fn focus_node(self, focus_node: FocusNodeRef) -> DropdownButton<T>

Dart DropdownButton(focusNode:).

Source

pub fn autofocus(self, autofocus: bool) -> DropdownButton<T>

Dart DropdownButton(autofocus:).

Source

pub fn dropdown_color(self, dropdown_color: Color) -> DropdownButton<T>

Dart DropdownButton(dropdownColor:).

Source

pub fn menu_max_height(self, menu_max_height: f64) -> DropdownButton<T>

Dart DropdownButton(menuMaxHeight:).

Source

pub fn enable_feedback(self, enable_feedback: bool) -> DropdownButton<T>

Dart DropdownButton(enableFeedback:).

Source

pub fn alignment(self, alignment: AlignmentGeometry) -> DropdownButton<T>

Dart DropdownButton(alignment:).

Source

pub fn border_radius(self, border_radius: BorderRadius) -> DropdownButton<T>

Dart DropdownButton(borderRadius:).

Source

pub fn padding( self, padding: impl Into<EdgeInsetsGeometry>, ) -> DropdownButton<T>

Dart DropdownButton(padding:).

Source

pub fn barrier_dismissible(self, barrier_dismissible: bool) -> DropdownButton<T>

Dart DropdownButton(barrierDismissible:).

Source

pub fn mouse_cursor(self, mouse_cursor: MouseCursorRef) -> DropdownButton<T>

Dart DropdownButton(mouseCursor:).

Source

pub fn dropdown_menu_item_mouse_cursor( self, dropdown_menu_item_mouse_cursor: MouseCursorRef, ) -> DropdownButton<T>

Dart DropdownButton(dropdownMenuItemMouseCursor:).

Trait Implementations§

Source§

impl<T: 'static> Debug for DropdownButton<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 DropdownButton<T>

Source§

type State = DropdownButtonState<T>

The state this widget creates.
Source§

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

Source§

fn debug_check_constructor(&self)

Source§

fn create_state(&self) -> DropdownButtonState<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 DropdownButton<T>

§

impl<T> !Send for DropdownButton<T>

§

impl<T> !Sync for DropdownButton<T>

§

impl<T> !UnwindSafe for DropdownButton<T>

§

impl<T> Forget for DropdownButton<T>

§

impl<T> Freeze for DropdownButton<T>
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const u8 is !null: Freeze, PhantomData<DropdownMenuItem<T>>: Freeze, T: Freeze, *const RcInner<dyn Widget> is !null: Freeze, PhantomData<RcInner<dyn Widget>>: Freeze, *const RcInner<dyn Fn(&mut App, Option<T>)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, Option<T>)>>: Freeze, *const RcInner<dyn Fn(&mut App)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App)>>: Freeze, NonZero<u32>: Freeze, *const (): Freeze, *const RcInner<dyn Fn(&mut App, AnyElement) -> Vec<Rc<dyn Widget>>> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, AnyElement) -> Vec<Rc<dyn Widget>>>>: Freeze, &'static dyn ColorExtension: Freeze, *const RcInner<dyn ColorExtension> is !null: Freeze, PhantomData<RcInner<dyn ColorExtension>>: 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, &'static FocusNodeVTable: Freeze, *const RcInner<Keep> is !null: Freeze, PhantomData<RcInner<Keep>>: Freeze, *const RcInner<dyn MouseCursor> is !null: Freeze, PhantomData<RcInner<dyn MouseCursor>>: Freeze, *const RcInner<dyn TextStyleExtension> is !null: Freeze, PhantomData<RcInner<dyn TextStyleExtension>>: Freeze, *const RcInner<dyn InputBorder> is !null: Freeze, PhantomData<RcInner<dyn InputBorder>>: Freeze,

§

impl<T> Move for DropdownButton<T>

§

impl<T> Unpin for DropdownButton<T>
where T: Unpin, Rc<dyn Key>: Unpin, *const u8 is !null: Unpin, Rc<dyn Fn(&mut App)>: Unpin, NonZero<u32>: Unpin, *const (): Unpin, Rc<dyn Widget>: Unpin, Rc<dyn Fn(&mut App, Option<T>)>: Unpin, Rc<dyn Fn(&mut App, AnyElement) -> Vec<Rc<dyn Widget>>>: Unpin, &'static dyn ColorExtension: Unpin, Rc<dyn ColorExtension>: Unpin, Rc<Paint>: Unpin, &'static FocusNodeVTable: Unpin, Rc<Keep>: Unpin, Rc<dyn MouseCursor>: Unpin, Rc<dyn TextStyleExtension>: Unpin, Rc<dyn InputBorder>: Unpin,

§

impl<T> UnsafeUnpin for DropdownButton<T>
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<DropdownMenuItem<T>>: UnsafeUnpin, T: UnsafeUnpin, *const RcInner<dyn Widget> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Widget>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, Option<T>)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, Option<T>)>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App)>>: UnsafeUnpin, NonZeroU32Inner: UnsafeUnpin, *const (): UnsafeUnpin, *const RcInner<dyn Fn(&mut App, AnyElement) -> Vec<Rc<dyn Widget>>> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, AnyElement) -> Vec<Rc<dyn Widget>>>>: UnsafeUnpin, &'static dyn ColorExtension: UnsafeUnpin, *const RcInner<dyn ColorExtension> is !null: UnsafeUnpin, PhantomData<RcInner<dyn ColorExtension>>: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, PhantomData<String>: UnsafeUnpin, *const RcInner<Paint> is !null: UnsafeUnpin, PhantomData<RcInner<Paint>>: UnsafeUnpin, PhantomData<Shadow>: UnsafeUnpin, PhantomData<FontFeature>: UnsafeUnpin, PhantomData<FontVariation>: UnsafeUnpin, &'static FocusNodeVTable: UnsafeUnpin, *const RcInner<Keep> is !null: UnsafeUnpin, PhantomData<RcInner<Keep>>: UnsafeUnpin, *const RcInner<dyn MouseCursor> is !null: UnsafeUnpin, PhantomData<RcInner<dyn MouseCursor>>: UnsafeUnpin, *const RcInner<dyn TextStyleExtension> is !null: UnsafeUnpin, PhantomData<RcInner<dyn TextStyleExtension>>: UnsafeUnpin, *const RcInner<dyn InputBorder> is !null: UnsafeUnpin, PhantomData<RcInner<dyn InputBorder>>: 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> 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, 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>