【发布时间】:2019-11-22 18:00:56
【问题描述】:
我的订单有 4 个状态:准备中、待处理、交付和已交付。当订单状态从一个更改为另一个时,我想向用户显示更改发生的通知。
*我使用了本地通知插件。在此处显示的订单页面小部件中,它由上面的流触发,该流从 fire-base 获取订单。 *这就是为什么我认为每次状态发生变化时 orderPage 将再次重建,initstate 将被调用并发送带有新状态的新通知消息,但这并没有发生。
- 另一个解决方案是使用 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;
}
}
【问题讨论】: