For UI that should differ slightly on iOS and Android, i.e. on different platforms, there must be a way to detect
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');
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
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.
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);
}
Thanks to Collin, the final answer is:
bool isIOS = Theme.of(context).platform == TargetPlatform.iOS;
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(),