I have been working on the Snackbar and achieved in my project successfully. However, there is this little thing which I want to add to Snackbar and that is the margins
. I have seen in this link : Snackbars - Material Design
I really want my Snackbar to come like this :
What I'm getting is this now :
My code is :
final snackBar = SnackBar(
content: Text("Feild(s) are empty!"),
duration: new Duration(seconds: 1),
backgroundColor: Theme.of(context).primaryColor,
);
Scaffold.of(context).showSnackBar(snackBar);
}
We can create flutter snackbar in our application by calling its constructor and there is only one required property to create a snackbar which is content. Usually, we will use a Text widget for content since we need to show some message to the user. We can use other widgets instead if we want.
Flutter team have updated the snackbar to match the material design in this PR. You can simply get the new behavior by setting
behavior: SnackBarBehavior.floating
Here is a sample code
final snackBar = SnackBar(
elevation: 6.0,
backgroundColor: Configs.current.COLORS_PRIMARY,
behavior: SnackBarBehavior.floating,
content: Text(
"Snack bar test",
style: TextStyle(color: Colors.white),
),
);
and the result will look like this
Not sure about margins. Round corner SnackBar can be created like:
Scaffold
.of(context)
.showSnackBar(
SnackBar(
content: Text(message),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)))));
Use required border radius in above.
Update:
You can use floating SnackBar
to add default margins. Pass below to SnackBar
constructor:
Scaffold
.of(context)
.showSnackBar(
SnackBar(
content: Text(message),
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(20)))));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With