Maciej Pelc
Maciej Pelc

Reputation: 31

how to make SnackBar not disappearing or hide after x seconds?

i want to make my SnackBar waiting for my actions, this is my current code:

Scaffold.of(context).showSnackBar(
                SnackBar(
                  action: SnackBarAction(
                    label: 'Dissmiss',
                    textColor: Colors.yellow,
                    onPressed: () {
                      Scaffold.of(context).hideCurrentSnackBar();
                    },
                  ),
                  content: TextField(
                    controller: _controller,
                  ),
                ),
              );

in this code i have 'Dissmiss' action, when pressed it will hide my SnackBar. If im waiting SnackBar is hiding automaticly. How i can make him wait or at least wait specific time?

Upvotes: 3

Views: 3654

Answers (3)

Nuqo
Nuqo

Reputation: 4091

You can simply add the duration: property:

SnackBar(
  duration: Duration(seconds: 5), //or 'days: 1' the snackBar to stay practically forever
  //...
),

The default duration of the SnackBar is 4 seconds and in here we override that with 5 seconds. You can simply look these properties up in the Flutter documentation: SnackBar class - material library

Upvotes: 2

MrWeeMan
MrWeeMan

Reputation: 1204

Snackbar can't wait for fo action and it is not designed to act like that as for material snackbar doc. The behaviour you are looking for is easily achievable with this library.

showFlash(
  persistent: true,
  ...
  builder: (..) {
    return Flash(..);
  }
)

Upvotes: 0

Mr Random
Mr Random

Reputation: 2218

add a duraton of 1 day

Scaffold.of(context).showSnackBar(
        SnackBar(
          action: SnackBarAction(
            label: 'Dissmiss',
            textColor: Colors.yellow,
            onPressed: () {
              Scaffold.of(context).hideCurrentSnackBar();
            },
          ),
          content: TextField(
              controller: _controller,
              ),
          duration: Duration(days: 1),
        ),
      );

Upvotes: 5

Related Questions