Reputation: 157
How do I know if my web flutter application is running on desktop or on mobile ? I can't use kIsWeb because it says if the app is built for the web not if it's running on the web. Thank you.
Upvotes: 6
Views: 3928
Reputation: 2994
In order to detect if Flutter Web is running on a Mobile device (iOS or Android only), you can detect that in two steps:
kIsWeb
to detect whether you're on web or notdefaultTargetPlatform
to get the default platform (it'll give you the operating system)*Here's a test that I just did calling defaultTargetPlatform
from Safari on iOS:
To use defaultTargetPlatform
, you need to import the following:
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. You can see the implementation here: https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/foundation/_platform_web.dartUpvotes: 16
Reputation: 121
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.
Upvotes: -4