0

I am new to Flutter, i want to built an app that can control the Screen Brightness using flutter, i found a Plugin in screen 0.0.5 it is working , it can effect the screen brightness. I am using Screen Plugin you can check it. But My Problem is that when i hide the Application then the Brightness Set by my App is overwritten by Mobile's default Brightness? How it is possible to full control the phone brightness?

code

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

void main() => runApp(MaterialApp(
    theme: ThemeData(primarySwatch: Colors.red, brightness: Brightness.dark),
    themeMode: ThemeMode.dark,
    darkTheme: ThemeData(brightness: Brightness.dark),
    debugShowCheckedModeBanner: false,
    home: ScreenBrightness()));
//ManageScreenDemoState pageState;

class ScreenBrightness extends StatefulWidget {
  @override
  _ScreenBrightnessState createState() => _ScreenBrightnessState();
}

class _ScreenBrightnessState extends State<ScreenBrightness> {
  @override
  double _brightness;
  bool _enableKeptOn;

  @override
  void initState() {
    super.initState();
    getBrightness();
    getIsKeptOnScreen();
  }

  void getBrightness() async {
    double value = await Screen.brightness;
    setState(() {
      _brightness = double.parse(value.toStringAsFixed(1));
    });
  }

  void getIsKeptOnScreen() async {
    bool value = await Screen.isKeptOn;
    setState(() {
      _enableKeptOn = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Screen Brightness")),
      body: Column(
        children: <Widget>[
          // Notice
          Container(
            margin: const EdgeInsets.all(10),
            padding: const EdgeInsets.all(10),
            alignment: Alignment(0, 0),
            height: 50,
            decoration: BoxDecoration(color: Colors.orange),
            child: Text(
              "Do this example on a Real Phone, not an Emulator.",
              style: TextStyle(color: Colors.white),
            ),
          ),
          // Brightness Settings
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text("Brightness:"),
                (_brightness == null)
                    ? CircularProgressIndicator(
                        backgroundColor: Colors.grey,
                      )
                    : Slider(
                        activeColor: Colors.grey,
                        value: _brightness,
                        min: 0,
                        max: 1.0,
                        divisions: 20,
                        onChanged: (newValue) {
                          setState(() {
                            _brightness = newValue;
                          });
                          // set screen's brightness
                          Screen.setBrightness(_brightness);
                        },
                      ),
                Text((_brightness * 100).toStringAsFixed(1) + "%"),
              ],
            ),
          ),
          // Kept-On Settings
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text("Kept on Screen:"),
                Text(_enableKeptOn.toString()),
                (_enableKeptOn == null)
                    ? CircularProgressIndicator(
                        backgroundColor: Colors.blue,
                      )
                    : Switch(
                        activeColor: Colors.grey,
                        value: _enableKeptOn,
                        onChanged: (flag) {
                          Screen.keepOn(flag);
                          getIsKeptOnScreen();
                        },
                      )
              ],
            ),
          )
        ],
      ),
    );
  }
}

Image enter image description here

Qasim Ali
  • 434
  • 2
  • 5
  • 31
  • 2
    Does this answer your question? [Can flutter change the brightness of the screen?](https://stackoverflow.com/questions/63233620/can-flutter-change-the-brightness-of-the-screen) – Mobina Aug 27 '20 at 10:38
  • 1
    No, this is not helpful @Mobina – Qasim Ali Aug 28 '20 at 17:41
  • 1
    Did you add the `` to the manifest? – Mobina Aug 28 '20 at 18:58
  • 1
    I used it Now it is working but another problem still exit , Now Problem is that when i close the , the brightness of phone Controlled by my code is overwritten by phones default brightness how it can be disabled with code inside this app? @Mobina – Qasim Ali Aug 28 '20 at 19:36
  • 1
    I updated my question Now @Mobina – Qasim Ali Aug 28 '20 at 19:37

1 Answers1

1

Try it:

MaterialApp(
  theme: ThemeData.dark(),
)

Also check :

how to implement dark mode in flutter

4b0
  • 21,981
  • 30
  • 95
  • 142
ElsayedDev
  • 601
  • 1
  • 8
  • 15