SnackBar with action no longer auto-dismisses
Summary
#The default behavior of a SnackBar
with an action has changed. Previously, a
SnackBar
with an action would not auto-dismiss if talkback was enabled.
Now, all SnackBar
s with an action default to a non-dismissible state until the
user interacts with the action button.
Context
#A SnackBar
with an action button is now treated as a more persistent
notification that requires user interaction. This change improves accessibility
and user experience by ensuring that critical notifications remain on the screen
until they are acknowledged.
Description of change
#This change aligns with the Material 3 design specifications for SnackBar
s:
- Old behavior: A
SnackBar
with an action button would auto-dismiss after a duration unless talkback was enabled. - New behavior: A
SnackBar
with an action button won't auto-dismiss; it remains on screen until dismissed by the user.
To override this behavior, an optional persist
property has
been added to SnackBar
. When persist
is true, the SnackBar
won't auto-dismiss
and remains on screen until manually dismissed by the user. When false, the
SnackBar
auto-dismisses after its standard duration, regardless of the
presence of an action. When null, the SnackBar
follows the default
behavior, which won't auto-dismiss if an action is present.
Migration guide
#To restore the old auto-dismiss behavior for a SnackBar with an action, set
persist
to false.
Code before migration:
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('This is a snackbar with an action.'),
action: SnackBarAction(
label: 'Action',
onPressed: () {
// Perform some action
},
),
),
);
Code after migration:
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: const Text('This is a snackbar with an action.'),
persist: false, // Add this line to restore auto-dismiss behavior
action: SnackBarAction(
label: 'Action',
onPressed: () {
// Perform some action
},
),
),
);
Timeline
#Landed in version: TBD In stable release: TBD
References
#API documentation:
Relevant PRs:
除非另有说明,本文档之所提及适用于 Flutter 的最新稳定版本,本页面最后更新时间: 2025-08-25。 查看文档源码 或者 为本页面内容提出建议.