Nice programing

Dart 코드에서 호스트 플랫폼을 어떻게 감지합니까?

nicepro 2020. 12. 9. 21:47
반응형

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

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

반응형