From f9b0f6d226f33f558c2d75582ea722bb2fca102f Mon Sep 17 00:00:00 2001 From: broodjeaap89 Date: Tue, 17 Aug 2021 21:09:21 +0200 Subject: [PATCH] got full screen notifications working, with routing to a notification page. --- lib/main.dart | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 8896514..bc2816b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -56,6 +56,7 @@ void main() async { initializationSettings, onSelectNotification: (String? payload) async { selectedNotificationPayload = payload; + print("onSelectNotification"); selectNotificationSubject.add(payload); }); @@ -68,26 +69,29 @@ void main() async { String initialRoute = "/"; if (notificationAppLaunchDetails?.didNotificationLaunchApp ?? false) { selectedNotificationPayload = notificationAppLaunchDetails!.payload; + print("Started by notification"); initialRoute = "/event/notification"; } - runApp( - MaterialApp( - title: "PizzaPlanner", - initialRoute: initialRoute, - onGenerateRoute: RouteGenerator.generateRoute, - ) - ); + runApp(PizzaPlanner(initialRoute)); //await Hive.close(); } class PizzaPlanner extends StatefulWidget { + final String initialRoute; + + PizzaPlanner(this.initialRoute); + @override PizzaPlannerState createState() => PizzaPlannerState(); } class PizzaPlannerState extends State { + // need this because in _configureSelectNotificationSubject() we need to pushNamed a route + // but only descendants of a MaterialApp have access to the Navigator, + // and this build() returns the MaterialApp + final GlobalKey navigatorKey = new GlobalKey(); @override void initState(){ @@ -97,12 +101,18 @@ class PizzaPlannerState extends State { @override Widget build(BuildContext context) { - return PizzaEventsPage(); + return MaterialApp( + title: "PizzaPlanner", + initialRoute: this.widget.initialRoute, + navigatorKey: this.navigatorKey, + onGenerateRoute: RouteGenerator.generateRoute, + ); } void _configureSelectNotificationSubject() { selectNotificationSubject.stream.listen((String? payload) async { - await Navigator.pushNamed(context, '/event/notification', arguments: payload); + print("tapped on notification"); + await this.navigatorKey.currentState?.pushNamed('/event/notification', arguments: payload); }); } }