对于在iOS和Android上略有不同的UI,即在不同的平台上,必须有一种方法来检测应用程序在哪个平台上运行,但我在文档中找不到它。是什么?
当前回答
您可以将这个扩展文件(platform_ext.dart)添加到项目中并调用任何对象
import 'dart:io';
import 'package:flutter/foundation.dart' show kIsWeb;
extension Target on Object {
bool isAndroid() {
return Platform.isAndroid;
}
bool isIOS() {
return Platform.isIOS;
}
bool isLinux() {
return Platform.isLinux;
}
bool isWindows() {
return Platform.isWindows;
}
bool isMacOS() {
return Platform.isMacOS;
}
bool isWeb() {
return kIsWeb;
}
// ···
}
只需导入文件并调用它
import 'platform_ext.dart';
....
@override
Widget build(BuildContext context) {
return isAndroid()? Text("Android"):Text("Not Android");
}
其他回答
Dart主机平台检查。
导入'dart:io'作为io;
_checkingHostPlatform(){
if(IO.Platform.isAndroid){
//Execute code for android
}else if(IO.Platform.isIOS){
//Execute code for iOS
}else{
//Execute code for other platforms
}
}
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.
}
您可以将此扩展添加到项目中
import 'package:flutter/material.dart';
extension PlatformExtension on BuildContext {
bool get isMobile =>
Theme.of(this).platform == TargetPlatform.iOS ||
Theme.of(this).platform == TargetPlatform.android;
bool get isDesktop =>
Theme.of(this).platform == TargetPlatform.macOS ||
Theme.of(this).platform == TargetPlatform.windows ||
Theme.of(this).platform == TargetPlatform.linux;
}
extension TargetPlatformExtension on TargetPlatform {
bool get isMobile =>
this == TargetPlatform.iOS || this == TargetPlatform.android;
bool get isDesktop =>
this == TargetPlatform.linux ||
this == TargetPlatform.macOS ||
this == TargetPlatform.windows;
}
现在您可以使用。
内置context => context.isDesktop 目标平台=> defaulttarded平台
建议从上下文访问平台。
要检测您的应用程序是否在浏览器上运行,您可以轻松地使用基础库中的kIsWeb常量。
import 'package:flutter/foundation.dart';
log('$kIsWeb'); // will print true if the app is running on a browser
注意,如果你在桌面上运行web应用程序,isDesktop getter将返回true,对于移动平台也是如此。 为了避免这种情况。
import 'package:flutter/foundation.dart';
if (!kIsWeb && defaultTargetPlatform.isDesktop) {
// do stuff for desktop apps only
}
if (!kIsWeb && defaultTargetPlatform.isMobile) {
// do stuff for mobile apps only
}
您可以修改扩展以获得您喜欢的实现。
import 'dart:io' show Platform;
if (Platform.isAndroid) {
// Android-specific code
} else if (Platform.isIOS) {
// iOS-specific code
}
所有选项包括:
Platform.isAndroid
Platform.isFuchsia
Platform.isIOS
Platform.isLinux
Platform.isMacOS
Platform.isWindows
你也可以使用kIsWeb来检测你是否在web上运行,kIsWeb是一个全局常量,指示应用程序是否被编译为在web上运行:
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.
}
平台文档:https://api.flutter.dev/flutter/dart-io/Platform-class.html kIsWeb文档:https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html
如果您只是需要一个字符串来记录日志,您可以使用Platform。operatingSystem,返回操作系统名称为小写字符串。
import 'dart:io';
import 'package:flutter/foundation.dart';
String _getPlatform() {
if (kIsWeb) return 'web';
return Platform.operatingSystem;
}
推荐文章
- 如何在颤振小部件中创建超链接?
- 错误地使用父数据小部件。扩展小部件必须放置在flex小部件中
- 颤振给容器圆形边界
- Flutter: RenderBox没有布局
- 颤振插件未安装错误;当运行'扑动医生'时
- 如何改变appBar返回按钮的颜色
- 如何在扑动中设置按钮的宽度和高度?
- 我如何“休眠”Dart程序
- 在Flutter app上检查是否有网络连接
- 如何禁用或覆盖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 "