跳到正文
DEV Community· Muhammad Uzair·· 2 小時前AI 評分30

Flutter QR Code 掃描器:20 分鐘實作教學

Flutter QR Code Scanner in 20 Minutes

AI 導讀

指南示範如何使用 Flutter 的 mobile_scanner 套件,約 20 分鐘建成可重用的 ScannerScreen,提供即時相機預覽、單次結果處理、閃光燈與鏡頭控制,以及從相片庫掃描。掃描在裝置端完成,無需網絡呼叫,並支援 Android 和 iOS;指南亦説明相機及相片庫權限設定,以及如何安全處理掃描所得的連結。

正文

QR codes are on menus, tickets, payment terminals, and product packaging, so a scanner is one of the most requested features in client apps. Building one from scratch means wiring the camera, decoding frames, and handling permissions on two platforms. The mobile_scanner package already does all three, and this guide turns it into a complete scanner screen in about 20 minutes: a live camera preview, one-time result handling, torch and camera controls, scanning from the photo gallery, and the security checks that keep a scanned code from opening something dangerous.

By the end you will have a reusable ScannerScreen widget you can drop into any Flutter app. The code targets current stable Flutter and Dart, decodes entirely on the device with no network call, and needs no native code of your own — only the camera permission entries in the Android manifest and the iOS Info.plist. The same screen also pairs well with QR generation, covered in the guide to building a secure QR medical profile in Flutter.

Why use mobile_scanner for QR scanning in Flutter?

Use mobile_scanner because it is the maintained QR and barcode scanning package for Flutter, it decodes on the device with no network call, and it works on Android and iOS from one API. The older qr_code_scanner package is no longer maintained, and the camera plugin alone gives you a preview with no barcode decoding, so you would have to ship a decoder yourself. mobile_scanner wraps Google ML Kit barcode scanning on Android and AVFoundation on iOS, so the same widget reads QR codes, Aztec, Data Matrix, EAN, and Code 128 without extra setup. Decoding runs on the device, which means a scanned code never leaves the phone until your own code decides what to do with it.

Approach What you get When to use it
mobile_scanner Camera preview plus decoding in one widget Almost always: the fastest path to a working scanner
camera plugin plus a decoder Full control over the camera pipeline You need custom frame processing, such as feeding frames to your own ML model
qr_code_scanner An older scanning widget Do not use it for new apps: it is no longer maintained

Step 1: add the package and the camera permission

Add three packages to pubspec.yaml: mobile_scanner for the scanner itself, image_picker for gallery scanning later in this guide, and url_launcher for safely opening scanned links.

\yaml
dependencies:
flutter:
sdk: flutter
mobile_scanner: ^7.0.0
image_picker: ^1.1.0
url_launcher: ^6.3.0
\
\

Then declare the camera permission. On Android, add the permission inside AndroidManifest.xml, before the application tag:

\xml
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
\
\

On iOS, add the camera usage description to Info.plist, plus the photo library description for the gallery step:

\xml
<key>NSCameraUsageDescription</key>
<string>This app uses the camera to scan QR codes.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app lets you pick a photo containing a QR code to scan it.</string>
\
\

The manifest entry and the Info.plist key only declare the permission. The operating system still asks the user the first time the camera opens, so the scanner screen must handle a denial gracefully — the errorBuilder in


Originally published at uzairxdev.online

來源:DEV Community · dev.to