Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change position of a Flutter Snackbar

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,
        ),
      ),
    );
  }
}
like image 460
mongy910 Avatar asked Sep 11 '18 00:09

mongy910


4 Answers

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,);
like image 198
K Vij Avatar answered Oct 12 '22 13:10

K Vij


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!"),
);
like image 41
Sameera Damith Avatar answered Oct 12 '22 14:10

Sameera Damith


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

like image 2
siega Avatar answered Oct 12 '22 15:10

siega


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

like image 1
Sahandevs Avatar answered Oct 12 '22 14:10

Sahandevs