Оберните ваше MaterialApp в класс-оболочку ... позволяет вызвать этот FCMWrapper.
class FCMWrapper extends StatefulWidget {
final Widget child;
const FCMWrapper({Key key, this.child}) : super(key: key);
@override
_FCMWrapperState createState() => _FCMWrapperState();
}
class _FCMWrapperState extends State<FCMWrapper> {
@override
Widget build(BuildContext context) {
return Consumer<YourObservable>(
builder: (context, yourObservable, _) {
if (yourObservable != null && yourObservable.isNotEmpty) {
Future(
() => navigatorKey.currentState.push(
PushNotificationRoute(
child: YourViewOnNotification(),
)),
),
);
}
return widget.child;
},
);
}
}
Я сохранил свои данные в наблюдаемом из отдельного класса. Поэтому, когда я получаю уведомление, я обновляю свой наблюдаемый объект. Поскольку мы используем этот наблюдаемый объект, будет вызван PushNotificationRoute.
PushNotificationRoute - это просто класс, расширяющий ModalRoute.
class PushNotificationRoute extends ModalRoute {
final Widget child;
PushNotificationRoute({this.child});
... //Override other methods to your requirement
//This is important
@override
Widget buildPage(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation) {
return SafeArea(
child: Builder(builder: (BuildContext context) {
return child;
}),
);
}
...
@override
Duration get transitionDuration => Duration(milliseconds: 200);
}
Теперь в main.dart объявите глобальный ключ, например
var navigatorKey = GlobalKey<NavigatorState>();
и оберните ваше MaterialApp как
...
FCMWrapper(
child: MaterialApp(
navigatorKey: navigatorKey,
title: 'Your App',
...
Итак, теперь каждый раз, когда приходит уведомление, ваш наблюдаемый объект должен обновляться, а pu sh - модальный маршрут, который будет отображаться в любом месте приложения.