How do you detect the host platform from Dart code?

前端 未结 9 2221
情书的邮戳
情书的邮戳 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: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 {
      @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 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);
    }
    

提交回复
热议问题