权限与传感器
在 Flutter 中请求运行时权限并读取加速度计等设备传感器,构建感知上下文且融合集成平台功能的应用。
权限与传感器 是 CoddyKit 上的免费 Flutter Mobile Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flutter Mobile Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flutter Mobile Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Permissions & Sensors?
Many platform features — camera, location, sensors — require runtime permission. And sensors like the accelerometer let your app react to physical movement.
The permission_handler Package
The permission_handler package gives a unified API for requesting and checking permissions across Android and iOS.
dependencies:
permission_handler: ^11.0.0Checking Status
Check whether a permission is granted before using a feature.
final status = await Permission.camera.status;
if (status.isGranted) {
openCamera();
}Requesting a Permission
Call request() to prompt the user. Always handle a denial gracefully.
final result = await Permission.location.request();
if (result.isGranted) {
startTracking();
}Permanently Denied
If the user permanently denies, you cannot re-prompt — send them to settings instead.
if (status.isPermanentlyDenied) {
await openAppSettings();
}Declaring Permissions
You must also declare permissions natively: AndroidManifest.xml on Android and Info.plist usage descriptions on iOS.
<!-- Android -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- iOS Info.plist -->
<key>NSCameraUsageDescription</key>
<string>We need the camera to scan codes</string>Meet the Sensors
The sensors_plus package exposes device motion sensors as streams.
- Accelerometer
- Gyroscope
- Magnetometer
dependencies:
sensors_plus: ^4.0.0Reading the Accelerometer
Subscribe to the accelerometer stream to get x, y, z acceleration values.
accelerometerEventStream().listen((event) {
print('x: ' + event.x.toString() + ' y: ' + event.y.toString());
});The Gyroscope
The gyroscope reports rotation rate around each axis — useful for tilt and orientation effects.
gyroscopeEventStream().listen((event) {
print('rotation: ' + event.x.toString());
});Detecting a Shake
Combine accelerometer axes into a magnitude and compare against a threshold to detect a shake gesture.
import 'dart:math';
void onEvent(AccelerometerEvent e) {
final magnitude = sqrt(e.x * e.x + e.y * e.y + e.z * e.z);
if (magnitude > 15) onShake();
}Cleaning Up Subscriptions
Sensor streams keep firing — cancel the subscription in dispose to save battery.
late StreamSubscription sub;
@override
void dispose() {
sub.cancel();
super.dispose();
}Quick Check
The user permanently denied location. What should your app do?
Recap
You learned permissions and sensors:
- permission_handler: status, request, openAppSettings
- Native declarations on Android and iOS
- sensors_plus accelerometer and gyroscope streams
- Shake detection and cleanup
Your apps can now responsibly access platform capabilities.
用 AI 导师学习 Dart — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 22
- 课程
- 88
常见问题解答
「权限与传感器」课时是免费的吗?
是的 — 「权限与传感器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flutter Mobile Development 课程的其余内容,请升级到 CoddyKit PRO。 Flutter Mobile Development 课程共包含 4 节课。
「权限与传感器」这节课中我会学到什么?
在 Flutter 中请求运行时权限并读取加速度计等设备传感器,构建感知上下文且融合集成平台功能的应用。 你通过在浏览器中直接运行的动手代码来练习 Flutter Mobile Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flutter Mobile Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flutter Mobile Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「权限与传感器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flutter Mobile Development 课中编写并运行代码吗?
能。每节 Flutter Mobile Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。