【发布时间】:2020-01-16 03:15:36
【问题描述】:
我知道我可以使用Platform.isAndroid、Platform.isIOS 等来检测操作系统,但没有像Platform.isWeb 这样的东西,那么我该如何检测呢?
【问题讨论】:
-
有关更多信息,您可以使用以下链接查看我在其他问题中的答案。 stackoverflow.com/a/70039641/9985458
我知道我可以使用Platform.isAndroid、Platform.isIOS 等来检测操作系统,但没有像Platform.isWeb 这样的东西,那么我该如何检测呢?
【问题讨论】:
有一个全局布尔值kIsWeb 可以告诉您应用程序是否已编译为在网络上运行。
文档:https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html
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.
}
【讨论】:
isFuchsia 和isLinux,但我还是得来这里看看如何检测网络。
下面编写了一段代码来获取运行 Flutter 的 OS/web...
if(kIsWeb)
return Text("It's web");
else if(Platform.isAndroid){
return Text("it's Android"); }
【讨论】:
kIsweb 常量?
如果你想知道你的操作系统在网络上是什么,你可以使用
String platform = "";
if (kIsWeb) {
platform = getOSInsideWeb();
}
String getOSInsideWeb() {
final userAgent = window.navigator.userAgent.toString().toLowerCase();
if( userAgent.contains("iphone")) return "ios";
if( userAgent.contains("ipad")) return "ios";
if( userAgent.contains("android")) return "Android";
return "Web";
}
【讨论】:
您可以使用此代码轻松确定颤动屏幕 我希望这会对某人有所帮助。提前感谢 这是我的代码
bool isMobile = MediaQuery.of(context).size.width < 850;
bool isTablet = MediaQuery.of(context).size.width < 1100 &&
MediaQuery.of(context).size.width >= 850;
bool isDesktop = MediaQuery.of(context).size.width >= 1100;
【讨论】: