【问题标题】:How can I detect if my Flutter app is running in the web?如何检测我的 Flutter 应用程序是否在 Web 中运行?
【发布时间】:2020-01-16 03:15:36
【问题描述】:

我知道我可以使用Platform.isAndroidPlatform.isIOS 等来检测操作系统,但没有像Platform.isWeb 这样的东西,那么我该如何检测呢?

【问题讨论】:

标签: flutter dart


【解决方案1】:

有一个全局布尔值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.
}

【讨论】:

  • 如果一个应用程序被编译为在网络上运行,而我现在在android/ios上运行一个应用程序,那么这段代码是否可以执行?
  • 它会给我力量,让我可以让应用程序在网络上运行,而不是在 android/ios/software/windows 中运行吗?
  • 别忘了导入... import 'package:flutter/foundation.dart';
  • 是否可以检测桌面与移动设备?
  • 讽刺的是,我们已经有了isFuchsiaisLinux,但我还是得来这里看看如何检测网络。
【解决方案2】:

下面编写了一段代码来获取运行 Flutter 的 OS/web...

if(kIsWeb)
   return Text("It's web");

else if(Platform.isAndroid){
     return Text("it's Android"); }

【讨论】:

  • 如何在单元测试期间测试kIsweb 常量?
【解决方案3】:

如果你想知道你的操作系统在网络上是什么,你可以使用

    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";
   }

【讨论】:

    【解决方案4】:

    您可以使用此代码轻松确定颤动屏幕 我希望这会对某人有所帮助。提前感谢 这是我的代码

    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;
    

    【讨论】:

      猜你喜欢
      • 2021-06-01
      • 2015-11-05
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2010-10-04
      • 1970-01-01
      • 2015-10-31
      • 2012-08-18
      相关资源
      最近更新 更多