Flutter Mobile Development · درس

الأذونات والمستشعرات

اطلب أذونات وقت التشغيل واقرأ مستشعرات الجهاز، مثل مقياس التسارع، في Flutter لبناء تطبيقات واعية بالسياق ومتكاملة مع المنصة.

الدرس 4 من 413 خطوة

الأذونات والمستشعرات درس مجاني في Flutter Mobile Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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.0

Checking 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.0

Reading 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.

البدء مجانًا

تعلم Dart مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
22
الدروس
88

الأسئلة الشائعة

هل درس «الأذونات والمستشعرات» مجاني؟

نعم — نص درس «الأذونات والمستشعرات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flutter Mobile Development، انتقل إلى CoddyKit PRO. تتضمن دورة Flutter Mobile Development 4 دروس في المجموع.

ماذا ستتعلم في «الأذونات والمستشعرات»؟

اطلب أذونات وقت التشغيل واقرأ مستشعرات الجهاز، مثل مقياس التسارع، في Flutter لبناء تطبيقات واعية بالسياق ومتكاملة مع المنصة. تتمرن على Flutter Mobile Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flutter Mobile Development؟

لا تُشترط خبرة سابقة. Flutter Mobile Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «الأذونات والمستشعرات»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flutter Mobile Development هذا؟

نعم. كل درس في Flutter Mobile Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. قنوات المنصة (MethodChannel)
  2. الموقع الجغرافي وإضافات الكاميرا
  3. دمج واجهة المستخدم الأصلية
  4. الأذونات والمستشعرات
← العودة إلى Flutter Mobile Development