Change position of a Flutter Snackbar
Asked Answered
L

9

35

I want to display a simple disappearing error message above a button when certain conditions aren't met. It seems as if Flutter's Snackbar is well suited to this purpose.

However, I'm having difficulty changing the position of the Snackbar to be anything other than at the very bottom of the screen. Is this possible? If not, is there a Widget better suited for this purpose?

My current snackbar code:

class ContinueButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.only(
          bottom: 24.0, top: 24.0),
      child: Align(
        alignment: FractionalOffset.bottomCenter,
        child: MaterialButton(
          onPressed: () {
            final snackBar = SnackBar(
              content: Text('Yay! A SnackBar!'),
            );
            Scaffold.of(context).showSnackBar(snackBar);
          },

          child: Text('Continue'),
          height: 40.0,
          minWidth: 300.0,
          color: Colors.greenAccent,
        ),
      ),
    );
  }
}
Lincoln answered 11/9, 2018 at 0:11 Comment(0)
M
28

You can try by, setting behavior as SnackBarBehavior.floating and setting margin as much as you want.

SnackBar(
  behavior: SnackBarBehavior.floating,
  margin: EdgeInsets.only(bottom: 100.0),
  content: Text("Hello World!"),
);

The only issue with this solution is that everything underneath won't be clickable.

To resolve this clickable issue, set dismissDirection property to DismissDirection.none. This also means that the default property of a snackbar being able to be dismissed by dragging it down is lost. (default is DismissDirection.down)

SnackBar(
  behavior: SnackBarBehavior.floating,
  margin: EdgeInsets.only(bottom: 100.0),
  content: Text("Hello World!"),
  dismissDirection: DismissDirection.none
);
Maltose answered 30/10, 2021 at 10:21 Comment(2)
the only issue with this solution is that everything underneath will not be clickableSlipcase
And that's a big problem...Ansilma
Y
7

You can do this by placing a container inside the snackbar. Since snackbar can take any widget and you can change its background color to transparent, you can use a container with custom padding and borders to give an illusion of positioning.

SnackBar(content: Container(
  //color: Colors.white,
  decoration: BoxDecoration(color: Colors.white, border: Border.all(width: 2.0, color: Colors.black), borderRadius: BorderRadius.circular(20)),
  margin: EdgeInsets.fromLTRB(0, 0, 0, 75),
  child: Padding(
    padding: const EdgeInsets.all(8.0),
    child: Text('Yay! A SnackBar!'),
  ),
), backgroundColor: Colors.transparent, elevation: 1000, behavior: SnackBarBehavior.floating,);
Yoong answered 24/4, 2020 at 7:14 Comment(0)
C
3

Unfortunately, no. But you can use https://api.flutter.dev/flutter/widgets/Overlay-class.html to display a Widget over another Widget (In your case over and above like a Tooltip widget) and create a Widget similar to Snackbar Widget

Conspecific answered 18/9, 2019 at 12:1 Comment(0)
F
2

I'm afraid you can't do that.

A lightweight message with an optional action which briefly displays at the bottom of the screen.

You can just change it's behavior and elevation

Felton answered 18/9, 2019 at 11:47 Comment(0)
K
1

You can use Flushbar instead of SnackBar

check it for more detail

flushbar

Note: It has been discontinued as on 2022

Kellda answered 4/8, 2020 at 16:2 Comment(1)
due the discontinuation you might want to check another_flushbarKessia
N
1

Works for me:

You can wrap your container with 'Align'.

SnackBar(
    content: GestureDetector(
      onTap: () {
        controller?.close();
      },
      child: Align(
        alignment: Alignment.topCenter,
        child: Container( ........
Nabob answered 11/12, 2020 at 6:38 Comment(0)
P
1

I needed to add the SnackBar at the top of the screen too, and after a lot of searching, I finally found one here and will be sharing it in case anyone needs to add the SnackBar at the top too.

I did it like this:

final snackBar = SnackBar(
      showCloseIcon: true,
      closeIconColor: Colors.white,
      content: Text('your text', textAlign: TextAlign.center),
      dismissDirection: DismissDirection.up,
      behavior: SnackBarBehavior.floating,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(10.0),
      ),
      margin: EdgeInsets.only(
          bottom: MediaQuery.of(context).size.height - 70, left: 10, right: 10),
    );
    ScaffoldMessenger.of(context).showSnackBar(snackBar);

An important note is that this SnackBar will be at the top of all screens, meaning the screen below will not be clickable, so you will have to wait for the SnackBar to disappear or close it. According to all my studies, I was unable to make the lower screen clickable with just the SnackBar in this position, it will only be possible with the package.

Propraetor answered 18/10, 2023 at 12:59 Comment(0)
W
0

Bit late to the party but it's possible to use a ScaffoldMessenger to control the positioning. See https://docs.flutter.dev/release/breaking-changes/scaffold-messenger

Instead of using

ScaffoldMessenger.of(context).showSnackBar(snackBar);

You then wrap a Scaffold that you've positioned in your UI with a ScaffoldMesseneger and given it a key. Use this key to then show the snackBar:

final key = GlobalKey<ScaffoldMessengerState>();
ScaffoldMessenger(
      key: key,
      child: Scaffold(
        body: ...
...
key.currentState?.showSnackBar(snackBar);
Willis answered 7/2, 2023 at 9:34 Comment(0)
B
0

Just add a margin only property to the SnackBar and set the top bottom value.

margin: EdgeInsets.only(
     bottom: MediaQuery.of(context).size.height - 150,
  ),
Bathsheeb answered 14/3 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.