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
Related
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
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
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
I would like to show the native date picker depending on the device. For example UIDatePicker if it's iOS.
I've tried to add SelectDateTextField inside my widget and it works, but it looks like the Android one. Does anyone have any recommendations?
You should check the Platform then need to write specific widgets as per your need.
Use the APIs provided by the Dart.
Example:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
Your Widget
Platform.isIOS ? CupertinoWidget() : MaterialWidget()
I tried to integrate the webview, in the flutter app. After adding the code in the example, the page loads normally, but when I click on the input box in the page, no soft keyboard pops up,
This problem only happens on Android and it is a known issue here. https://github.com/flutter/flutter/issues/19718.
If your app does not need the webview to be on the same screen with other Flutter widgets, I recommend this webview library from the Flutter community. There is no keyboard issue here. https://pub.dartlang.org/packages/flutter_webview_plugin
I have tried multiple things but finally i have fixed it through scaffold, now my keyboard is opening.
just wrap your WebView inside Scaffold widget and apply following property inside scaffold.
resizeToAvoidBottomInset: false,
I know it's late but changing the webview version to v3.0.0 actually solved the issue for me.
This one can use for now. Hope flutter team fix it soon.
You need to open your android project in Android Studio to view all dependencies and in the webview_flutter
To fix most recent version of code, besides import
import android.app.Activity;
import io.flutter.app.FlutterApplication;
Change:
webView = new InputAwareWebView(context, containerView);
To:
Context activityContext = context;
Context appContext = context.getApplicationContext();
if (appContext instanceof FlutterApplication) {
Activity currentActivity = ((FlutterApplication) appContext).getCurrentActivity();
if (currentActivity != null) {
activityContext = currentActivity;
}
}
webView = new InputAwareWebView(activityContext, containerView);
Original answer here: #ryanhz https://github.com/flutter/flutter/issues/25767#issuecomment-588603862