How do you detect the host platform from Dart code?

前端 未结 9 2151
情书的邮戳
情书的邮戳 2020-12-02 08:57

For UI that should differ slightly on iOS and Android, i.e. on different platforms, there must be a way to detect

相关标签:
9条回答
  • 2020-12-02 09:34

    You can do

    defaultTargetPlatform == TargetPlatform.iOS
              ? kIOSTheme
              : kDefaultTheme,
    

    from import 'package:flutter/foundation.dart';

    0 讨论(0)
  • 2020-12-02 09:43
    import 'dart:io' show Platform;
    
    if (Platform.isAndroid) {
      // Android-specific code
    } else if (Platform.isIOS) {
      // iOS-specific code
    }
    

    All options include:

    Platform.isAndroid
    Platform.isFuchsia
    Platform.isIOS
    Platform.isLinux
    Platform.isMacOS
    Platform.isWindows
    

    You can also detect if you are running on the web using kIsWeb, a global constant indicating if the application was compiled to run on the web:

    import 'package:flutter/foundation.dart' show kIsWeb;
    
    if (kIsWeb) {
      // running on the web!
    } else {
      // NOT running on the web! You can check for additional platforms here.
    }
    
    • Platform documentation: https://docs.flutter.io/flutter/dart-io/Platform-class.html
    • kIsWeb documentation: https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html
    0 讨论(0)
  • 2020-12-02 09:43
    import 'dart:io' show Platform;  //at the top
    
    String os = Platform.operatingSystem; //in your code
    print(os);
    
    0 讨论(0)
提交回复
热议问题