반응형
Dart 코드에서 호스트 플랫폼을 어떻게 감지합니까?
UI의 경우에 다소 차이가해야 iOS
하고 Android
, 당신이 실행중인 하나를 감지 할 수있는 방법이 있어야합니다,하지만 난 워드 프로세서에서 그것을 찾을 수 없습니다. 뭐야?
import 'dart:io' show Platform;
if (Platform.isAndroid) {
// Android-specific code
} else if (Platform.isIOS) {
// iOS-specific code
}
기타 옵션은 다음과 같습니다.
Platform.isFuchsia
Platform.isLinux
Platform.isMacOS
Platform.isWindows
kIsWeb // A global constant indicating if the application was compiled to run on the web
- 플랫폼 문서 : https://docs.flutter.io/flutter/dart-io/Platform-class.html
- kIsWeb 문서 : https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html
Collin 덕분에 최종 답변은 다음과 같습니다.
bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
defaultTargetPlatform
작동 하지만 Theme.of(context).targetPlatform
. 이를 통해 iOS 동작을 테스트 할 수 있습니다 ( defaultTargetPlatform
항상 TargetPlatform.android
테스트 중이므로 ). 또한 위젯의 조상이 위젯으로 래핑하여 대상 플랫폼을 재정의 할 수 있습니다 Theme
.
넌 할 수있어
defaultTargetPlatform == TargetPlatform.iOS
? kIOSTheme
: kDefaultTheme,
...에서 import 'package:flutter/foundation.dart';
import 'dart:io' show Platform; //at the top
String os = Platform.operatingSystem; //in your code
print(os)
;
대부분의 "Flutter"대답은 다음과 같습니다.
import 'package:flutter/foundation.dart' show TargetPlatform;
//...
if(Theme.of(context).platform == TargetPlatform.android)
//do sth for Android
else if(Theme.of(context).platform == TargetPlatform.iOS)
//do sth else for iOS
else if(Theme.of(context).platform == TargetPlatform.fuchsia)
//even do sth else for Fuchsia OS
참고 URL : https://stackoverflow.com/questions/45924474/how-do-you-detect-the-host-platform-from-dart-code
반응형
'Nice programing' 카테고리의 다른 글
Google Maps V3에서 번호가 매겨진지도 마커를 어떻게 만들 수 있습니까? (0) | 2020.12.09 |
---|---|
iOS 충돌 보고서 : atos가 예상대로 작동하지 않음 (0) | 2020.12.09 |
API 요청 시간 초과를 가져 오시겠습니까? (0) | 2020.12.09 |
C에서 동적 다차원 배열을 어떻게 사용합니까? (0) | 2020.12.09 |
Django 파일 업로드 크기 제한 (0) | 2020.12.09 |