26

I'm trying to show a snackbar.
After I click on a gesture detector, this snack has two buttons.
The problem is that the snackbar appears for seconds and then disappears.

So I have two questions:

  1. How to stop the snackbar from disappearing until the user takes an action and clicks on a button?
  2. Also, the snackbar has a height of the whole screen.
    How can I make it have a specific height in the bottom of the screen?
zx485
  • 28,498
  • 28
  • 50
  • 59
Mee
  • 1,413
  • 5
  • 24
  • 40
  • 2
    It's a good idea when troubleshooting to share relevant snippets of your code so we can help you figure out how to solve your issue. – Corina Sep 24 '18 at 20:16

4 Answers4

48

You can use a long duration

HomeScreen.scaffoldKey.currentState.showSnackBar(
    SnackBar(duration: const Duration(minutes: 5), content: Text(message)));

See also https://material.io/design/components/snackbars.html#behavior

Appearing and disappearing

Snackbars appear without warning, and don't require user interaction. They automatically disappear from the screen after a minimum of four seconds, and a maximum of ten seconds.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thank you for your response.I thought about this, but it will not make it disappear even after the user click on the button, it will remain the whole duration and this is not what i want. – Mee Sep 24 '18 at 16:59
  • 3
    You can call `hideCurrentSnackBar(...)` on `ScaffoldState` to remove it in `onPressed: ...` of the button https://docs.flutter.io/flutter/material/ScaffoldState/hideCurrentSnackBar.html – Günter Zöchbauer Sep 24 '18 at 17:01
  • How can i call it on ScaffoldState? – Mee Sep 24 '18 at 17:49
  • Use a GlobalKey that you pass to Scaffold. – Günter Zöchbauer Sep 24 '18 at 17:50
10

I wrote a little something about how i solved this https://dev.to/eyewritecode/persisting-flutter-snackbar-until-user-interacts-with-it-2583

You basically have to set a long duration

duration: Duration(days: 1)

And call the following whenever you want to hide it.

Scaffold.of(context).hideCurrentSnackBar();
Eyewritecode
  • 151
  • 1
  • 8
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/25946835) – Tal Z Apr 24 '20 at 14:59
  • My bad, I've added more details. – Eyewritecode Apr 24 '20 at 15:10
  • 4
    "Scaffold.of(context).hideCurrentSnackBar();" has been deprecated. The latest one is "ScaffoldMessenger.of(context).hideCurrentSnackBar();". Thanks – Kamlesh May 12 '21 at 09:23
-2

You can change the height of a snackbar by setting a different Max Number of lines (default is 2 usually):

You can do this like this (example with 10 lines):

 View snackbarView = snackbar.getView();
            TextView snackTextView = (TextView) snackbarView.findViewById(
            com.google.android.material.R.id.snackbar_text);

            snackTextView.setMaxLines(10);
            snackbar.show();
-4
final Snackbar snack = Snackbar.make(findViewById(android.R.id.content),  helpMsg, Snackbar.LENGTH_INDEFINITE);
snack.setAction("OK", new View.OnClickListener() {
       @Override
       public void onClick(View v) {
          // Respond to the click dismiss is automatic with this

           }
});
View view = snack.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams();
params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snack.show();
JDPearlman
  • 51
  • 2