flutter_screen_light_plugin 0.0.1 flutter_screen_light_plugin: ^0.0.1 copied to clipboard
A new Flutter project.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_screen_light_plugin/flutter_screen_light_plugin.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
double _light = 0;
double _maxLight = 0;
String _lightNm = '';
@override
void initState() {
super.initState();
FlutterScreenLightPlugin.getMaxLight.then((onValue) {
print("当前屏幕最大亮度:$onValue");
_maxLight = onValue;
});
FlutterScreenLightPlugin.getCurrentLight.then((onValue) {
print("当前屏幕亮度:$onValue");
_light = onValue;
setState(() {
_lightNm='$_light';
});
});
}
void addLight() {
if(_light>_maxLight){
return;
}
_light=_light+0.1*_maxLight;
FlutterScreenLightPlugin.setLight(_light);
setState(() {
_lightNm="$_light";
});
}
void _lessLight() {
if(_light<0){
return;
}
_light=_light-0.1*_maxLight;
FlutterScreenLightPlugin.setLight(_light);
setState(() {
_lightNm="$_light";
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: <Widget>[
Text("当前亮度$_lightNm"),
FlatButton(
child: Text("增加亮度"),
onPressed: () {
addLight();
},
),
FlatButton(
child: Text("减少亮度"),
onPressed: () {
_lessLight();
},
)
],
),
),
),
);
}
}