简体   繁体   English

Flutter WidgetsBindingObserver 的 didChangeAppLifecycleState 未在 Windows 桌面上调用

[英]Flutter WidgetsBindingObserver's didChangeAppLifecycleState not being called on Windows Desktop

The print functions should be called according to the lifecycle transitions, but none of them are being called.打印函数应该根据生命周期转换被调用,但它们都没有被调用。 To test this, I'm running the app in debug mode and moving it to the background/foreground (ie changing to another app and then returning to this app).为了对此进行测试,我在调试模式下运行该应用程序并将其移至后台/前台(即更改为另一个应用程序然后返回到该应用程序)。

What am I doing wrong?我究竟做错了什么?

import 'package:flutter/material.dart';

class StopwatchVw extends StatefulWidget {
  const StopwatchVw({Key? key}) : super(key: key);
  @override _StopwatchVwState createState() => _StopwatchVwState();
}

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

  @override
  Future<void> didChangeAppLifecycleState(AppLifecycleState state) {
    print('Changed');

    switch (state) {
      case AppLifecycleState.inactive:
        print("Inactive");
        break;
      case AppLifecycleState.paused:
        print("Paused");
        break;
      case AppLifecycleState.resumed:
        print("Resumed");
        break;
      case AppLifecycleState.detached:
        print("Suspending");
        break;
    }
  }

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

  @override
    Widget build(BuildContext context) {
      return Scaffold(body: Text('HEY'));
    }

This is my main.dart:这是我的 main.dart:

import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:clocker/stopwatch_vw.dart';
import 'package:flutter/material.dart';
import 'package:flutter_acrylic/flutter_acrylic.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Window.initialize();
  await Window.hideWindowControls();

  runApp(const MyApp());

  doWhenWindowReady(() {
    const initialSize = Size(350, 200);
    appWindow
      ..size = initialSize
      ..minSize = initialSize
      ..maxSize = initialSize
      ..alignment = Alignment.bottomRight
      ..show();
  });

  Window.setEffect(
    effect: WindowEffect.acrylic,
    color: const Color.fromARGB(29, 250, 250, 227),
  );
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MoveWindow(child: const StopwatchVw())
    );
  }
}

Flutter currently doesn't support LifeCycle events on the desktop, But there is an existing issue which is being tracked here #30735 . Flutter 目前不支持桌面上的 LifeCycle 事件,但这里存在一个正在跟踪的问题#30735

Currently, you can determine the status of your application with the window_manager package目前,您可以使用window_manager package 确定您的应用程序的状态

class Windows extends StatefulWidget {
  const Windows({super.key});

  @override
  State<Windows> createState() => _WindowsState();
}

class _WindowsState extends State<Windows> with WindowListener {
  @override
  void onWindowClose() {
    // do something
  }

  @override
  void onWindowFocus() {
    // do something
  }
  @override
  void onWindowMinimize() {
    // do something
  }
  
  @override
  void initState() {
    windowManager.addListener(this);
    super.initState();
  }

  @override
  void dispose() {
    windowManager.removeListener(this);
    super.dispose();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM