duduf
duduf

Reputation: 157

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 ? 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

Answers (2)

osaxma
osaxma

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:

  • Use kIsWeb to detect whether you're on web or not
  • Use defaultTargetPlatform 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);

Upvotes: 16

SetNameHere
SetNameHere

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

Related Questions