Skip to main content

SnackBar

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

page.show_dialog(ft.SnackBar(ft.Text("Opened snack bar")))
SnackBar
Opened snack bar

Inherits: DialogControl

Properties

Events

  • on_action - Called when action button is clicked.
  • on_visible - Called the first time that the snackbar is visible within the page.

Examples

Live example

Basic Example

import flet as ft


def main(page: ft.Page):
def on_click(e: ft.Event[ft.Button]):
page.show_dialog(ft.SnackBar(ft.Text("Hello, world!")))

page.add(ft.SafeArea(content=ft.Button("Open SnackBar", on_click=on_click)))


if __name__ == "__main__":
ft.run(main)
basic

Counter

import flet as ft


class Data:
def __init__(self) -> None:
self.counter = 0

def increment(self):
self.counter += 1

def decrement(self):
self.counter -= 1


data = Data()


def main(page: ft.Page):
page.title = "SnackBar Example"

snack_bar = ft.SnackBar(
content=ft.Text("You did it!"),
action="Undo it!",
on_action=lambda e: data.decrement(),
)

def handle_button_click(e: ft.Event[ft.Button]):
data.increment()
snack_bar.content.value = f"You did it x {data.counter}"
if not snack_bar.open:
page.show_dialog(snack_bar)
page.update()

page.add(
ft.SafeArea(content=ft.Button("Open SnackBar", on_click=handle_button_click))
)


if __name__ == "__main__":
ft.run(main)
Snack bar with counter
Snack bar with counter

Action

import flet as ft


def main(page: ft.Page):
def open_simple_action(e: ft.Event[ft.Button]):
page.show_dialog(
ft.SnackBar(
ft.Text("The file has been deleted."),
action="Undo",
on_action=lambda e: print("Simple Undo clicked"),
)
)

def open_custom_action(e: ft.Event[ft.Button]):
page.show_dialog(
ft.SnackBar(
ft.Text("The directory has been deleted."),
persist=False,
action=ft.SnackBarAction(
label="Undo delete",
text_color=ft.Colors.YELLOW,
bgcolor=ft.Colors.BLUE,
on_click=lambda e: print("Custom Undo clicked"),
),
)
)

page.add(
ft.SafeArea(
content=ft.Column(
controls=[
ft.Button(
"Open SnackBar with a Simple action",
on_click=open_simple_action,
),
ft.Button(
"Open SnackBar with a Custom action",
on_click=open_custom_action,
),
]
)
)
)


if __name__ == "__main__":
ft.run(main)
Snack bar with a simple action
Snack bar with a simple action
Snack bar with a custom action
Snack bar with a custom action

Properties

actionclass-attributeinstance-attribute

action: Union[str, SnackBarAction, None] = None

An optional 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_thresholdclass-attributeinstance-attribute

action_overflow_threshold: Annotated[Optional[Number], V.between(0.0, 1.0)] = 0.25

The percentage threshold for action's width before it overflows to a new line.

If the width of the snackbar's content is greater than this percentage of the width of the snackbar minus the width of its action, then the action will appear below the content.

At a value of 0.0, the action will not overflow to a new line.

Raises:

  • ValueError - If it is not between 0.0 and 1.0, inclusive.

behaviorclass-attributeinstance-attribute

behavior: Optional[SnackBarBehavior] = None

This defines the behavior and location of the snack bar.

Defines where a SnackBar should appear within a page and how its location should be adjusted when the page also includes a FloatingActionButton or a NavigationBar.

If None, flet.SnackBarTheme.behavior is used. If that's is also None, defaults to flet.SnackBarBehavior.FIXED.

Note

bgcolorclass-attributeinstance-attribute

bgcolor: Optional[ColorValue] = None

SnackBar background color.

clip_behaviorclass-attributeinstance-attribute

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

close_icon_colorclass-attributeinstance-attribute

close_icon_color: Optional[ColorValue] = None

The color of the close icon, if show_close_icon is True.

contentinstance-attribute

content: Annotated[StrOrControl, V.str_or_visible_control()]

The primary content of the snack bar.

Typically a Text control.

Raises:

  • ValueError - If it is neither a string nor a visible Control.

dismiss_directionclass-attributeinstance-attribute

dismiss_direction: Optional[DismissDirection] = None

The direction in which the SnackBar can be dismissed.

If None, flet.SnackBarTheme.dismiss_direction is used. If that's is also None, defaults to flet.DismissDirection.DOWN.

durationclass-attributeinstance-attribute

duration: DurationValue = field(default_factory=(lambda: Duration(milliseconds=4000)))

The amount of time this snack bar should stay open for.

elevationclass-attributeinstance-attribute

elevation: Annotated[Optional[Number], V.ge(0)] = None

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

Raises:

  • ValueError - If it is not greater than or equal to 0.

marginclass-attributeinstance-attribute

margin: Optional[MarginValue] = None

Empty space to surround the snack bar.

Has effect only when behavior=SnackBarBehavior.FLOATING and will be ignored if width is specified.

paddingclass-attributeinstance-attribute

padding: Optional[PaddingValue] = None

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

persistclass-attributeinstance-attribute

persist: Optional[bool] = None

Whether the snack bar will stay or auto-dismiss after timeout.

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.

shapeclass-attributeinstance-attribute

shape: Optional[OutlinedBorder] = None

The shape of this snack bar.

show_close_iconclass-attributeinstance-attribute

show_close_icon: bool = False

Whether to include a "close" icon widget.

Tapping the icon will close the snack bar.

widthclass-attributeinstance-attribute

width: Optional[Number] = None

The width of the snack bar.

If width is specified, the snack bar will be centered horizontally in the available space.

Note

Has effect only when behavior is flet.SnackBarBehavior.FLOATING. It can not be used if margin is specified.

Events

on_actionclass-attributeinstance-attribute

on_action: Optional[ControlEventHandler[SnackBar]] = None

Called when action button is clicked.

on_visibleclass-attributeinstance-attribute

on_visible: Optional[ControlEventHandler[SnackBar]] = None

Called the first time that the snackbar is visible within the page.

A button that can be used as an action in a SnackBar.

An action button for a SnackBar.

Note
  • Snack bar actions are always enabled. Instead of disabling a snack bar action, avoid including it in the snack bar in the first place.
  • Snack bar actions will only respond to first click. Subsequent clicks/presses are ignored.

Inherits: Control

Properties

Events

  • on_click - Called when this action button is clicked.

Properties

bgcolorclass-attributeinstance-attribute

bgcolor: Optional[ColorValue] = None

The button background fill color.

If None, flet.SnackBarTheme.action_bgcolor is used.

disabled_bgcolorclass-attributeinstance-attribute

disabled_bgcolor: Optional[ColorValue] = None

The button disabled background color. This color is shown after the action is dismissed.

If None, flet.SnackBarTheme.disabled_action_bgcolor is used.

disabled_text_colorclass-attributeinstance-attribute

disabled_text_color: Optional[ColorValue] = None

The button disabled label color. This color is shown after the action is dismissed.

labelinstance-attribute

label: str

The button's label.

text_colorclass-attributeinstance-attribute

text_color: Optional[ColorValue] = None

The button label color.

If None, flet.SnackBarTheme.action_text_color is used.

Events

on_clickclass-attributeinstance-attribute

on_click: Optional[ControlEventHandler[SnackBarAction]] = None

Called when this action button is clicked.