如何通过更改对象状态来触发动作?

时间:2019-07-13 03:37:57

标签: flutter listener

我的订单状态为4:正在准备,待处理,正在交付和已交付。当订单状态从一个更改为另一个时,我想向用户显示更改通知。

*我已经使用了本地通知插件。在此处显示的订单页面小部件中,它是由上方的流触发的,该流从fire-base获取订单。 *这就是为什么我认为每次状态更改时都会重新构建orderPage,并且将重新调用initstate并发送具有新状态的新通知msg,但这没有发生。

  • 另一种解决方案是使用didchangedependency,但没有得到不同的结果。 我知道这是一项错过的工作,但这就是我的想法。

*我真正想要的是使我能够监听状态的东西,当发生更改时,将调用函数“ singleNotification”来显示通知。 任何帮助将不胜感激。

class OrderPage extends StatefulWidget {
  const OrderPage({
    Key key,
    @required this.order,

  }) : super(key: key);
  final Order order;

  @override
  OrderPageState createState() {
    return new OrderPageState();
  }
}

class OrderPageState extends State<OrderPage> {
  final DateTime now = DateTime.now().toUtc().add(
        Duration(seconds: 3),
      );
  String title = "notification";
  String msg = "";

  FlutterLocalNotificationsPlugin localNotificationsPlugin =
      FlutterLocalNotificationsPlugin();
  initializeNotifications() async {
    var initializeAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');
    var initializeIOS = IOSInitializationSettings();
    var initSettings = InitializationSettings(initializeAndroid, initializeIOS);
    await localNotificationsPlugin.initialize(initSettings);
  }

  Future singleNotification(
      DateTime datetime, String message, String subtext, int hashcode,
      {String sound}) async {
    var androidChannel = AndroidNotificationDetails(
      'channel-id',
      'channel-name',
      'channel-description',
      importance: Importance.Max,
      priority: Priority.Max,
    );

    var iosChannel = IOSNotificationDetails();
    var platformChannel = NotificationDetails(androidChannel, iosChannel);
    localNotificationsPlugin.schedule(
        hashcode, message, subtext, datetime, platformChannel,
        payload: hashcode.toString());
  }

  @override
  void initState() {
    super.initState();
    getMsgState(widget.order.status);
    initializeNotifications();
    singleNotification(
      now,
      title,
      msg,
      98123871,
    );
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    getMsgState(widget.order.status);
    initializeNotifications();
    singleNotification(
      now,
      title,
      msg,
      98123871,
    );
  }
Widget build(BuildContext context) {
return Container();
}

String getMsgState(String orderStatus) {
    switch (orderStatus) {
      case 'pending':
        return msg = "Your order is pending";
        break;
      case 'preparing':
        return msg = "your order is currently preparing";
        break;
      case 'delivering':
        return msg = "your order is currently delivering";
        break;
      case 'delivered':
        return msg = "Your order is delivered";
      default:
        return msg = "CustomStepState.Complete";
        break;
    }
  }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,Firebase会知道订单状态何时更改。并发送通知。您想将其展示给用户。 您可以使用FCM和Firebase应用内通知。我的一个项目对服务器是否进行处理以及Flutter移动应用程序显示状态有类似的要求。我做了以下事情:

  1. 在服务器端编写一小段代码,以调用Firebase云消息API以及用户显示消息和数据有效负载。
  2. 在Flutter移动应用程序端编写代码以显示通知(采用应用程序内样式)(如果应用程序已在前台)。

示例代码段:

_firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async {
      Helper.write('on message $message');
      //Map data = json.decode(message);
      this.scaffoldKey.currentState.showSnackBar(SnackBar(content: Text(message['aps']['alert']['body']),));
    },
    onResume: (Map<String, dynamic> message) async {
      Helper.write('on resume $message');
    },
    onLaunch: (Map<String, dynamic> message) async {
      Helper.write('on launch $message');
    },
  );
  analytics.logEvent(name:'firebaseCloudMessaging_Listeners_Done',parameters:null);
}
相关问题