Building a platform-specific UI
In a Flutter application, there are two primary ways to access the platform that the application is running on: statically (using platform flags) or dynamically (using the TargetPlatform
object from BuildContext
). The platform can be detected statically using the list of platform flags provided by the Flutter framework, as detailed here:
isLinux
isMacOS
isWindows
isAndroid
isIOS
isFuschia
Each name implies the platform in which the application is executing. The tradeoff of using these flags is that they cannot be used to dynamically switch the styles for a platform. To do so, we turn to TargetPlatform
defined on context. Before we dive into using MaterialApp
to build our Settings application, let’s demonstrate how to access the platform dynamically.
We can access the target platform information directly from BuildContext
by using Theme.of(context).platform
. Open the app.dart
file and add the following code...