[flutter] device_info 라이브러리를 이용하여 디바이스의 카메라 플래시 지원 여부 확인하기

Flutter 앱을 개발할 때 카메라 플래시를 사용해야 할 경우, 디바이스가 플래시를 지원하는지 먼저 확인해야 합니다. 이를 위해 device_info 라이브러리를 사용하여 디바이스의 특성을 가져와 확인할 수 있습니다.

device_info 라이브러리 설치하기

먼저, pubspec.yaml 파일에 다음과 같이 device_info 라이브러리를 추가해주세요:

dependencies:
  device_info: ^2.0.0

그리고 패키지를 가져오기 위해 다음 명령어를 실행하세요:

$ flutter pub get

카메라 플래시 지원 여부 확인하기

다음은 device_info 라이브러리를 사용하여 카메라 플래시 지원 여부를 확인하는 예시 코드입니다. async-await 과정을 거쳐 비동기적으로 실행됩니다:

import 'package:flutter/material.dart';
import 'package:device_info/device_info.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  Future<bool> isFlashSupported() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo;
    IosDeviceInfo iosInfo;

    if (Theme.of(context).platform == TargetPlatform.android) {
      androidInfo = await deviceInfo.androidInfo;
      return androidInfo != null && androidInfo.hasFlash;
    } else if (Theme.of(context).platform == TargetPlatform.iOS) {
      iosInfo = await deviceInfo.iosInfo;
      return iosInfo != null && iosInfo.hasFlash;
    }

    return false;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flash Support Checker'),
        ),
        body: Center(
          child: RaisedButton(
            onPressed: () async {
              bool flashSupported = await isFlashSupported();
              if (flashSupported) {
                print('Flash is supported on this device');
                // 카메라 플래시를 사용할 수 있는 로직 추가
              } else {
                print('Flash is not supported on this device');
                // 카메라 플래시를 사용할 수 없는 로직 추가
              }
            },
            child: Text('Check Flash Support'),
          ),
        ),
      ),
    );
  }
}

위 예시 코드는 Flutter 앱의 홈화면에 “Check Flash Support” 버튼이 표시됩니다. 버튼이 눌렸을 때, isFlashSupported 함수를 호출하여 카메라 플래시를 지원하는지 확인합니다. 만약 플래시를 지원한다면 “Flash is supported on this device” 메시지가 출력되고, 플래시를 지원하지 않는다면 “Flash is not supported on this device” 메시지가 출력됩니다.

마무리

위의 예시 코드를 사용하여 Flutter 앱에서 디바이스의 카메라 플래시 지원 여부를 확인할 수 있습니다. device_info 라이브러리를 통해 디바이스의 특성을 가져와서 플래시의 지원 여부를 확인하는 방법은 간단하고 효과적입니다. 디바이스의 특성을 활용하여 필요한 로직을 추가하면 됩니다.