top-snackbar-flutter icon indicating copy to clipboard operation
top-snackbar-flutter copied to clipboard

Support to scaffoldMessengerKey to show snackbar without context.

Open luizpaulofranz opened this issue 2 years ago • 1 comments

Is there any way of call top-snackbar-flutter using a GlobalKey for scaffoldMessengerKey?

We have a specific use case in which we don't have access to context, but we do have a instance of scaffoldMessengerKey, I have tried this without success:

    showTopSnackBar(
      scaffoldMessageKey.currentContext!,
      ToastBody(
         ...
      ),
      padding: EdgeInsets.zero,
    );

Is there any way of using top-snack-bar without BuildContext directly?

luizpaulofranz avatar Sep 20 '22 17:09 luizpaulofranz

same problem here

itaispector avatar Sep 29 '22 08:09 itaispector

You could always take an overlayState from GlobalKey<NavigatorState> navigatorKey

vizhan-lanars avatar Feb 14 '23 12:02 vizhan-lanars

Any news?

ROiPinheiro avatar Mar 27 '23 18:03 ROiPinheiro

Out of scope.

For TopSnackbar you should pass overlayState now.

Native snackbar has different API. You could implement global MessageService by your own:

class MessageService {

  MessageService({
    required this.scaffoldKey,
  });
  final GlobalKey<ScaffoldMessengerState> scaffoldKey;

  void showSnackBar(Widget content) {
    scaffoldKey.currentState?.showSnackBar(
      SnackBar(content: content),
    );
  }
}
final scaffoldKey = GlobalKey<ScaffoldMessengerState>();
final messageService = MessageService(scaffoldKey: scaffoldKey);
runApp(
    MaterialApp.router(
      scaffoldMessengerKey: scaffoldKey,
    ),
);

vizhan-lanars avatar Sep 14 '23 10:09 vizhan-lanars