For UI that should differ slightly on iOS and Android, i.e. on different platforms, there must be a way to detect which one the app is running on, but I couldn't find it in the docs. What is it?
11 Answers
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.htmlkIsWeb
documentation: https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html
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.
if (Platform.isAndroid) {
// Android-specific code/UI Component
} else if (Platform.isIOS) {
// iOS-specific code/UI Component
}
Don't forget to import IO Library.
import 'dart:io';
If you are using import 'dart:html';
too in same file then you have to specify Platform definition as both libraries has definition of "Platform"
in that case use Platform Specific Code like Below:
import 'dart:io' as IO;
import 'dart:html';
if (IO.Platform.isAndroid) {
// Android-specific code/UI Component
} else if (IO.Platform.isIOS) {
// iOS-specific code/UI Component
}
If you are looking into Platform Integration properly I would Suggest Use Complete Example on Flutter site: https://flutter.dev/docs/development/platform-integration/platform-channels
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
for more simple way for web and app both.try this
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;
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";
}
}
print("platformName :- "+platformName.toString());
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');