我正在做一个聊天应用程序,我想发送推送通知给一个用户,只有当他不在聊天屏幕,所以我需要知道当前屏幕或路径
我试着在他每次进入屏幕时都保存屏幕的名称,但当然,当用户使用Navigator.POP或他使用手势返回时,它不起作用
我不知道做这件事的正确方法是什么
我正在做一个聊天应用程序,我想发送推送通知给一个用户,只有当他不在聊天屏幕,所以我需要知道当前屏幕或路径
我试着在他每次进入屏幕时都保存屏幕的名称,但当然,当用户使用Navigator.POP或他使用手势返回时,它不起作用
我不知道做这件事的正确方法是什么
也许你可以使用dispose
方法在用户离开屏幕时更新状态:
import 'package:flutter/material.dart';
class ChatScreen extends StatefulWidget {
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
bool userInChat = false;
@override
void initState() {
super.initState();
// Set the user as "in the chat" when the widget is initialized
}
@override
void dispose() {
// Set the user as "not in the chat" when the widget is disposed (he left the screen)
super.dispose();
}
}
initState
方法在创建窗口小部件时调用,dispose
方法在窗口小部件从窗口小部件树中移除时调用,dispose
通常在用户导航离开屏幕时调用.