Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can flutter change the brightness of the screen?

Tags:

flutter

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?

like image 811
AceBlackGloss Avatar asked Aug 03 '20 16:08

AceBlackGloss


1 Answers

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.

like image 102
Didier Peran Ganthier Avatar answered Oct 19 '22 23:10

Didier Peran Ganthier