如何根据在 flutter 应用程序中检测到锁定或关闭屏幕采取行动

How to take action based on detection of locked or off screen in flutter app

通过使用来自 pub.dev 的名为 "torch_compat 1.0.2" 的 flutter 包来转动 on/off android 应用程序中的手电筒,如何在检测到锁定或关闭屏幕状态时关闭手电筒 (触发 TorchCompat.turnOff())

当前代码:

import 'package:flutter/material.dart';
import 'package:torch_compat/torch_compat.dart';

class FlashlightOnOffButton extends StatefulWidget {
  @override
  _FlashlightOnOffButtonState createState() => _FlashlightOnOffButtonState();
}

class _FlashlightOnOffButtonState extends State<FlashlightOnOffButton> {
  bool isFlashlightOn = false;
  @override
  Widget build(BuildContext context) {
    final data = MediaQuery.of(context);
    var isPortrait = MediaQuery.of(context).orientation;
    return Container(
      child: FlatButton(
        onPressed: () {
          isFlashlightOn ? TorchCompat.turnOff() : TorchCompat.turnOn();
          setState(() {
            isFlashlightOn = !isFlashlightOn;
          });
        },
        child: Icon(
          Icons.lightbulb,
          color: isFlashlightOn ? Colors.deepOrangeAccent : Colors.white10,
        ),
      ),
    );
  }
}

也许您正在寻找这些package package2

有关 flutter App Lifecycle 的更多信息可能会帮助您实现预期的结果。