简体   繁体   English

如何知道我的 Flutter Web 应用程序是在移动设备还是桌面设备上运行

[英]How to know if my flutter web app is running on mobile or desktop

How do I know if my web flutter application is running on desktop or on mobile ?我如何知道我的 web flutter 应用程序是在桌面还是移动设备上运行? I can't use kIsWeb because it says if the app is built for the web not if it's running on the web.我不能使用 kIsWeb,因为它说应用程序是否是为 Web 构建的,而不是它是否在 Web 上运行。 Thank you.谢谢你。

In order to detect if Flutter Web is running on a Mobile device (iOS or Android only), you can detect that in two steps:为了检测 Flutter Web 是否在移动设备(仅限 iOS 或 Android)上运行,您可以通过两个步骤进行检测:

  • Use kIsWeb to detect whether you're on web or not使用kIsWeb检测您是否在网络上
  • Use defaultTargetPlatform to get the default platform (it'll give you the operating system)*使用defaultTargetPlatform获取默认平台(它将为您提供操作系统)*

Here's a test that I just did calling defaultTargetPlatform from Safari on iOS:这是我刚刚在 iOS 上从 Safari 调用defaultTargetPlatform的测试:

To use defaultTargetPlatform , you need to import import 'package:flutter/foundation.dart';要使用defaultTargetPlatform ,您需要导入import 'package:flutter/foundation.dart';

Example:例子:

import 'package:flutter/foundation.dart';
final isWebMobile = kIsWeb && (defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.android)

Maybe you could make a desicion based on the screensize.也许你可以根据屏幕尺寸做出决定。

var screenSize = MediaQuery.of(context).size;
final double width = screenSize.width;
final double height = screenSize.height - kToolbarHeight;

If height or width is below a defined value, you could estimate that the app is executed on a mobile device.如果高度或宽度低于定义的值,您可以估计该应用程序是在移动设备上执行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter Web 应用程序无法在移动浏览器上运行。 如果在移动浏览器上运行flutter代码,如何调试错误? - Flutter web app not working on mobile browser. How to debug the error if running flutter code on mobile browser? 如何清除 flutter 桌面应用程序数据,如 flutter 移动应用程序 - How to clear flutter desktop app data like flutter mobile app 如何在 Flutter Web App(移动端和桌面端)中提示“保存在主屏幕” - How to prompt "save in the home screen" in a Flutter Web App (both mobile and desktop) 如何将 Flutter 移动应用迁移到 Flutter Web - How to migrate flutter mobile app to flutter web 如何检查我的 Flutter Web 应用程序当前是否正在运行? - How to check if my Flutter Web app is currently running? 如何在 flutter web 中知道该用户使用 PWA 或桌面? - How to know in flutter web that user using PWA or desktop? 如何检测我的 Flutter 应用程序是否在 web 中运行? - How can I detect if my Flutter app is running in the web? 如何将 Flutter 移动应用程序转换为 Web 应用程序 - How to convert Flutter Mobile app to Web App 在 Flutter Web/桌面上运行 Java - Running Java on Flutter Web/Desktop 如何同时为移动和桌面制作 Flutter Firebase 应用程序 - How to do a Flutter Firebase app for both Mobile and Desktop at the same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM