How to display snackbar infinite duration in flutter?

6,427

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

Share:
6,427
KOrra
Author by

KOrra

I am very serious programmer of Flutter Framework. I am an expert in one or more key components of my application; other developers come to me with questions. I have designed, developed, and maintained major features. I have proposed and/or led architectural changes.

Updated on December 15, 2022

Comments

  • KOrra
    KOrra over 1 year

    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