如何在flutter中显示没有上下文的对话框?

问题描述 投票:0回答:1

我试图使用firebase云评估,在这个过程中,我想在推送通知到达时向用户显示一个弹出的对话。但是为了显示对话,我们需要将上下文对象作为参数之一。showDialogBuildContext.

我试过很多方法,但都没有用。目前,我的代码是这样的。

_firebaseMessaging.configure(
        onMessage: (Map<String, dynamic> message) {
          print('onMessage: $message');
          return;
        },
        onBackgroundMessage: myBackgroundMessageHandler,
        onResume: (Map<String, dynamic> message) {
          print('onResume: $message');
          return;
        },
        onLaunch: (Map<String, dynamic> message) {
          print('onLaunch: $message');
            Text('onLaunch: $message'),
          );
          return;
        });

注: 这段代码是在一个单独的类中写的,我试图在没有任何第三部分库的情况下实现它。

flutter firebase-cloud-messaging flutter-dialog
1个回答
0
投票

首先,如果没有一个有效的上下文,你是无法显示对话框的。你为什么不简单地通过一个 BuildContext 到你的类中,就像这样?

class SeparateClass {
  final BuildContext context;

  SeparateClass(this.context);

  void configure() {
    // your rest of the configuration code
    // you can use showDialog(context, ...) here
  }
}

0
投票

我解决了一个类似的问题,显示一个SnackBar而不是Dialog。有了这段代码,你可以在你的应用程序中任何地方启动一个SnackBar或Dialog。

import 'package:collectio/pages/init_screen_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'helper.dart';

void main() {
  Provider.debugCheckInvalidValueType = null;
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
      // here the context refers to MaterialApp widget,
      // you can´t call Scaffold.of(context)
        body: Builder(builder: (context) {
          // here the context refers to Scaffold widget
          Helper.startFirebaseMessaging(context);
          return InitScreenPage();
        }),
    ));
  }
}

class Helper {
  static startFirebaseMessaging(BuildContext buildContext) {
    final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
        var notification = message['notification'];

        // build the snackbar
        final snackBar = SnackBar(
          content: Text(notification['title']),
          action: SnackBarAction(
              label: 'Ok',
              onPressed: (){}
          ),
        );

        try {
          Scaffold.of(buildContext).showSnackBar(snackBar);
        } catch (e) {
          print(e);
        }
      },
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: true));
    _firebaseMessaging.onIosSettingsRegistered
        .listen((IosNotificationSettings settings) {
      print("Settings registered: $settings");
    });
    _firebaseMessaging.getToken().then((String token) {
      assert(token != null);
      print("Push Messaging token: $token");;
    });
    print("Waiting for token...");
  }
}

也许你可以把这个应用到你的代码中。

© www.soinside.com 2019 - 2024. All rights reserved.