I created flutter project in android studio and tried to show snackbar infinite duration.This is my code
final snackBar = SnackBar(
content: Text('Cart:'+countProducts.toString()+" Products($countCost:sum)",style: TextStyle(color: Colors.black),),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(topLeft:Radius.circular(22),topRight:Radius.circular(22))),
backgroundColor: Colors.white70,
action: SnackBarAction(
textColor: Colors.blueAccent,
label: "Buy",
onPressed: () {
// Some code to undo the change.
},
),
);
Scaffold.of(context).showSnackBar(snackBar);
There is another button that will change snackbar text. But I do not want to it dismiss back.So how to display snackbar infinite duration in flutter
You can change the duration which a SnackBar is displayed by assigning a required duration to the backgroundColor property of SnackBar, as a Duration object.
The duration should be LENGTH_SHORT, LENGTH_LONG or LENGTH_INDEFINITE. When LENGTH_INDEFINITE is used, the snackbar will be displayed indefinite time and can be dismissed with swipe off. And you can set the duration of your snackbar by setDuration(int) method.
To get started building, displaying, and styling your SnackBar, first complete the following steps: Launch Android Studio or another IDE of your choice. Start a new Flutter project. Select Flutter Application and name the project something like “snackbardemo”
You can use the Duration property on the snackbar
Example
final snackBar = SnackBar(
content: Text('Cart:'+countProducts.toString()+" Products($countCost:sum)",style: TextStyle(color: Colors.black),),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(topLeft:Radius.circular(22),topRight:Radius.circular(22))),
backgroundColor: Colors.white70,
action: SnackBarAction(
textColor: Colors.blueAccent,
label: "Buy",
onPressed: () {
// Some code to undo the change.
},
),
duration: Duration(seconds: double.infinity),
//Gets problem int != double );
using Duration(seconds: double.infinity)
Not Sure if this is the best though.
You can try the following instead of using double.infinity
Duration(days: 365)
Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With