How do you discover the host platform from the Dart code?

For the user interface, which should be slightly different on iOSand Android, there should be a way to determine which one you are working on, but I could not find it in the documentation. What is it?

+27
source share
4 answers

Thanks Collin, the final answer is:

bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
+27
source
import 'dart:io' show Platform;

if (Platform.isAndroid) {
  // Android-specific code
} else if (Platform.isIOS) {
  // iOS-specific code
}

Other options include:

Platform.isFuchsia
Platform.isLinux
Platform.isMacOS
Platform.isWindows

Docs: https://docs.flutter.io/flutter/dart-io/Platform-class.html.

+25
source

defaultTargetPlatform , Theme.of(context).targetPlatform. iOS ( defaultTargetPlatform TargetPlatform.android ). , Theme.

+10
source

You can do

defaultTargetPlatform == TargetPlatform.iOS
          ? kIOSTheme
          : kDefaultTheme,

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

+7
source

Source: https://habr.com/ru/post/1684622/


All Articles