I wanted to make an app to regulate the phone brightness (android) but cannot find anything.
The only thing that I found is the screen plugin but visual studio code doesn't accept it.
Do you guy maybe have a practical example that I can use?
Try use screen_brightness, like this:
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.red,
body: Center(
child: FutureBuilder<double>(
future: ScreenBrightness().current,
builder: (context, snapshot) {
double currentBrightness = 0;
if (snapshot.hasData) {
currentBrightness = snapshot.data!;
}
return StreamBuilder<double>(
stream: ScreenBrightness().onCurrentBrightnessChanged,
builder: (context, snapshot) {
double changedBrightness = currentBrightness;
if (snapshot.hasData) {
changedBrightness = snapshot.data!;
}
return Column(
mainAxisSize: MainAxisSize.min,
children: [
FutureBuilder<bool>(
future: ScreenBrightness().hasChanged,
builder: (context, snapshot) {
return Text(
'Brightness has changed via plugin: ${snapshot.data}');
},
),
Text('Current brightness: $changedBrightness'),
Slider.adaptive(
value: changedBrightness,
onChanged: (value) {
setBrightness(value);
},
),
],
);
},
);
},
),
),
);
}
Future<void> setBrightness(double brightness) async {
try {
await ScreenBrightness().setScreenBrightness(brightness);
} catch (e) {
debugPrint(e.toString());
throw 'Failed to set brightness';
}
}
Future<double> get currentBrightness async {
try {
return await ScreenBrightness().current;
} catch (e) {
print(e);
throw 'Failed to get current brightness';
}
}
}
Remember that this package won't work on emulator.