0

The idea is that when the app is in foreground, I want to show an alert dialog and when the user presses the okay button from the dialog, it navigates them to a different screen. However, the alterdialog does not appear. If I remove the alertdialog and just keep the navigation code, then the app navigates to a different screen as soon as the notification arrives. This is a bad user experience since the user does not have control of the UI. I want the user to confirm that that they want to navigate to different screen. Hence the use of alertdialog.

Here's my code:

void initState(){ 
    super.initState();
    var initializationSettingsAndroid  = AndroidInitializationSettings('@drawable/splash');
    var initializationSettings = InitializationSettings(android:initializationSettingsAndroid);
    flutterLocalNotificationsPlugin.initialize(initializationSettings);
    
    FirebaseMessaging.onMessage.listen((RemoteMessage message) {
      print('Inside onmessage'); 
      RemoteNotification notification = message.notification;
      AndroidNotification android = message.notification?.android;
      if (notification != null && android != null) {
        flutterLocalNotificationsPlugin.show(
            notification.hashCode,
            notification.title,
            notification.body,
            NotificationDetails(
              android: AndroidNotificationDetails(
                channel.id,
                channel.name,
                channel.description,
                icon: '@drawable/splash',
                playSound: true
              ),
            ));
           
            AlertDialog(
               title:Text(notification.title),
               actions:  <Widget>[
              TextButton(
                  child: Text('Cancel'),
                  onPressed: () {
                   setState(() {
                    //Navigator.of(context).pop();
                  navigatorKey.currentState.push(
    MaterialPageRoute(builder: (_) => OrdersScreen()));
                });
                  }),

            ],
               content:SingleChildScrollView(

                 child:
                 Column(
                   crossAxisAlignment: CrossAxisAlignment.start,
                   children:[
                     Text(notification.body)
                   ]
                 )
               )
             );
      }
    });
  }
mcfred
  • 1,183
  • 2
  • 29
  • 68

1 Answers1

1

Your code just expresses the AlertDialog but it does not do anything with it. That's why it does not show up.

Solution:

You need to call showDialog and pass it the AlertDialog object like below:

final AlertDialog alertDialog = AlertDialog(
        title: Text(notification.title),
        actions: <Widget>[
          TextButton(
              child: Text('Cancel'),
              onPressed: () {
                setState(() {
                  //Navigator.of(context).pop();
                  navigatorKey.currentState
                      .push(MaterialPageRoute(builder: (_) => OrdersScreen()));
                });
              }),
        ],
        content: SingleChildScrollView(
            child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [Text(notification.body)])));

showDialog(
      context: context// (or `navigatorKey.currentContext` in this case),
      builder: (BuildContext context) {
          return alertDialog;
      },
);    
Victor Eronmosele
  • 7,040
  • 2
  • 10
  • 33
  • I am getting this exception: Unhandled Exception: No MaterialLocalizations found. – mcfred Jun 19 '21 at 06:17
  • MyApp widgets require MaterialLocalizations to be provided by a Localizations widget ancestor. E/flutter (11352): The material library uses Localizations to generate messages, labels, and abbreviations. E/flutter (11352): To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application to include them automatically, or add a Localization widget with a MaterialLocalizations delegate. E/flutter (11352): The specific widget that could not find a MaterialLocalizations ancestor was: E/flutter (11352): MyApp – mcfred Jun 19 '21 at 06:17
  • The error is because `showDialog` is using a `context` that does not have a MaterialLocalizations widget. See https://stackoverflow.com/a/56275856/11039164. Please post the full main.dart file. – Victor Eronmosele Jun 19 '21 at 06:23
  • I got it. I used navigatorKey.currentContext inside showDialog and it worked. Thanks for giving me the direction. – mcfred Jun 19 '21 at 06:52
  • 1
    Great! I'll update my answer with `navigatorKey.currentContext`. – Victor Eronmosele Jun 19 '21 at 06:54