Skip to main content

SnackBar

Struct SnackBar 

Source
pub struct SnackBar {
Show 20 fields pub key: Option<KeyRef>, pub content: WidgetRef, pub background_color: Option<Color>, pub elevation: Option<f64>, pub margin: Option<EdgeInsetsGeometry>, pub padding: Option<EdgeInsetsGeometry>, pub width: Option<f64>, pub shape: Option<ShapeBorderRef>, pub hit_test_behavior: Option<HitTestBehavior>, pub behavior: Option<SnackBarBehavior>, pub action: Option<SnackBarAction>, pub action_overflow_threshold: Option<f64>, pub show_close_icon: Option<bool>, pub close_icon_color: Option<AnyColor>, pub duration: Duration, pub persist: Option<bool>, pub animation: Option<AnimationRef<f64>>, pub on_visible: Option<VoidCallback>, pub dismiss_direction: Option<DismissDirection>, pub clip_behavior: Clip,
}
Expand description

A lightweight message with an optional action which briefly displays at the bottom of the screen.

Learn more about SnackBar on the Flutter YouTube channel.

To display a snack bar, call ScaffoldMessenger::of(app, context).show_snack_bar(..), passing an instance of SnackBar that describes the message.

To control how long the SnackBar remains visible, specify a duration.

A SnackBar with an action will not time out when TalkBack or VoiceOver are enabled. This is controlled by AccessibilityFeatures.accessibleNavigation.

During page transitions, the SnackBar will smoothly animate to its location on the other page. For example if the SnackBar::behavior is set to SnackBarBehavior::Floating and the next page has a floating action button, while the current one does not, the SnackBar will smoothly animate above the floating action button. It also works in the case of a back gesture transition.

See also:

  • ScaffoldMessenger::of, to obtain the current ScaffoldMessengerState, which manages the display and animation of snack bars.
  • ScaffoldMessengerState::show_snack_bar, which displays a SnackBar.
  • ScaffoldMessengerState::remove_current_snack_bar, which abruptly hides the currently displayed snack bar, if any, and allows the next to be displayed.
  • SnackBarAction, which is used to specify an action button to show on the snack bar.
  • SnackBarThemeData, to configure the default property values for SnackBar widgets.
  • https://material.io/design/components/snackbars.html

Fields§

§key: Option<KeyRef>§content: WidgetRef

The primary content of the snack bar.

Typically a Text widget.

§background_color: Option<Color>

The snack bar’s background color.

If not specified, the ambient SnackBarThemeData::background_color is used. If that is not specified it will default to a dark variation of ColorScheme.surface for light themes, or ColorScheme.onSurface for dark themes.

§elevation: Option<f64>

The z-coordinate at which to place the snack bar. This controls the size of the shadow below the snack bar.

Defines the card’s Material.elevation.

If this property is null, then the ambient SnackBarThemeData::elevation is used, if that is also null, the default value is 6.0.

§margin: Option<EdgeInsetsGeometry>

Empty space to surround the snack bar.

This property is only used when behavior is SnackBarBehavior::Floating. It can not be used if width is specified.

If this property is null, then the ambient SnackBarThemeData::inset_padding is used. If that is also null, then the default is EdgeInsets.fromLTRB(15.0, 5.0, 15.0, 10.0).

If this property is not null and hit_test_behavior is null, then hit_test_behavior default is HitTestBehavior::DeferToChild.

§padding: Option<EdgeInsetsGeometry>

The amount of padding to apply to the snack bar’s content and optional action.

If this property is null, the default padding values are as follows:

If this property is not null, the padding is as follows:

  • content
    • Left, top and bottom paddings are assigned normally.
    • Right padding is assigned normally if there is no action, otherwise 0.
  • action
    • Left padding is replaced with half the right padding.
    • Top and bottom paddings are assigned normally.
    • Right padding is replaced with one and a half times the right padding.
§width: Option<f64>

The width of the snack bar.

If width is specified, the snack bar will be centered horizontally in the available space. This property is only used when behavior is SnackBarBehavior::Floating. It can not be used if margin is specified.

If this property is null, then the ambient SnackBarThemeData::width is used. If that is null, the snack bar will take up the full device width less the margin.

§shape: Option<ShapeBorderRef>

The shape of the snack bar’s Material.

Defines the snack bar’s Material.shape.

If this property is null, then the ambient SnackBarThemeData::shape is used. If that’s null then the shape will depend on the SnackBarBehavior. For SnackBarBehavior::Fixed, no overriding shape is specified, so the SnackBar is rectangular. For SnackBarBehavior::Floating, it uses a RoundedRectangleBorder with a circular corner radius of 4.0.

§hit_test_behavior: Option<HitTestBehavior>

Defines how the snack bar area, including margin, will behave during hit testing.

If this property is null, and margin is not null or the ambient SnackBarThemeData::inset_padding is not null, then HitTestBehavior::DeferToChild is used by default.

Please refer to HitTestBehavior for a detailed explanation of every behavior.

§behavior: Option<SnackBarBehavior>

This defines the behavior and location of the snack bar.

Defines where a SnackBar should appear within a Scaffold and how its location should be adjusted when the scaffold also includes a FloatingActionButton or a BottomNavigationBar

If this property is null, then the ambient SnackBarThemeData::behavior is used. If that is null, then the default is SnackBarBehavior::Fixed.

If this value is SnackBarBehavior::Floating, the length of the bar is defined by either width or margin.

§action: Option<SnackBarAction>

(optional) An action that the user can take based on the snack bar.

For example, the snack bar might let the user undo the operation that prompted the snackbar. Snack bars can have at most one action.

The action should not be “dismiss” or “cancel”.

§action_overflow_threshold: Option<f64>

(optional) The percentage threshold for action widget’s width before it overflows to a new line.

Must be between 0 and 1. If the width of the action divided by the total snackbar width is greater than this percentage, the action will appear below the content.

At a value of 0, the action will always overflow to a new line.

Defaults to 0.25.

§show_close_icon: Option<bool>

(optional) Whether to include a “close” icon widget.

Tapping the icon will close the snack bar.

§close_icon_color: Option<AnyColor>

An optional color for the close icon, if show_close_icon is true.

If this property is null, then the ambient SnackBarThemeData::close_icon_color is used. If that is null, then the default is inverse surface.

If close_icon_color is a WidgetStateColor, then the icon color will be resolved against the set of WidgetStates that the action text is in, thus allowing for different colors for states such as pressed, hovered and others.

§duration: Duration

The amount of time the snack bar should be displayed.

Defaults to 4.0s.

See also:

§persist: Option<bool>

Whether the snack bar will stay or auto-dismiss after timeout, as given; read effective_persist.

If true, the snack bar remains visible even after the timeout, until the user taps the action button or the close icon.

If false, the snack bar will be dismissed after the timeout.

If not provided, but the snackbar action is not null, the snackbar will persist as well.

§animation: Option<AnimationRef<f64>>

The animation driving the entrance and exit of the snack bar.

§on_visible: Option<VoidCallback>

Called the first time that the snackbar is visible within a Scaffold.

When multiple Scaffolds are registered to the same ScaffoldMessengerState, on_visible is called once for each scaffold.

See also:

  • ScaffoldMessenger, which manages SnackBars for Scaffold descendants.
§dismiss_direction: Option<DismissDirection>

The direction in which the SnackBar can be dismissed.

If this property is null, then the ambient SnackBarThemeData::dismiss_direction is used. If that is null, then the default is DismissDirection::Down.

§clip_behavior: Clip

The content will be clipped (or not) according to this option.

See the enum Clip for details of all possible options and their common use cases.

Defaults to Clip::HardEdge.

Implementations§

Source§

impl SnackBar

Source

pub fn new<K>(content: impl IntoWidget<K>) -> SnackBar

Creates a snack bar; Dart’s optional arguments are the setters.

The elevation must be null or non-negative.

Source

pub fn key(self, key: KeyRef) -> SnackBar

Dart SnackBar(key:).

Source

pub fn background_color(self, background_color: Color) -> SnackBar

Dart SnackBar(backgroundColor:).

Source

pub fn elevation(self, elevation: f64) -> SnackBar

Dart SnackBar(elevation:).

Source

pub fn margin(self, margin: impl Into<EdgeInsetsGeometry>) -> SnackBar

Dart SnackBar(margin:).

Source

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

Dart SnackBar(padding:).

Source

pub fn width(self, width: f64) -> SnackBar

Dart SnackBar(width:).

Source

pub fn shape(self, shape: impl Into<ShapeBorderRef>) -> SnackBar

Dart SnackBar(shape:).

Source

pub fn hit_test_behavior(self, hit_test_behavior: HitTestBehavior) -> SnackBar

Dart SnackBar(hitTestBehavior:).

Source

pub fn behavior(self, behavior: SnackBarBehavior) -> SnackBar

Dart SnackBar(behavior:).

Source

pub fn action(self, action: SnackBarAction) -> SnackBar

Dart SnackBar(action:).

Source

pub fn action_overflow_threshold( self, action_overflow_threshold: f64, ) -> SnackBar

Dart SnackBar(actionOverflowThreshold:).

Source

pub fn show_close_icon(self, show_close_icon: bool) -> SnackBar

Dart SnackBar(showCloseIcon:).

Source

pub fn close_icon_color(self, close_icon_color: impl Into<AnyColor>) -> SnackBar

Dart SnackBar(closeIconColor:).

Source

pub fn duration(self, duration: Duration) -> SnackBar

Dart SnackBar(duration:).

Source

pub fn persist(self, persist: bool) -> SnackBar

Dart SnackBar(persist:).

Source

pub fn animation(self, animation: AnimationRef<f64>) -> SnackBar

Dart SnackBar(animation:).

Source

pub fn on_visible(self, on_visible: impl Fn(&mut App) + 'static) -> SnackBar

Dart SnackBar(onVisible:).

Source

pub fn dismiss_direction(self, dismiss_direction: DismissDirection) -> SnackBar

Dart SnackBar(dismissDirection:).

Source

pub fn clip_behavior(self, clip_behavior: Clip) -> SnackBar

Dart SnackBar(clipBehavior:).

Source

pub fn effective_persist(&self) -> bool

Dart’s persist field, as its constructor fills it: persist ?? action != null.

Source

pub fn create_animation_controller( app: &mut App, vsync: impl TickerProvider, duration: Option<Duration>, reverse_duration: Option<Duration>, ) -> Ref<AnimationController>

Creates an animation controller useful for driving a snack bar’s entrance and exit animation.

Source

pub fn with_animation( &self, new_animation: AnimationRef<f64>, fallback_key: Option<KeyRef>, ) -> SnackBar

Creates a copy of this snack bar but with the animation replaced with the given animation.

If the original snack bar lacks a key, the newly created snack bar will use the given fallback key.

Trait Implementations§

Source§

impl Clone for SnackBar

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 Debug for SnackBar

Source§

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

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

impl StatefulWidget for SnackBar

Source§

type State = SnackBarState

The state this widget creates.
Source§

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

Source§

fn create_state(&self) -> SnackBarState

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 debug_check_constructor(&self)

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 !RefUnwindSafe for SnackBar

§

impl !Send for SnackBar

§

impl !Sync for SnackBar

§

impl !UnwindSafe for SnackBar

§

impl Forget for SnackBar

§

impl Freeze for SnackBar
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, *const RcInner<dyn Widget> is !null: Freeze, PhantomData<RcInner<dyn Widget>>: Freeze, *const RcInner<dyn ShapeBorder> is !null: Freeze, PhantomData<RcInner<dyn ShapeBorder>>: Freeze, &'static dyn ColorExtension: Freeze, *const RcInner<dyn ColorExtension> is !null: Freeze, PhantomData<RcInner<dyn ColorExtension>>: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze, *const RcInner<dyn Fn(&mut App)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App)>>: Freeze, NonZero<u32>: Freeze, &'static AnimationVTable<f64>: Freeze, *const RcInner<Keep> is !null: Freeze, PhantomData<RcInner<Keep>>: Freeze,

§

impl Move for SnackBar

§

impl Unpin for SnackBar
where Rc<dyn Key>: Unpin, Rc<dyn Widget>: Unpin, Rc<dyn ShapeBorder>: Unpin, &'static dyn ColorExtension: Unpin, Rc<dyn ColorExtension>: Unpin, *const u8 is !null: Unpin, Rc<dyn Fn(&mut App)>: Unpin, NonZero<u32>: Unpin, &'static AnimationVTable<f64>: Unpin, Rc<Keep>: Unpin,

§

impl UnsafeUnpin for SnackBar
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, *const RcInner<dyn Widget> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Widget>>: UnsafeUnpin, *const RcInner<dyn ShapeBorder> is !null: UnsafeUnpin, PhantomData<RcInner<dyn ShapeBorder>>: UnsafeUnpin, &'static dyn ColorExtension: UnsafeUnpin, *const RcInner<dyn ColorExtension> is !null: UnsafeUnpin, PhantomData<RcInner<dyn ColorExtension>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<u8>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App)>>: UnsafeUnpin, NonZeroU32Inner: UnsafeUnpin, &'static AnimationVTable<f64>: UnsafeUnpin, *const RcInner<Keep> is !null: UnsafeUnpin, PhantomData<RcInner<Keep>>: 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>