How do you detect the host platform from Dart code?

前端 未结 9 2150
情书的邮戳
情书的邮戳 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:26

    You can use Universal Platform package:

    https://pub.dev/packages/universal_platform

    import 'package:universal_platform/universal_platform.dart';
    
    bool isIos = UniversalPlatform.isIOS;
    bool isAndroid = UniversalPlatform.isAndroid;
    bool isWeb = UniversalPlatform.isWeb;
    print('iOS: $isIos');
    print('Android: $isAndroid');
    print('Web: $isWeb');
    
    0 讨论(0)
  • 2020-12-02 09:29

    Most "Flutter" answer is as follows:

    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
    
    0 讨论(0)
  • Although defaultTargetPlatform will work, I would suggest using Theme.of(context).targetPlatform. This enables testing of iOS behavior (because defaultTargetPlatform is always TargetPlatform.android in tests). It also allows ancestors of your widget to override its target platform by wrapping it in a Theme widget.

    0 讨论(0)
  • 2020-12-02 09:30

    for more simple way you just use this return method, that access full project. like i print these on button click.

    import 'package:flutter/material.dart';
    import 'package:gymmanager/components/platformCheck.dart';
    
    class SplashScreen extends StatefulWidget {
      @override
      _SplashScreenState createState() => _SplashScreenState();
    }
    
    class _SplashScreenState extends State<SplashScreen> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: RaisedButton(
              child: Text("Platform check"),
              onPressed: () async {
                var platform = await platformName();
                print("Platform :- " + platform);
              },
            ),
          ),
        );
      }
    }
    

    and this is your platformCheck.dart class

    import 'dart:io' show Platform;
    import 'package:flutter/foundation.dart' show kIsWeb;
    
    Future<String> platformName() {
      var platformName = '';
      if (kIsWeb) {
        platformName = "Web";
      } else {
        if (Platform.isAndroid) {
          platformName = "Android";
        } else if (Platform.isIOS) {
          platformName = "IOS";
        } else if (Platform.isFuchsia) {
          platformName = "Fuchsia";
        } else if (Platform.isLinux) {
          platformName = "Linux";
        } else if (Platform.isMacOS) {
          platformName = "MacOS";
        } else if (Platform.isWindows) {
          platformName = "Windows";
        }
      }
      return Future.value(platformName);
    }
    
    0 讨论(0)
  • 2020-12-02 09:32

    Thanks to Collin, the final answer is:

    bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
    
    0 讨论(0)
  • 2020-12-02 09:34

    It is simple just import the io library

    import'dart:io' show Platform;
    void main(){
    if(Platform.isIOS){
      return someThing();
    }else if(Platform.isAndroid){
      return otherThing();
    }else if(Platform.isMacOS){
      return anotherThing();
    }
    

    or in very simple way

    Platform.isIOS ? someThing() : anOther(),
    
    0 讨论(0)
提交回复
热议问题