Flutter Mobile Development · บทเรียน

สิทธิ์และเซ็นเซอร์

ขอสิทธิ์ขณะทำงานและอ่านเซ็นเซอร์ของอุปกรณ์ เช่น มาตรความเร่ง ใน Flutter เพื่อสร้างแอปที่รับรู้บริบทและผสานรวมกับแพลตฟอร์ม

บทเรียน 4 จาก 413 ขั้นตอน

สิทธิ์และเซ็นเซอร์ เป็นบทเรียน Flutter Mobile Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
22
บทเรียน
88

คำถามที่พบบ่อย

บทเรียน “สิทธิ์และเซ็นเซอร์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “สิทธิ์และเซ็นเซอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flutter Mobile Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flutter Mobile Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “สิทธิ์และเซ็นเซอร์”

ขอสิทธิ์ขณะทำงานและอ่านเซ็นเซอร์ของอุปกรณ์ เช่น มาตรความเร่ง ใน Flutter เพื่อสร้างแอปที่รับรู้บริบทและผสานรวมกับแพลตฟอร์ม คุณปฏิบัติ Flutter Mobile Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flutter Mobile Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flutter Mobile Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “สิทธิ์และเซ็นเซอร์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flutter Mobile Development นี้ได้ไหม

ได้ บทเรียน Flutter Mobile Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ช่องทางแพลตฟอร์ม (MethodChannel)
  2. ปลั๊กอินระบุตำแหน่งและกล้อง
  3. การผสานรวมส่วนติดต่อผู้ใช้แบบเนทีฟ
  4. สิทธิ์และเซ็นเซอร์
← กลับไปที่ Flutter Mobile Development