对于在iOS和Android上略有不同的UI,即在不同的平台上,必须有一种方法来检测应用程序在哪个平台上运行,但我在文档中找不到它。是什么?
当前回答
如果你同时导入"dart:io"和"dart:html",它不知道要导入哪个平台并给出错误。导入其中一个。
import 'dart:io';
Platform.isIOS ? CupertinoWidget() : MaterialWidget()
其他回答
所以这里有一个小工具,我用来检测平台和操作系统的反应适当。
import'dart:io' show Platform;
import 'package:flutter/foundation.dart' show kIsWeb;
class Util {
os getPlatform() {
if (kIsWeb) {
return os.Web;
} else if (Platform.isIOS) {
return os.IOS;
} else if (Platform.isAndroid) {
return os.Android;
} else if (Platform.isFuchsia) {
return os.Fuchsia;
} else if (Platform.isLinux) {
return os.Linux;
} else if (Platform.isMacOS) {
return os.MacOS;
} else if (Platform.isWindows) {
return os.Windows;
}
return os.Unknown;
}
bool isWeb() {
return (getPlatform()==os.Web);
}
bool isMobile() {
os platform = getPlatform();
return (platform == os.Android || platform == os.IOS || platform== os.Fuchsia);
}
bool isComputer() {
os platform = getPlatform();
return (platform == os.Linux || platform == os.MacOS || platform== os.Windows);
}
}
enum os { Unknown, Web, Android, Fuchsia, IOS, Linux, MacOS, Windows }
你可以这样做
defaultTargetPlatform == TargetPlatform.iOS
? kIOSTheme
: kDefaultTheme,
从导入'包:flutter/foundation.dart';
这个自定义创建的类将帮助您检测平台:
import 'package:flutter/foundation.dart'
show defaultTargetPlatform, kIsWeb, TargetPlatform;
enum Os {
web,
android,
ios,
macOS,
linux,
windows,
fuchsia,
}
class Platform {
const Platform();
/// Platform is Web.
static bool get isWeb => os == Os.web;
/// Platform is Android.
static bool get isAndroid => os == Os.android;
/// Platform is IOS.
static bool get isIOS => os == Os.ios;
/// Platform is Fuchsia.
static bool get isFuchsia => os == Os.fuchsia;
/// Platform is Linux.
static bool get isLinux => os == Os.linux;
/// Platform is MacOS.
static bool get isMacOS => os == Os.macOS;
/// Platform is Windows.
static bool get isWindows => os == Os.windows;
/// Platform is Android or IOS.
static bool get isMobile => isAndroid || isIOS;
/// Platform is Android or IOS or Fuchsia.
static bool get isFullMobile => isMobile || isFuchsia;
/// Platform is Linux or Windows or MacOS.
static bool get isDesktop => isLinux || isWindows || isMacOS;
/// Getting the os name.
static Os get os {
if (kIsWeb) {
return Os.web;
}
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return Os.android;
case TargetPlatform.iOS:
return Os.ios;
case TargetPlatform.macOS:
return Os.macOS;
case TargetPlatform.windows:
return Os.windows;
case TargetPlatform.fuchsia:
return Os.fuchsia;
case TargetPlatform.linux:
return Os.linux;
}
}
}
import 'dart:io' show Platform;
if (Platform.isAndroid) {
// Android-specific code
} else if (Platform.isIOS) {
// iOS-specific code
}else if (Platform.isFuchsia) {
// Fuchsia-specific code
}else if (Platform.isLinux) {
// Linux-specific code
}else if (Platform.isMacOS) {
// MacOS-specific code
}else if (Platform.isWindows) {
// Windows-specific code
}else if (Platform.isWindows) {
// Windows-specific code
}
网络
import 'package:flutter/foundation.dart' show kIsWeb;
if (kIsWeb) {
// running on the web!
} else {
// NOT running on the web! You can check for additional platforms here.
}
if (Platform.isAndroid) {
// Android-specific code/UI Component
} else if (Platform.isIOS) {
// iOS-specific code/UI Component
}
不要忘记导入IO库。
import 'dart:io';
如果你使用导入'dart:html';那么你必须指定平台定义,因为两个库都有“平台”的定义
在这种情况下,使用如下所示的平台特定代码:
import 'dart:io' as IO;
import 'dart:html';
if (IO.Platform.isAndroid) {
// Android-specific code/UI Component
} else if (IO.Platform.isIOS) {
// iOS-specific code/UI Component
}
如果你正在寻找合适的平台集成,我建议在Flutter网站上使用完整的示例: https://flutter.dev/docs/development/platform-integration/platform-channels
推荐文章
- 如何禁用或覆盖Android的“返回”按钮,在扑动?
- 如何在扑动中垂直和水平居中文本?
- Flutter and google_sign_in plugin: PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10:, null)
- 如何在扑动中格式化日期时间
- 在Flutter中Column的子元素之间的空间
- Visual Studio代码- URI的目标不存在" package:flutter/material.dart "
- Flutter:升级游戏商店的版本代码
- 在一个子树中有多个英雄共享相同的标签
- 如何检查Flutter应用程序是否正在调试中运行?
- 在Flutter中向有状态小部件传递数据
- 未处理异常:ServicesBinding.defaultBinaryMessenger在绑定初始化之前被访问
- 出现键盘时,Flutter小部件将调整大小。如何预防这种情况?
- 颤振-换行文本
- 如何在Dart中四舍五入到小数点后的给定精度程度?
- 添加一个启动屏幕颤振应用程序