24

In my code I call a bottom sheet to display a list of tiles. These tiles contain buttons that display a snackbar. That functionality works fine, the only issue is that the snackbar is displayed behind the bottom sheet so you can only see it if you close the bottom sheet. Each of them are called with the following code:

1. Bottom Sheet:

  void _settingModalBottomSheet(context, stream, scaffoldKey ) {
    if (_availableRides.length == 0) {
      return null;
    } else {
      return scaffoldKey.currentState.showBottomSheet((context) {
        return Column(
          children: Widgets;
      });
    }
  }

2. Snackbar

widget.scaffoldKey.currentState.showSnackBar(SnackBar(
         content: Text("Created", textAlign: 
    TextAlign.center,),),

Does anyone know how I can position the snackbar in front of the bottom sheet

Pablo Barrera
  • 10,387
  • 3
  • 28
  • 49
Samuel Drescher
  • 457
  • 1
  • 3
  • 13

9 Answers9

17

So I was able to solve this by just adding another Scaffold() to my Bottom sheet and passing it a new scaffold key

Samuel Drescher
  • 457
  • 1
  • 3
  • 13
7

SnackBar has a property for this. It's called behavior, you could do this:

SnackBar(
    behavior: SnackBarBehavior.floating,
    ...

SnackBarBehavior enum

floating → const SnackBarBehavior

This behavior will cause SnackBar to be shown above other widgets in the Scaffold. This includes being displayed above a BottomNavigationBar and a FloatingActionButton.

See material.io/design/components/snackbars.html for more details.

Kamlesh
  • 5,233
  • 39
  • 50
Pablo Barrera
  • 10,387
  • 3
  • 28
  • 49
  • 1
    Unfortunately, it shows higher than a bottom sheet and even a FAB, which looks weird. I wish there was an option to show it at the bottom, as in SnackBarBehavior.fixed, but over the bottom sheet. – Andrei Volgin Jan 20 '20 at 04:15
  • 20
    Sorry to say, but suggested solution did not work for me. Thanks. – Kamlesh Jun 07 '21 at 18:09
  • @Kamlesh I a now I am late to the party, but take a look to my answer It worked for me. – I.Step Apr 06 '23 at 09:01
2

I solved by Set (padding from bottom to SnackBar) As much as the height of the (bottomSheet : height) .

In This Case I Have This bottomSheet:

bottomSheet: Container(
          child: RaisedButton(...),
          width: MediaQuery.of(context).size.width,
          height: AppBar().preferredSize.height * 0.85,
        ),

And This snackBar:

_scaffoldKey.currentState.showSnackBar(SnackBar(
padding:EdgeInsetsDirectional.only(
bottom:AppBar().preferredSize.height * 0.85),
backgroundColor: Colors.red,
duration: new Duration(milliseconds: 3000),
content: Text('ETC..'),
                ));
Esmaeil Ahmadipour
  • 840
  • 1
  • 11
  • 32
2

You can achieve this Simply by wrapping your BottomSheet widget with a Scaffold.

eg:

 void _settingModalBottomSheet(context, stream, scaffoldKey ) {
    if (_availableRides.length == 0) {
      return null;
    } else {
      return scaffoldKey.currentState.showBottomSheet((context) {
        return Scaffold(
           body: Column(
             children: Widgets;
           })
         );
    }
  }
2

I arrived on pretty decent solution. I wrapped my bottom sheet in a Scaffold, but I added it as the bottomSheet parameter. While adding the Scaffold, some trailing background will be added, so I just made its background transparent.

Scaffold(
  backgroundColor: Colors.transparent,
  bottomSheet: ...,
)
  • the `bottomSheet` has to be body for the `backgroundColor` to take effect. – SwiftiSwift Feb 06 '23 at 07:49
  • 1
    For me the, the transparency did not work. However, we can wrap the Scaffold with a SizedBox that only has a specific height (considering we always want that for the bottomSheetLayout), example: SizedBox( height: MediaQuery.of(context).size.height * .8, child: Scaffold( bottomSheet: ), ) – bd1909 Apr 22 '23 at 21:23
2

This is a working solution according to documentation. https://docs.flutter.dev/cookbook/design/snackbars

This example works with bottom sheets as well.

  1. Initialize ScaffoldMessengerKey.
  2. Wrap your component widget with Scaffold.
  3. Wrap Scaffold with ScaffoldMessenger.
  4. Add key scaffoldMessengerKey to ScaffoldMessenger
  5. Call method scaffoldMessengerKey.currentState?.showSnackBar(SnackBar());

Example:

final scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();


// Any widget with button. 
// (Bottom sheet also) - root widget must be ScaffoldMessenger.
ScaffoldMessenger(
  key: scaffoldMessengerKey,
  child: Scaffold(
    body: Container(
      child: ElevatedButton(
        style: raisedButtonStyle,
        onPressed: () { 
          scaffoldMessengerKey.currentState?.showSnackBar(
            //SnackBar design.
            SnackBar(
             backgroundColor: Colors.white,
             elevation: 8,
             content: Container(
               decoration: BoxDecoration(
                 color: Colors.white,
                 borderRadius: BorderRadius.circular(5),
               ),
               child: Text(
                 'Simple snackbar text.',
                 style: FlutterFlowTheme.of(context).bodyText1
                 .override(fontFamily: 'Rubik',
                          fontWeight: FontWeight.w300,
                          lineHeight: 1.5,
               ),
             ),
             action: SnackBarAction(
                label: 'Undo',
                onPressed: () {
                  // Some code to undo the change.
                },
             ),
             duration: Duration(seconds: 5),
             behavior: SnackBarBehavior.floating,
               
       },
       child: Text('Open snackbar over bottom sheet!'),
      ); //ElevatedButton
    ); //Container
  ); //Scaffold
); //ScaffoldMessenger

Note: With this approach you don't need to pass BuildContext.

If you don't want to register ScaffoldMessengerKey. You can show SnackBar like this: ScaffoldMessenger.of(context).showSnackBar(SnackBar());

I.Step
  • 613
  • 6
  • 22
1

I solved this problem with idle solution but it works

The Solution by 3 steps:

1 Add SizedBox and set the same height of BottomSheet
2 Add Scaffold as child of SizedBox
3 Add Your BottomSheet

SizedBox(
  height: ... ,// height of bottomSheet
  child: Scaffold(
       body: ... , // your bottomSheet
  )
)

0

I solved this by changing bottomSheet to bottomNavigationBar since the floating snack bar solution didn't work for me.

Lee Mordell
  • 473
  • 5
  • 16
  • Where did you make this change? kindly share. Thanks. – Kamlesh Jun 07 '21 at 18:10
  • `Scaffold` has both bottomSheet and bottomNavigationBar properties. When the scaffold had a bottomSheet widget, the snackbar would not display in front of the widget. When I changed it to bottomNavigation bar, the snackbar displayed in front of the widget. – Lee Mordell Jun 08 '21 at 19:35
  • 1
    I already have a bottomNavigationBar, bottomModelSheet and snackbar. bottomModelSheet is displaying as overlay of bottomNavigationBar which is working. But when I want to show snackbar over the bottomModelSheet it does not displayed. Snackbar is displayed between bottomNavigationBar and bottomModelSheet. If you have any experience of it. Kindly share your thoughts. Thanks a lot. – Kamlesh Jun 09 '21 at 07:48
  • i am facing same issue as Kamlesh – Shree Softech Nov 09 '22 at 06:32
0

you can use flushbar package. I think this is the better option if need to use with bottomSheet.

context should be your page's context, not bottomsheet context

any event inside bottomSheet

 CustomFlushBar().flushBar(text: 'Thank you for your payment!', context: context,duration: 2);

CustomFlushBar class

  class CustomFlushBar {
      void flushBar({int duration, @required String text, Color iconColor, IconData iconData, Color backgroundColor, @required BuildContext context}) async {
        await dismiss();
        Flushbar(
          margin: EdgeInsets.all(8),
          borderRadius: 8,
          backgroundColor: backgroundColor ?? Palette.greenButton,
          icon: Icon(iconData ?? Icons.done, color: iconColor ?? Palette.white),
          flushbarStyle: FlushbarStyle.FLOATING,
          message: text,
          duration: Duration(seconds: duration ?? 3),
        )..show(context);
      }
    
      Future<void> dismiss() async {
        if (!Flushbar().isDismissed()) {
          await Flushbar().dismiss();
        }
      }
    }
BIS Tech
  • 17,000
  • 12
  • 99
  • 148