import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' show TargetPlatform;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
var platform = Theme.of(context).platform;
return MaterialApp(
title: 'My Multiplatform App',
home: Scaffold(
appBar: AppBar(
title: Text(platform == TargetPlatform.iOS ? 'iOS' : 'Android or other'),
),
),
);
}
}
Result on iPhone:
Result on Android:
While Platform.isIOS
or Platform.isAndroid
will work in emulators, there are two issues:
Theme
has a different target platform, the widget won’t properly inherit the parent widget’s platform (as Platform.isX
is oblivious to the context
)TargetPlatform.android
so we are not able to test iOS behavior in our appTherefore, always use Theme.of(context).platform
!