如何在运行时检查平台
How to check platform at runtime
如何在运行时检查平台 (Android/iOS
)?
如果我在 Android
而不是 iOS
,我想改变我的 flutter 应用程序行为。
像这样:
_openMap() async {
// Android
var url = 'geo:52.32,4.917';
if (/* i'm on iOS */) {
url = 'http://maps.apple.com/?ll=52.32,4.917';
}
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
我在 SO 上搜索了一下,也尝试 Google 它但是这个场景没有很好的索引,所以我认为我的问题和答案可以帮助开始在 Flutter 上开发。
如果您必须在运行时检查 OS 或设备的平台,您可以使用 Platform
class of dart.io
library.
import 'dart:io'
这样你就可以像这样检查:
_openMap() async {
// Android
var url = 'geo:52.32,4.917';
if (Platform.isIOS) {
// iOS
url = 'http://maps.apple.com/?ll=52.32,4.917';
} else if (Platform.isWindows) {
// TODO - something to do?
}
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
相反,如果您还需要对设备有更深入的了解,您可以使用 dart device_info package
。
有个很好的例子here。
这样您不仅可以检查您 运行 所在的平台,还可以检查 OS 的特定版本(iOS 9, 10.3, 11.x, Lollipop, Jellybean
等)和许多其他设备信息。
更新:
在 Flutter Live 2018 之后 --> 查看这个 gr8 youtube video 了解平台感知小部件以及与 Android 和 iOS UI 兼容的最佳方式来自相同的代码库。
获取当前平台的推荐方法是使用Theme
。
Theme.of(context).platform
这样,您可以在运行时使用自定义 Theme
覆盖该值并立即看到所有更改。
import 'dart:io'
String os = Platform.operatingSystem;
这是检查平台的简单方法,还可以提供有关正在使用的设备的许多其他有用信息。 Link 到关于 Platform class.
的文档
如何在运行时检查平台 (Android/iOS
)?
如果我在 Android
而不是 iOS
,我想改变我的 flutter 应用程序行为。
像这样:
_openMap() async {
// Android
var url = 'geo:52.32,4.917';
if (/* i'm on iOS */) {
url = 'http://maps.apple.com/?ll=52.32,4.917';
}
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
我在 SO 上搜索了一下,也尝试 Google 它但是这个场景没有很好的索引,所以我认为我的问题和答案可以帮助开始在 Flutter 上开发。
如果您必须在运行时检查 OS 或设备的平台,您可以使用 Platform
class of dart.io
library.
import 'dart:io'
这样你就可以像这样检查:
_openMap() async {
// Android
var url = 'geo:52.32,4.917';
if (Platform.isIOS) {
// iOS
url = 'http://maps.apple.com/?ll=52.32,4.917';
} else if (Platform.isWindows) {
// TODO - something to do?
}
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
相反,如果您还需要对设备有更深入的了解,您可以使用 dart device_info package
。
有个很好的例子here。
这样您不仅可以检查您 运行 所在的平台,还可以检查 OS 的特定版本(iOS 9, 10.3, 11.x, Lollipop, Jellybean
等)和许多其他设备信息。
更新:
在 Flutter Live 2018 之后 --> 查看这个 gr8 youtube video 了解平台感知小部件以及与 Android 和 iOS UI 兼容的最佳方式来自相同的代码库。
获取当前平台的推荐方法是使用Theme
。
Theme.of(context).platform
这样,您可以在运行时使用自定义 Theme
覆盖该值并立即看到所有更改。
import 'dart:io'
String os = Platform.operatingSystem;
这是检查平台的简单方法,还可以提供有关正在使用的设备的许多其他有用信息。 Link 到关于 Platform class.
的文档