Brutal
Brutal

Reputation: 922

Flutter: How to create a persistent (infinite) SnackBar?

I would like to create a persistent (infinite) SnackBar in a Flutter application—a SnackBar that remains visible indefinitely and does not dismiss itself automatically.

Could you please assist me with this?

Upvotes: 19

Views: 15990

Answers (1)

Tinus Jackson
Tinus Jackson

Reputation: 3663

To make a SnackBar persistent (infinite) in a Flutter application, you can set its duration property to a very large value that is unlikely to be reached during the app session, such as one year (365 days).

Example:

final snackBar = SnackBar(
  content: ...,
  action: ...,
  duration: Duration(days: 365), // Set duration to a large value.
);

Note: Using Duration(seconds: double.infinity) will not work because the seconds property accepts only integers. Casting double.infinity to int will result in NaN (Not a Number). Similarly, using double.maxFinite.toInt() will not work.

Learn more about the duration property and the SnackBar class.

Upvotes: 28

Related Questions