ambient_light 包提供了一种跨平台解决方案,可在 Android、iOS、macOS、Windows 和 Linux 上访问环境光传感器数据。它使应用程序能够检测周围光线水平,从而实现动态 UI 调整,例如自动亮度调节或自适应显示设置。该插件轻量且与 Flutter 应用无缝集成,通过流提供实时传感器更新。它支持在可用情况下进行前台和后台运行,非常适合节能且具备上下文感知能力的应用程序。
适合场景
自动亮度调节
节能显示模式
上下文感知的用户界面设计
环境监测应用
智能照明集成
主要特点
实时光照水平流
跨平台支持(Android、iOS、macOS、Windows、Linux)
简单的 API,支持基于流的更新
低功耗
背景传感器访问
适合对象
需要环境意识的应用程序
无障碍功能
电池优化工具
智能家居集成
可穿戴设备和移动应用
使用建议
传感器可用性取决于设备硬件
某些平台上的精度有限
需要在 Android 和 iOS 上具备适当的权限
并非所有较旧设备都支持
后台使用可能受到操作系统策略的限制
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Ambient Light for iOS and android
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.
Features
Android: Uses the SensorManager to access the device's ambient light sensor.
iOS: Uses CoreMotion to access the ambient light sensor data on compatible iOS devices.
macOS: Uses IOKit to access the ambient light sensor data on compatible macOS devices.
Retrieve Current Light Level: Get the current ambient light level as a single value.
Stream Light Level: Listen to continuous updates of the ambient light level.
Installation
To use this plugin, you can add it to your Flutter project in one of two ways:
1. Add to pubspec.yaml
Include the following dependency in your pubspec.yaml file:
dependencies:ambient_light:^0.2.1
2. Add directly from the terminal
Run the following command to add the plugin directly to your project:
flutter pub add ambient_light
Usage
Import the package and use the provided methods to get ambient light sensor data.
[!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:
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.
iOS
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
Contributions are welcome! Please submit a pull request or open an issue to discuss any changes.
Licence
This project is licensed under the BSD 3-Clause License. See
the LICENSE file for
details.