Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display snackbar infinite duration in flutter?

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

like image 415
KOrra Avatar asked Nov 09 '19 08:11

KOrra


People also ask

How do you give a SnackBar 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.

How do I set duration on SnackBar?

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.

How do you show SnackBar Flutter?

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”


1 Answers

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.

EDIT

You can try the following instead of using double.infinity

Duration(days: 365)

Reference

  • Duration
like image 73
Tinus Jackson Avatar answered Oct 19 '22 20:10

Tinus Jackson