{"id":4138,"date":"2024-11-08T10:40:34","date_gmt":"2024-11-08T10:40:34","guid":{"rendered":"https:\/\/www.qworqs.com\/blog\/?p=4138"},"modified":"2024-11-08T17:15:01","modified_gmt":"2024-11-08T17:15:01","slug":"detect-platform-in-flutter","status":"publish","type":"post","link":"https:\/\/www.voodoo.business\/blog\/2024\/11\/08\/detect-platform-in-flutter\/","title":{"rendered":"Detect platform in flutter"},"content":{"rendered":"\n<p>Sometimes, you need to know what operating system your flutter app is running on, most times for theming purposes, but sometimes for functionalities !<\/p>\n\n\n\n<p>There are basically 2 popular ways to do this (3 popular methods if you are willing to use a package specifically to simplify theme issues !), let us assume we will be calling a certain method called _iosMethod if we are on iOS, and a _genericMethod if we are on any other platform<\/p>\n\n\n\n<p>The most common use case is to determine whether to use Material or Cupertino, to give the application that native look the users are used to&#8230; in any case&#8230; here are the two methods<\/p>\n\n\n\n<p>In both methods, we have a function called doStuff<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 1<\/h2>\n\n\n\n<p>In the first method, we get the platform using the Theme (We must pass context to function) ! <br>The ThemeData object has an enum called TargetPlatform&#8230; we will use that here<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  void doStuff(BuildContext context) {<br>    final platform = Theme.of(context).platform;<br>    if (platform == TargetPlatform.iOS) {<br>      _iosMethod(context);<br>    } else {<br>      _genericMethod(context);<br>    }<br>  }<br><\/pre>\n\n\n\n<p>The possible options that can come out of this enum are (android, iOS, linux, macOS, windows, fuchsia)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 2<\/h2>\n\n\n\n<p>Here, we can simply start by importing (<strong>import &#8216;dart:io&#8217; show Platform;<\/strong>), then use it directly<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import 'dart:io' show Platform;<br>...<br>...<br>  void doStuff(BuildContext context) {<br>    if (Platform.isIOS) {<br>      _iosMethod(context);<br>    } else {<br>      _genericMethod(context);<br>    }<br>  }<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Method 3<\/h2>\n\n\n\n<p>This time, we will be using the package (<a href=\"https:\/\/pub.dev\/packages\/flutter_platform_widgets\">flutter_platform_widgets<\/a>), this package is very useful for theming your application to have the look and feel of the default theme of the OS&#8230;<\/p>\n\n\n\n<p>things become very simple with this package, all you need is the line<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">return PlatformElevatedButton(onPressed: onPressed, child: child);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, you need to know what operating system your flutter app is running on, most times for theming purposes, but sometimes for functionalities ! There are basically 2 popular ways to do this (3 popular methods if you are willing to use a package specifically to simplify theme issues !), let us assume we will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[203,160],"tags":[],"class_list":["post-4138","post","type-post","status-publish","format-standard","hentry","category-dart-flutter","category-software-development"],"_links":{"self":[{"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/posts\/4138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/comments?post=4138"}],"version-history":[{"count":6,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/posts\/4138\/revisions"}],"predecessor-version":[{"id":4157,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/posts\/4138\/revisions\/4157"}],"wp:attachment":[{"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/media?parent=4138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/categories?post=4138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.voodoo.business\/blog\/wp-json\/wp\/v2\/tags?post=4138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}