Flutter Screen

Flutter Screen:

In this tutorial, we are going to study Flutter Screen Brightness settings / Brightness adjustments. Some times we need to adjust the brightness of the app screen to see all the apps properly on the app screen. and some times when the charging is low at that time we are reducing the brightness to increase the battery performance. Few examples I have mentioned hero but there are so many advantages are there with app screen brightness adjustments.

Creating Flutter Screen Brightness Settings:

Follow the below steps to create Flutter Screen brightness adjustments

Add the dependency:

Adding the dependency package to pubspec.yaml file. use the below code to add dependency package to pubspec.yaml file. After adding the package run the get package method to import all the necessary files for the Flutter Screen App.

dependencies:
screen: ^0.0.5

Install the Package:

You can install the package from the command line using the below code or method.

$ flutter packages get

Importing the Package:

After Adding the dependency package to the pubspec.yaml file , you can now import the package into the dart code by using the below code. without adding the dependency package to the pubspec.yaml file if you import it will show package not found an error. 

import 'package:screen/screen.dart';

Taking permission from Android Manifest File:

If we want to change the Flutter Screen Brightness we have to take the permission from Android Manifest.xml file. without taking the permission we can not change the Flutter Screen Brightness.  So take the Android Manifest permission using the below code as shown. 

 

 

Complete Example Code For Flutter Screen App:

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 {
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: [
new Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
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);
})
]
)
),
),
);
}
}

Congratulations You Have Learned Flutter Screen Brightness Adjustments….!!!

If It Is Helpful Share It With Your Friends….!!!

Leave a Reply

Categories