33
21

More than 5 years have passed since last update.

FlutterでOS判定をする

Last updated at Posted at 2018-05-05

Flutterでのアプリ開発する際にデザインは基本マテリアルデザインを踏襲していくと思いますが、
やはりiOSとAndroidでUIを変えたいと感じることがあると考えられます。
もしくは、外部サービスを利用する際に発行されるIDがAndroidとiOSは別アプリなので個別発行されることにより、内部で切り替えたいといったケースも考えられます。

そこでOSを判定する必要がでてきます。
判定するための実装内容は非常に簡単です。

import 'dart:io';

// OS名を取得、自分でごりごりやりたいならこちら
String os = Platform.operatingSystem;

// OSごとで判定するプロパティも提供されている
bool isAndroid = Platform.isAndroid;

上記のように、OSを取得して自身で振り分けをする方法と、isAndroidといったようなプロパティ任せにする方法があります。

内部では以下のような実装になっており単純に文字列比較をしてフラグ管理をしているだけです。やることは変わらないかと思われます。

//Platform.operatingSystem
static String get operatingSystem => _operatingSystem;

//Platform.isAndroid
static final bool isAndroid = (_operatingSystem == "android")

他にも、isIOSisFuchsiaisLinuxisMacOSisWindowsといったプロパティが用意されていました。どこまで視野に入れているのだろうかと思わせるプロパティ群ですね。

以上になります。

33
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
21