在Windows桌面上未调用抖动小部件BindingObserver的didChangeAppLifecycleState

camsedfj  于 2022-12-14  发布在  Windows
关注(0)|答案(1)|浏览(126)

print函数应该根据生命周期转换被调用,但是它们都没有被调用。为了测试这一点,我在调试模式下运行应用程序,并将其移到后台/前台(即切换到另一个应用程序,然后返回到该应用程序)。

我做错了什么?

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'));
    }

这是我的主要.镖:

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())
    );
  }
}
ccrfmcuu

ccrfmcuu1#

Flutter目前不支持桌面上的生命周期事件,但存在一个现有问题,正在此处进行跟踪#30735
目前,您可以使用window_manager包确定应用程序的状态

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();
  }

相关问题