Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a margin or padding to a Snackbar?

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 :

Target Picture

What I'm getting is this now :

Resultant Picture

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);
}
like image 442
Alok Avatar asked Mar 22 '19 14:03

Alok


People also ask

How do you customize SnackBar in Flutter?

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.


2 Answers

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

enter image description here

like image 169
Osama FelFel Avatar answered Nov 24 '22 00:11

Osama FelFel


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)))));
like image 34
spgodara Avatar answered Nov 24 '22 00:11

spgodara