Flutter 如何在颤振中收听UIIOS应用程序生命周期

Flutter 如何在颤振中收听UIIOS应用程序生命周期,flutter,Flutter,我想检测我的iOS应用程序写与颤振移动到背景和前景。我习惯于用WidgetsBindingObserver来收听颤振。但它不起作用,这完全不是我的期望。当我在颤振应用程序生命周期中按下本机控制器时,触发AppLifecycleState。已暂停,但我的应用程序正在运行。有人刚刚遇到过这个问题吗?以及你们解决问题的方式。谢谢 @覆盖 void didchangeAppifecyclestate(AppLifecycleState状态){ super.didChangeAppLifecycleSta

我想检测我的iOS应用程序写与颤振移动到背景和前景。我习惯于用WidgetsBindingObserver来收听颤振。但它不起作用,这完全不是我的期望。当我在颤振应用程序生命周期中按下本机控制器时,触发AppLifecycleState。已暂停,但我的应用程序正在运行。有人刚刚遇到过这个问题吗?以及你们解决问题的方式。谢谢

@覆盖
void didchangeAppifecyclestate(AppLifecycleState状态){
super.didChangeAppLifecycleState(州);
if(state==AppLifecycleState.paused){
_viewModel.applicationMoveToBackground();
}else if(state==AppLifecycleState.resume){
_viewModel.applicationMoveToForeground();
}
}

您正在使用WidgetsBindingObserver吗

class _PageState extends State<Page> with WidgetsBindingObserver {
    @override
    void initState() {
        super.initState();
        WidgetsBinding.instance.addObserver(this);
    }

    @override
    dispose() {
        super.dispose();
        WidgetsBinding.instance.removeObserver(this);
    }

    @override
    void didChangeAppLifecycleState(AppLifecycleState state) {
        super.didChangeAppLifecycleState(state);
        if(state == AppLifecycleState.paused) {
          _viewModel.applicationMoveToBackground();

        } else if(state == AppLifecycleState.resumed) {
          _viewModel.applicationMoveToForeground();
        }
    }
}
class\u PageState使用WidgetsBindingObserver扩展状态{
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(这个);
}
@凌驾
处置{
super.dispose();
WidgetsBinding.instance.removeObserver(此);
}
@凌驾
void didchangeAppifecyclestate(AppLifecycleState状态){
super.didChangeAppLifecycleState(州);
if(state==AppLifecycleState.paused){
_viewModel.applicationMoveToBackground();
}else if(state==AppLifecycleState.resume){
_viewModel.applicationMoveToForeground();
}
}
}

您正在使用WidgetsBindingObserver吗

class _PageState extends State<Page> with WidgetsBindingObserver {
    @override
    void initState() {
        super.initState();
        WidgetsBinding.instance.addObserver(this);
    }

    @override
    dispose() {
        super.dispose();
        WidgetsBinding.instance.removeObserver(this);
    }

    @override
    void didChangeAppLifecycleState(AppLifecycleState state) {
        super.didChangeAppLifecycleState(state);
        if(state == AppLifecycleState.paused) {
          _viewModel.applicationMoveToBackground();

        } else if(state == AppLifecycleState.resumed) {
          _viewModel.applicationMoveToForeground();
        }
    }
}
class\u PageState使用WidgetsBindingObserver扩展状态{
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(这个);
}
@凌驾
处置{
super.dispose();
WidgetsBinding.instance.removeObserver(此);
}
@凌驾
void didchangeAppifecyclestate(AppLifecycleState状态){
super.didChangeAppLifecycleState(州);
if(state==AppLifecycleState.paused){
_viewModel.applicationMoveToBackground();
}else if(state==AppLifecycleState.resume){
_viewModel.applicationMoveToForeground();
}
}
}

我通过使用FlatterBasicMessageChannel将iOSUIApplication生命周期发送到Flatter解决了这个问题。 在iOS Runner应用程序中:

导入UIKit
进口颤振
@UIApplicationMain@objc类AppDelegate:AppDelegate{
var applicationLifeCycleChannel:flatterBasicMessageChannel!
重写func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptions:[UIApplicationLaunchOptions:任意]?)->Bool{
GeneratedPluginRegistrant.register(带:self)
applicationLifeCycleChannel=基本消息通道(
名称:“应用程序生命周期”,
binaryMessenger:(window.rootViewController为!flatterViewController)。binaryMessenger,
编解码器:flatterStringCodec.sharedInstance())
return super.application(application,didfishlaunchingwithoptions:launchOptions)
}
重写func应用程序将终止(application:UIApplication){
applicationLifeCycleChannel.sendMessage(“applicationWillTerminate”)
}
重写func applicationWillEnterForeground(application:UIApplication){
applicationLifeCycleChannel.sendMessage(“applicationWillEnterForeground”)
}
重写函数applicationIdentinterbackground(u应用程序:UIApplication){
applicationLifeCycleChannel.sendMessage(“ApplicationIdentinterBackground”)
}
}
在颤振应用程序中:

静态常量applicationLifecycleChannel=BasicMessageChannel('applicationLifeCycle',StringCodec()); 静态常量kApplicationWillTerminate='applicationWillTerminate'; 静态常量kApplicationWillEnterForeground='applicationWillEnterForeground'; 静态常量kApplicationDidEnterBackground='ApplicationIdentinterBackground'; @凌驾 void initState(){ applicationLifecycleChannel.setMessageHandler((消息)异步{ 开关(信息){ 案例K申请将终止: 打破 案例K应用程序将进入前台: _viewModel.applicationMoveToForeground(); 打破 案例应用背景: _viewModel.applicationMoveToBackground(); 打破 违约: 打破 } 返回消息; }); super.initState(); }
我通过使用FlatterBasicMessageChannel将iOSUIApplication生命周期发送到Flatter解决了这个问题。 在iOS Runner应用程序中:

导入UIKit
进口颤振
@UIApplicationMain@objc类AppDelegate:AppDelegate{
var applicationLifeCycleChannel:flatterBasicMessageChannel!
重写func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptions:[UIApplicationLaunchOptions:任意]?)->Bool{
GeneratedPluginRegistrant.register(带:self)
applicationLifeCycleChannel=基本消息通道(
名称:“应用程序生命周期”,
binaryMessenger:(window.rootViewController为!flatterViewController)。binaryMessenger,
编解码器:flatterStringCodec.sharedInstance())
return super.application(application,didfishlaunchingwithoptions:launchOptions)
}
重写func应用程序将终止(application:UIApplication){
applicationLifeCycleChannel.sendMessage(“applicationWillTerminate”)
}
重写func applicationWillEnterForeground(application:UIApplication){
applicationLifeCycleChannel.sendMessage(“applicationWillEnterForeground”)
}
重写函数applicationIdentinterbackground(u应用程序:UIApplication){
applicationLifeCycleChannel.sendMessage(“ApplicationIdentinterBackground”)
}
}
在颤振应用程序中:

static const applicationLifecycleChannel=BasicMessage