0

I want to display notifications that are being processed by a provider on an open gRPC stream. This is handled by notificationProvider.dart:

if (notification.type == 0) {
  showOverlayNotification(notification);
} else {
  notificationList.add(notification);
  notifyListeners();
}

showOverlayNotification() then displays custom notification Widget OverlayNotification() on top of the current screen that user is in, like:

showOverlayNotification() async {
    OverlayState? overlayState = Overlay.of(**context**);

    OverlayEntry overlayEntry = OverlayEntry(builder: (context) {
      return Positioned.fill(child: OverlayNotification());
    });

    overlayState?.insert(overlayEntry);
}

Now the problem is that I don't know how to get to the current context the user is in? Android was pretty straightforward with this, but I can't seem to find this in Flutter..

How do I display overlay widget to a current context? Do I have to keep track of it in a global variable in the end? Can I find it through NamedRoutes?

1 Answers1

1

can do something like this, fyi if you don't want to use navigator to pop current view you can create a navigator key as shown here

import 'package:flutter/material.dart';



void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

_buildPopupMessage(BuildContext context){
  
  return Center(
        child: IconButton(
        onPressed: () {
          showDialog(
            context: context,
            builder: (context) {
              return FittedBox(
                fit: BoxFit.scaleDown,
                child: AlertDialog(
                  title: Text(
                    'title text',
                    textAlign: TextAlign.center,
                  ),
                  content: Text('message text here',
                  textAlign: TextAlign.center),
                  actions: <Widget>[
                    TextButton(
                      onPressed: () {
                        debugPrint('closed pressed');
                        Navigator.pop(context);
                      },
                      child: Text('close'),
                    )
                  ],
                ),
              );
            },
          );
        },
        
        icon: Icon(
          Icons.live_help,
        ),
        ),
      );
  
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body:_buildPopupMessage(context),
    );
  }
}
mister_cool_beans
  • 1,441
  • 1
  • 8
  • 19
  • Tried the global key but for some reason it doesn't pass the right context. I'm using a separate class which doesn't have a build method as it is a provider that deals with incoming notification data and should trigger a showOverlayNotification() which has a Scaffold but I don't know how to pass it the context. – Sara Petrovic May 17 '22 at 11:24