0

How can I set spaces to snackbar from bottom and left/right sides?

katre
  • 173
  • 1
  • 5
  • 11

6 Answers6

0

Snackbars are somewhat difficult to change. I have found a package that is very easy to use and you can customize it however you like that will give you the same look as a snack bar with all of the customizational properties.

The Package is called FlushBar

This package is available for IOS and Android. I have personally used it and it works great. Additionally, you can use FlushBar without the use of a scaffold, where as with a snackbar you have to be able to refer to a scaffold to show a snackbar. Like so,

final snackBar = SnackBar(content: Text('Yay! A SnackBar!'));

// Find the Scaffold in the widget tree and use it to show a SnackBar.
Scaffold.of(context).showSnackBar(snackBar);

Here is a snippet that they provide to use the flushbar.

class YourAwesomeApp extends StatelessWidget {



@override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'YourAwesomeApp',
      home: Scaffold(
        Container(
          child: Center(
            child: MaterialButton(
              onPressed: (){
                Flushbar(
                  title:  "Hey Ninja",
                  message:  "Lorem Ipsum is simply dummy text of the printing and typesetting industry",
                  duration:  Duration(seconds: 3),              
                )..show(context);
              },
            ),
          ),
        ),
      ),
    );
  }
}
mrgnhnt96
  • 3,562
  • 1
  • 17
  • 36
  • I know flushbar. The flushbar in the structure I used caused different problems. I don't want to use it. – katre Dec 12 '19 at 19:40
0

Try flushbar, use margin property to give space.

Flushbar(
  message: "Lorem Ipsum is simply dummy text of the printing and typesetting industry",
  icon: Icon(
    Icons.info_outline,
    size: 28.0,
    color: Colors.blue[300],
    ),
  duration: Duration(seconds: 3),
  leftBarIndicatorColor: Colors.blue[300],
  margin: EdgeInsets.all(8),
  borderRadius: 8,
)..show(context);

OUTPUT:

enter image description here

Ravinder Kumar
  • 7,407
  • 3
  • 28
  • 54
  • Flushbar, causes image refresh errors in a list where I repeatedly delete and undo. Therefore do not want to use flushbar. – katre Dec 18 '19 at 16:30
0

You can wrap the content (text) of the snackbar with Container and provide margin or padding per your need. Working code sample below:

final snackBar = SnackBar(
            content: Container(
              margin: EdgeInsets.all(10),
              padding: EdgeInsets.only(left: 100, right: 50, bottom: 5),
              child: Text('Yay! A SnackBar!'),
            )

enter image description here

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Darshan
  • 10,550
  • 5
  • 49
  • 61
  • I'm trying to give margin and padding to the edges, not to the content of the snackbar. – katre Dec 18 '19 at 16:24
0

Try with this : A temporary solv:

Scaffold.of(context).showSnackBar(
    SnackBar(
       backgroundColor: Colors.transparent,
       content: Container(
         width: MediaQuery.of(context).size.width * 9,
         height: 30.0,
         color: Colors.red,
         padding: EdgeInsets.only(left: 100, right: 50, bottom: 5),
         child: Center(child:Text('Yay! A SnackBar!')),
      )
     )
   );

Or

Scaffold.of(context).showSnackBar(
   SnackBar(
      backgroundColor: Colors.transparent,
      content: Container(
      width: MediaQuery.of(context).size.width * 9,
      height: 30.0,
      decoration: BoxDecoration(
         color: Colors.red,
         borderRadius: BorderRadius.circular(10.0)
      ),
      padding: EdgeInsets.only(left: 100, right: 50, bottom: 5),
      child: Center(
         child:Text('Yay! A SnackBar!')
         ),
       )
      )
   );
Anil Chauhan
  • 658
  • 6
  • 17
  • Unfortunately, I didn't get a good look like this. Also, the shape is not applicable. (shape: RoundedRectangleBorder (borderRadius: BorderRadius.all (Radius.circular (10)))). – katre Dec 18 '19 at 16:41
  • @katre what you want to achieve? And for a circular container, you can just add "borderRadius: BorderRadius.circular(10.0)" . – Anil Chauhan Dec 19 '19 at 04:09
0

You can try this:

final snackBar = SnackBar(
    elevation: 6.0,
    behavior: SnackBarBehavior.floating,
    content: Text(
        "Snack bar test",
        style: TextStyle(color: Colors.white),
    ),
);

The main change here is the presence of the behavior attribute in the SnackBar class, that takes a SnackBarHehavior attribute as value.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Steve
  • 914
  • 8
  • 17
  • To use the SnackBarBehavior.floating parameter, the page must have a bottom navigation bar. Otherwise floating will not work. – katre Dec 18 '19 at 16:20
0

Add behaviour property to Snackbar as SnackBarBehavior.floating. See the code below.

ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text('Error from Stripe: ${e.error.localizedMessage}'),
        behavior: SnackBarBehavior.floating, // this will add margin to the snackbar
      ),
    )
Jayant Dhingra
  • 526
  • 6
  • 11