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
Related
I have a flutter app on an android device, it shows the app with the android adaptive things such as the date picker, the app bar, the dialogs...
I want to try a version of the app like it's on IOS, I mean I want that date picker shows as the IOS's, also for the app bar.
I'm pretty sure that this is related to the MediaQuery, but I just don't know what is it.
I know that I can show forcefully the widgets from the Cupertino library, however, I don't want to do it, I want just to get how the app will show when it runs on IOS with the current code.
Thank you !
Wherever you're setting your theme, you can simply set the platform property on it to TargetPlatform.iOS.
This should allow the theming to use iOS-specific widgets rather than their android counterparts. Note that this may not work everywhere; if Dart:io's Platform were used incorrectly in your code or 3rd party code for theming, this would not be overridden.
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
Let's suppose i am debugging an already built app and i needed to see the widget/screen and how it looks(without running the application and going through all the different screens to reach the desired one) ,
i know there is an annotation (#preview) in jetpack compose for native android to preview the widget in question isolated from the whole app ,
in the meantime to preview a certain widget i put it in
void main() => runApp(ParticularScreen());
is there any other option ?
ps : i think there is a plugin for vs code called preview
https://marketplace.visualstudio.com/items?itemName=jamesblasco.flutter-preview
i am looking for exactly the same but with android studio
I'm trying to find the best way to organise a project that I'm gonna start with Flutter. Here is what I need to develop:
1 Android & iOS app for customers-side
1 Android & iOS app for professionals-side
1 Web app for administration
Backend will be done with Firebase (Firebase Authentication, Firestore, ...)
Some code (models and logic) and features will be common on the three apps (and it would be nice if I could configure a production and a development environment).
How would you set up your project(s) in order to easily do that?
Does it seems like a good idea to have only 1 project with multiple flavors? I like the idea that I don't have to deal with multiple projects. Just to keep the development flow very simple. And There could be a condition in the main() function of the App that checks the flavor then open the right screen.
Would you prefer to have a common library that is used by multiple project? This seems like a good way to do it. But I'm not sure that this (small) project worth 3 to take the time to organise 3 distinct projects + a library.
Any other idea?
Thank you very much in advance
You should be able to reshare the majority of your code without different projects.
This is the beauty of widgets and of MediaQuery.
Create all your main components in widgets, your list of items can be ItemList();, the main menu can be MainDrawer();, etc.
For widgets that are meant for differing screen sizes, in your layout builder you can either return LargeScreenWidget() or SmallScreenWidget().
Use either a LayoutBuilder or OrientationBuilder, and write the code to respond to changes in screen size, width and orientation. This way it can share most of the same code.
When you return your layout builder, follow the general pseudo code:
isLargeScreen
? return Row(children[LeftWidget(), RightWidget()])
: return SingleChildScrollView(child: MobileWidget());
If somebody has a very small Chrome window on Desktop, it can switch to the mobile layout this way.
Just as a tidbit, I almost always start my code with the following so I can adapt the layout.
var size = MediaQuery.of(context).size;
var isLargeScreen = false;
if (MediaQuery.of(context).size.width > 900) {
isLargeScreen = true;
} else {
isLargeScreen = false;
}
This way when I am building a widget I can do like the following:
Container(
constraints: BoxConstraints(maxWidth: (isLargeScreen ? 700 : size.width * 0.9)),
),
You can also ask the user which operating system they are using with Platform.isIOS/isWindows/isAndroid.
EX:
onTap: () {
Navigator.push(context, MaterialPageRoute(
builder: (context) {
return Platform.isIOS ? AndroidPage() : iOSPage();
},
)); // MaterialPageRoute
},
Or when using a FutureBuilder
return Platform.isIOS ? CupertinoLoading() : CircularProgressIndicator();
On top of this, you can use VoidCallbackMethod with the OrientationBuilder to change the way the app functions.
In the articles I share, if the screen is small, it opens a Navigation route, otherwise it passes the data to a widget on the right side. This is good for a messaging app for example.
Here are some articles to help you out.
Develop A Responsive Layout Of Mobile App With Flutter
Developing for Multiple Screen Sizes and Orientations in Flutter
Of course, ultimately it is up to your project, what you and/or your team desires, and how comfortable you are with file-size/extra if statements running all the time.
However I will add, that at least for the mobile apps, I usually only use one project. Web/desktop may be a different project.
Happy fluttering!
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