How can I disable some functionality only if web on Flutter app? - flutter

I see that up today flutter has a beta web support. But if I have some view or in general some functionality that support only mobile devices, how can disable it only when I build for the web? is it possible ?

import 'package:flutter/foundation.dart';
if (kIsWeb) {
// running on the web!
} else {
// NOT running on the web! You can check for additional platforms here.
}
Also if you want to check for inside widget and not show mobile widget
child: kIsWeb ? Container():<Mobile Widget>
https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html

Related

How should I make some differences between Flutter mobile and web version of my app?

If I want to use one codebase for my application, but add some specific widgets/features for mobile app and some for web version of my app, how should I do that? Should I write all the code and just add an if statement before those specific widgets to see what platform the application is running on by the user and show/hide that widget/feature, or I must separate mobile and web widgets in different files?
For example I want to show a camera feature on mobile version and another feature on web version. How should I do that?
You can check de platform of the device, and then display or not your content.
You can do this by few ways, the easyes one is:
import 'dart:io' show Platform;
if (Platform.isAndroid) {
// Android-specific code
} else if (Platform.isIOS) {
// iOS-specific code
}
The options are:
Platform.isAndroid
Platform.isFuchsia
Platform.isIOS
Platform.isLinux
Platform.isMacOS
Platform.isWindows
Specific for web:
import 'package:flutter/foundation.dart' show kIsWeb;
if (kIsWeb) {
// web
} else {
// not web
}
Or you can also calculate the dimensions of the screen using MediaQuery.
To show or not the feature you can use the Widget Visibility
https://api.flutter.dev/flutter/widgets/Visibility-class.html
You can check your app is run on the web like that
import 'package:flutter/foundation.dart' show kIsWeb;
if (kIsWeb) {
// It's running on the web!
} else {
// NOT running on the web!
// You can also check for additional platforms here.
// Or you only develop for web and mobile this is mobile
}
Read Documentation from this Link for more understanding : https://api.flutter.dev/flutter/foundation/kIsWeb-constant.html

Flutter Platform Specific Navigation Approaches

I've just looking at Flutter for the first time and have a question around navigation. I have a single project targeting iOS, Android and Web.
I'm looking at Navigation Drawers here:
https://material.io/components/navigation-drawer/flutter#using-a-navigation-drawer
Is it possible to use a Standard Drawer if the target is Web and a Modal if targeting mobile?
I can't quite find anything in the docs around varying things on different platforms or idioms.
You could design custom drawers for each platform and show the specific one by checking the user's platform using the kIsWeb constant.
For example:
import 'package:flutter/foundation.dart' show kIsWeb;
if (kIsWeb) {
// how the web version drawer widget
} else {
// show the non-web version drawer widget
}
Reference taken from https://stackoverflow.com/a/50744481/5882307

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.
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);
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.dart
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.

How to split Cupertino/Material in Flutter app depending on app platfrom?

How to create (and is it a good practice) a Flutter app which shows Cupertino UI widgets at iOS and Material UI widgets at Android with same functionality?
Import import 'dart:io'
And then in your widget make if statement
if(Platform.isIOS){
...
} else {
...
}
I found Flutter Platform Widgets package, works fine

Take user to device settings to active gps in flutter

I have a problem with gps management in a flutter application.
I've seen in this answer:
Is GPS activated - Flutter
how I can check if a gps is active in a flutter app.
If is not active (GeolocationStatus.denied) how can I take with flutter a user to device settings for turn on gps?
Thank you.
Install plugin :
https://pub.dartlang.org/packages/android_intent#-installing-tab-
Import in your dart :
import 'package:android_intent/android_intent.dart';
Add method :
void openLocationSetting() async {
final AndroidIntent intent = new AndroidIntent(
action: 'android.settings.LOCATION_SOURCE_SETTINGS',
);
await intent.launch();
}
Invoke it done...