how to close showOverlayNotification in flutter firebase

Viewed 29

I get notifications from Firebase and show them using showOverlayNotification. I pass in showOverlayNotification two arguments one callback and Duration value. It closes showOverlayNotification widget after ending the duration time. And now I need to add gesture to close this showOverlayNotification widget. I've tried code below. It doesn't help. Waiting for your answers. Thanks! enter image description here

full code:

Future<void> _getNotifications(BuildContext context) async {
await FirebaseMessaging.instance
    .setForegroundNotificationPresentationOptions(
        alert: false, badge: true, sound: true);
Future<void> _getPermission() async {
  if (Platform.isIOS) {
    await FirebaseMessaging.instance.requestPermission(
      alert: true,
      announcement: false,
      badge: true,
      carPlay: false,
      criticalAlert: false,
      provisional: false,
      sound: true,
    );
  }
}

void handlePushNavigation(RemoteMessage message) {
  debugPrint("hhh: _handlePushNavigation");

  String messageTitle = message.notification?.title ?? '';
  String messageBody = message.notification?.body ?? '';

  context.read<BonusProvider>().changeStatus(message.data['status']);
  context.read<BonusProvider>().changeNotificationBody(messageBody);

  if (message.data['status'] == "in_process") {
  } else if (message.data['status'] == "success") {
    showOverlayNotification((context) {
      return Card(
        elevation: 3,
        shape: RoundedRectangleBorder(
          side: BorderSide(color: Colors.transparent),
          borderRadius: BorderRadius.circular(10.0),
        ),
        color: Color.fromRGBO(230, 252, 232, 1),
        margin: const EdgeInsets.only(left: 20, top: 50, right: 20),
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ListTile(
            leading: SizedBox(
              width: 40,
              height: 40,
              child: SvgPicture.asset("assets/ic_ok.svg"),
            ),
            title: Text('Успех!',
                style: const TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                  color: EvrikaColors.darkColor,
                )),
            subtitle: Text('Бонусы успешно списаны',
                style: const TextStyle(
                    fontSize: 13,
                    fontWeight: FontWeight.w400,
                    color: EvrikaColors.darkColor)),
          ),
        ),
      );
    }, duration: Duration(seconds: 4));
  } else if (message.data['status'] == "failure") {
    showOverlayNotification((context) {
      return Card(
        elevation: 3,
        shape: RoundedRectangleBorder(
          side: BorderSide(color: Colors.transparent),
          borderRadius: BorderRadius.circular(10.0),
        ),
        color: Color.fromRGBO(255, 226, 226, 1),
        margin: const EdgeInsets.only(left: 20, top: 50, right: 20),
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ListTile(
            leading: SizedBox(
              width: 40,
              height: 40,
              child: SvgPicture.asset("assets/ic_error.svg"),
            ),
            title: Text('Ошибка!',
                style: const TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                  color: EvrikaColors.darkColor,
                )),
            subtitle: Text(
                'Бонусы не списались. Сгенерируйте новый QR-код и попробуйте снова',
                style: const TextStyle(
                    fontSize: 13,
                    fontWeight: FontWeight.w400,
                    color: EvrikaColors.darkColor)),
          ),
        ),
      );
    }, duration: Duration(seconds: 4));
  } else {
    showOverlayNotification((context) {
      return Card(
        elevation: 3,
        shape: RoundedRectangleBorder(
          side: BorderSide(color: Colors.transparent),
          borderRadius: BorderRadius.circular(10.0),
        ),
        color: EvrikaColors.kPrimaryColor,
        margin: const EdgeInsets.only(left: 20, top: 50, right: 20),
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: ListTile(
            leading: Container(
              width: 40.0,
              height: 40.0,
              decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
              ),
              child: Center(
                child: Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: SvgPicture.asset("assets/ic_evrika.svg"),
                ),
              ),
            ),
            title: Text(messageTitle,
                style: const TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                  color: Colors.white,
                )),
            subtitle: Text(messageBody,
                style: const TextStyle(
                    fontSize: 13,
                    fontWeight: FontWeight.w400,
                    color: Colors.white)),
          ),
        ),
      );
    }, duration: Duration(seconds: 4));
  }
}

_handleForeground() {
  debugPrint("hhh: _handleForeground");
  FirebaseMessaging.onMessage.listen((RemoteMessage message) {
    String messageTitle = message.notification?.title ?? '';
    String messageBody = message.notification?.body ?? '';

    context.read<BonusProvider>().changeStatus(message.data['status']);
    context.read<BonusProvider>().changeNotificationBody(messageBody);

    if (message.data['status'] == "in_process") {
    } else if (message.data['status'] == "success") {
      showOverlayNotification((context) {
        return Card(
          elevation: 3,
          shape: RoundedRectangleBorder(
            side: BorderSide(color: Colors.transparent),
            borderRadius: BorderRadius.circular(10.0),
          ),
          color: Color.fromRGBO(230, 252, 232, 1),
          margin: const EdgeInsets.only(left: 20, top: 50, right: 20),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: ListTile(
              leading: SizedBox(
                width: 40,
                height: 40,
                child: SvgPicture.asset("assets/ic_ok.svg"),
              ),
              title: Text('Успех!',
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    color: EvrikaColors.darkColor,
                  )),
              subtitle: Text('Бонусы успешно списаны',
                  style: const TextStyle(
                      fontSize: 13,
                      fontWeight: FontWeight.w400,
                      color: EvrikaColors.darkColor)),
            ),
          ),
        );
      }, duration: Duration(seconds: 4));
    } else if (message.data['status'] == "failure") {
      showOverlayNotification((context) {
        return Card(
          elevation: 3,
          shape: RoundedRectangleBorder(
            side: BorderSide(color: Colors.transparent),
            borderRadius: BorderRadius.circular(10.0),
          ),
          color: Color.fromRGBO(255, 226, 226, 1),
          margin: const EdgeInsets.only(left: 20, top: 50, right: 20),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: ListTile(
              leading: SizedBox(
                width: 40,
                height: 40,
                child: SvgPicture.asset("assets/ic_error.svg"),
              ),
              title: Text('Ошибка!',
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    color: EvrikaColors.darkColor,
                  )),
              subtitle: Text(
                  'Бонусы не списались. Сгенерируйте новый QR-код и попробуйте снова',
                  style: const TextStyle(
                      fontSize: 13,
                      fontWeight: FontWeight.w400,
                      color: EvrikaColors.darkColor)),
            ),
          ),
        );
      }, duration: Duration(seconds: 4));
    } else {
      showOverlayNotification((context) {
        return Card(
          elevation: 3,
          shape: RoundedRectangleBorder(
            side: BorderSide(color: Colors.transparent),
            borderRadius: BorderRadius.circular(10.0),
          ),
          color: EvrikaColors.kPrimaryColor,
          margin: const EdgeInsets.only(left: 20, top: 50, right: 20),
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: ListTile(
              leading: Container(
                width: 40.0,
                height: 40.0,
                decoration: BoxDecoration(
                  color: Colors.white,
                  shape: BoxShape.circle,
                ),
                child: Center(
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: SvgPicture.asset("assets/ic_evrika.svg"),
                  ),
                ),
              ),
              title: Text(messageTitle,
                  style: const TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    color: Colors.white,
                  )),
              subtitle: Text(messageBody,
                  style: const TextStyle(
                      fontSize: 13,
                      fontWeight: FontWeight.w400,
                      color: Colors.white)),
            ),
          ),
        );
      }, duration: Duration(seconds: 4));
    }
  });
}

_handleBackground() {
  debugPrint("hhh: _handleBackground");
  FirebaseMessaging.onBackgroundMessage(
    _firebaseMessagingBackgroundHandler,
  );
}

_handleOpened() {
  debugPrint("hhh: _handleOpened");
  FirebaseMessaging.onMessageOpenedApp.listen(
    (RemoteMessage message) {
      handlePushNavigation(message);
    },
  );
}

_handleLaunch() {
  debugPrint("hhh: _handleLaunch");
  FirebaseMessaging.instance.getInitialMessage().then((message) {
    if (message != null) {
      handlePushNavigation(message);
    }
  });
}

await _getPermission();
_handleLaunch();
_handleOpened();
_handleBackground();
_handleForeground();
}
0 Answers
Related