Can flutter change the brightness of the screen?

0

Issue

Can flutter change the brightness of the screen? I found that Brightness in ThemeData only has two values: light and dark. How can I adjust the screen brightness from 1 to 10 in the app?

Solution

Yes Flutter can, you can use the screen plugin:
Screen plugin

This is an example of how to implement it, You just have to set _brightness for the start value you want and change it using the Slider:

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

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool _isKeptOn = false;
  double _brightness = 1.0;

  @override
  initState() {
    super.initState();
    initPlatformState();
  }

  initPlatformState() async {
    bool keptOn = await Screen.isKeptOn;
    double brightness = await Screen.brightness;
    setState((){
      _isKeptOn = keptOn;
      _brightness = brightness;
    });
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(title: new Text('Screen plugin example')),
        body: new Center(
            child: new Column(
                children: <Widget>[
                  new Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      new Text("Screen is kept on ? "),
                      new Checkbox(value: _isKeptOn, onChanged: (bool b){
                        Screen.keepOn(b);
                        setState((){_isKeptOn = b; });
                      })
                    ]
                  ),
                  new Text("Brightness :"),
                  new Slider(value : _brightness, onChanged : (double b){
                    setState((){
                       _brightness = b;
                     });
                    Screen.setBrightness(b);
                  })
                ]
            )
        ),
      ),
    );
  }
}

Let me know if you need any more explanation. This solution is only for Android and iOS, not desktop cases.

Answered By – Didier Peran Ganthier

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More