[英]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)上运行,您可以通过两个步骤进行检测:
kIsWeb
to detect whether you're on web or not使用kIsWeb
检测您是否在网络上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)
defaultTargetPlatform
is already web-aware and does the navigator shenanigans for you. defaultTargetPlatform
已经具有网络感知能力,并且会为您执行导航器恶作剧。 You can see the implementation here: https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/foundation/_platform_web.dart你可以在这里看到实现: https : //github.com/flutter/flutter/blob/master/packages/flutter/lib/src/foundation/_platform_web.dartMaybe 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.