Skip to content Skip to sidebar Skip to footer

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 Flutt

Solution 1:

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

voidmain() {
  runApp(WakelockExampleApp());
}

classWakelockExampleAppextendsStatefulWidget {
  @override
  _WakelockExampleAppState createState() => _WakelockExampleAppState();
}

class_WakelockExampleAppStateextendsState<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) {
                  finaldata= 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:

enter image description here

Output in iOS:

enter image description here

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.

Post a Comment for "Flutter: Is There A Way To Prevent The Screen From Turning Off?"