ambient_light
Um plugin poderoso para acessar dados do sensor de luz ambiente no Android, iOS e macOS
ali-you/ambient-light-plugin open-source repository details.
{"sdk":"flutter"}{"sdk":"flutter"}^2.0.2{"sdk":"flutter"}^5.0.0Texto original em inglês. Visite o GitHub para a versão atual.
A Flutter plugin to access ambient light sensor data on Android, iOS and macOS. This plugin allows you to retrieve the current ambient light level and listen to continuous updates.
SensorManager to access the device's ambient light sensor.CoreMotion to access the ambient light sensor data on compatible iOS devices.IOKit to access the ambient light sensor data on compatible macOS devices.To use this plugin, you can add it to your Flutter project in one of two ways:
pubspec.yamlInclude the following dependency in your pubspec.yaml file:
dependencies:
ambient_light: ^0.2.1
Run the following command to add the plugin directly to your project:
flutter pub add ambient_light
Import the package and use the provided methods to get ambient light sensor data.
import 'package:ambient_light/ambient_light.dart';
void main() async {
final AmbientLight _ambientLight = AmbientLight(frontCamera: true);
// Get ambient light value
double? lightLevel = await _ambientLight.currentAmbientLight();
print('Ambient light level: $lightLevel');
// Listen to ambient light sensor data stream
_ambientLight.ambientLightStream.listen((double lightLevel) {
print('Ambient light level: $lightLevel');
});
}
[!NOTE] Useful information that users should know, even when skimming content.
- The frontCamera parameter is iOS only and allows you to specify whether to use the front or back camera for measuring ambient light. For other platforms, you can initialize with the default:
final AmbientLight _ambientLight = AmbientLight();- Checkout Example for complete explanation
Future<double?> currentAmbientLight();
Returns the current ambient light level as a double. Returns null if the sensor is not available.
Stream<double> get ambientLightStream;
Returns a stream of ambient light sensor data as double.
For iOS, the plugin uses CoreMotion to access ambient light sensor data. You need to add the
following key to your Info.plist to request access to the camera, which is required for measuring
ambient light.
<key>NSCameraUsageDescription</key>
<string>We need access to the camera to measure ambient light.</string>
Contributions are welcome! Please submit a pull request or open an issue to discuss any changes.
This project is licensed under the BSD 3-Clause License. See the LICENSE file for details.