Flutter:有没有办法防止屏幕关闭?

时间:2018-12-30 16:28:56

标签: android android-studio dart flutter

我想构建一个“ Nightstand Clock”应用程序,并且我希望手机在不关闭屏幕的情况下只要应用程序处于活动状态就显示时钟。

在Flutter中有没有办法做到这一点?

我找到了this的答案,但是使用“屏幕”插件对我不起作用。 将依赖项添加到“ pubspecc.yaml”并运行flutter packages get后,我的应用程序不再运行,并且Android SDK陷入了“解决依赖项”阶段。

无论哪种方式,除了'screen'插件外,还有什么其他的方法可以实现。

1 个答案:

答案 0 :(得分:0)

从同一个SO post,它提到screen plugin遇到了一些问题。我检查了插件,它似乎自 2019 年 3 月 13 日以来没有更新。另一个可以提供您需要的相同功能的插件是 wakelock plugin。它仍然可用,目前由作者维护。事实上,最新版本 0.5.0+2 已于 2021 年 3 月 7 日发布。

<块引用>

嗨,我还在维护它 :) 我们最近添加了 macOS 支持作为 ? 另外,Flutter 是开源的——他们有第一方插件, 然而,他们确实说他们将放弃他们的 如果存在更好的第三方项目,则使用第一方解决方案。所以为什么 他们会想为完美无缺的东西创造一个吗? – creativecreatorormaybenot 2 月 8 日 16:17

我已经测试了 wakelock package 中给出的示例:

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

void main() {
  runApp(WakelockExampleApp());
}

class WakelockExampleApp extends StatefulWidget {
  @override
  _WakelockExampleAppState createState() => _WakelockExampleAppState();
}

class _WakelockExampleAppState extends State<WakelockExampleApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Wakelock example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              const Spacer(
                flex: 3,
              ),
              OutlinedButton(
                onPressed: () {
                  setState(() {
                    Wakelock.enable();
                  });
                },
                child: const Text('enable wakelock'),
              ),
              const Spacer(),
              OutlinedButton(
                onPressed: () {
                  setState(() {
                    Wakelock.disable();
                  });
                },
                child: const Text('disable wakelock'),
              ),
              const Spacer(
                flex: 2,
              ),
              FutureBuilder(
                future: Wakelock.enabled,
                builder: (context, AsyncSnapshot<bool> snapshot) {
                  final data = snapshot.data;
                  if (data == null) {
                    return Container();
                  }

                  return Text('The wakelock is currently '
                      '${data ? 'enabled' : 'disabled'}.');
                },
              ),
              const Spacer(
                flex: 3,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

这是Android中的输出:

enter image description here

iOS 输出:

enter image description here

此外,您似乎已经解决了代码中的上一个问题。很高兴分享 a minimal, complete and verifiable example 让社区很好地理解这个问题。

相关问题