LanarsInc/top-snackbar-flutter

Support to scaffoldMessengerKey to show snackbar without context.

luizpaulofranz opened this issue · 4 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?

same problem here

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

Any news?

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,
    ),
);