screen_brightness

Creator: coderz1093

Last updated:

Add to Cart

Description:

screen brightness

screen_brightness #
A Plugin for controlling screen brightness with application life cycle reset implemented.
This plugin only changes application brightness not system brightness. So no permission is needed for this plugin.
Getting Started #
Install #
Add the following lines in your pubspec.yaml file
screen_brightness: ^latest_version
copied to clipboard
latest_version:

API #
System brightness
Future<double> get systemBrightness async {
try {
return await ScreenBrightness().system;
} catch (e) {
print(e);
throw 'Failed to get system brightness';
}
}
copied to clipboard
Current brightness
Future<double> get currentBrightness async {
try {
return await ScreenBrightness().current;
} catch (e) {
print(e);
throw 'Failed to get current brightness';
}
}
copied to clipboard
Set brightness
Future<void> setBrightness(double brightness) async {
try {
await ScreenBrightness().setScreenBrightness(brightness);
} catch (e) {
print(e);
throw 'Failed to set brightness';
}
}
copied to clipboard
Reset brightness
Future<void> resetBrightness() async {
try {
await ScreenBrightness().resetScreenBrightness();
} catch (e) {
print(e);
throw 'Failed to reset brightness';
}
}
copied to clipboard
Current brightness changed stream
@override
Widget build(BuildContext context) {
return StreamBuilder<double>(
stream: ScreenBrightness().onCurrentBrightnessChanged,
builder: (context, snapshot) {
double changedBrightness = currentBrightness;
if (snapshot.hasData) {
changedBrightness = snapshot.data!;
}

return Text('current brightness $changedBrightness');
},
);
}
copied to clipboard
Has changed
@override
Widget build(BuildContext context) {
return FutureBuilder<bool>(
future: ScreenBrightness().hasChanged,
builder: (context, snapshot) {
return Text(
'Brightness has changed via plugin: ${snapshot.data}');
},
);
}
copied to clipboard
Auto reset (iOS only) (experiment feature things maybe weird)
bool isAutoReset = true;

Future<void> getAutoResetSetting() async {
if (!Platform.isIOS) {
return;
}

final _isAutoReset = await ScreenBrightness().isAutoReset;
setState(() {
isAutoReset = _isAutoReset;
});
}

@override
Widget build(BuildContext context) {
return Switch(
value: isAutoReset,
onChanged: !Platform.isIOS
? null
: (value) async {
await ScreenBrightness().setAutoReset(value);
await getAutoResetSetting();
},
);
}
copied to clipboard
Usage #

DON'T use didChangeAppLifecycleState to set or reset brightness because this plugin already implemented this function.
You may also use this plugin with wakelock to prevent screen sleep

Maintainer #
Jack Liu

License

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Customer Reviews

There are no reviews.