简体   繁体   中英

Flutter: Is there a way to prevent the screen from turning off?

I want to build a 'Nightstand Clock' app, and I want the phone to display the clock as long as the app is active without turning off the screen.

Is there a way to do this in Flutter?

I found this SO answer, but using the 'screen' plugin didn't work for me. After adding the dependency to 'pubspecc.yaml' and running flutter packages get my app doesn't run anymore and Android SDK is getting stuck at the 'resolving dependencies' stage.

Either way, is there any other way to do this in flutter except the 'screen' plugin?

From the same SO post ,it mentioned that the screen plugin encountered some issues. I've checked the plugin and it seems that it was not updated since March 13, 2019. Another plugin that would give the same function you needed is wakelock plugin . It is still available currently maintained by the author. In fact, latest version 0.5.0+2 was release last March 7, 2021.

Hi, I am still maintaining it :) We recently added macOS support as well 🚀 Also, Flutter is open source - they have first-party plugins, however, they do say themselves that they will abandon their first-party solutions if a better third party project exists. So why would they want to create one for something that works perfectly fine? – creativecreatorormaybenot Feb 8 at 16:17

I've tested the sample given in the 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,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Here is the output in Android:

在此处输入图片说明

Output in iOS:

在此处输入图片说明

Also, it seems that you've resolved the previous issue in your code. It would be nice to share a minimal, complete and verifiable example for the community to understand the issue very well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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