Snackbar duration and height
Asked Answered
B

4

27

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?
Beaulieu answered 24/9, 2018 at 16:28 Comment(1)
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.Superiority
G
51

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.

Greek answered 24/9, 2018 at 16:39 Comment(4)
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.Beaulieu
You can call hideCurrentSnackBar(...) on ScaffoldState to remove it in onPressed: ... of the button docs.flutter.io/flutter/material/ScaffoldState/…Wellrounded
How can i call it on ScaffoldState?Beaulieu
Use a GlobalKey that you pass to Scaffold.Wellrounded
C
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();
Carvalho answered 24/4, 2020 at 13:36 Comment(3)
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 ReviewZooplankton
My bad, I've added more details.Carvalho
"Scaffold.of(context).hideCurrentSnackBar();" has been deprecated. The latest one is "ScaffoldMessenger.of(context).hideCurrentSnackBar();". ThanksProline
F
-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();
Freestyle answered 19/10, 2021 at 10:50 Comment(0)
C
-3
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();
Cates answered 24/3, 2019 at 10:8 Comment(2)
Can you show your code so we can see why you get full pageCates
Did you carefully read the question tags? There is no android or java tagFoscalina

© 2022 - 2024 — McMap. All rights reserved.