I have an app I'm wanting to try out in Flutter but one major thing may hold me back. Our app will need to access and edit the layout in code depending on what comes from saved data. A user can choose to leave something disabled, and those settings will be saved in a Realm.
Is this something that's possible with Flutter? I know it works with native code, but haven't been able to find anything yet indicating that native code can access widgets build in Flutter.
I see that Realm has a native Android and iOS plugin. With Flutter, you can use a MethodChannel to execute native code. So you could create a Realm wrapper for Flutter using native code.
In short: Yes it is possible.
This is a useful link if you end up creating a wrapper: https://flutter.io/platform-channels/
Related
I'm trying to add native android code to an already existing Flutter app, that does something quite different.
So I'd just like to have all of that functionality on top of my current app.
The native android app is using Bluetooth to connect to an external device and is collecting data periodically.
I know, that there is the possibility to call native code via platform channels, but it seemed to me, that you'd have to call that code every time you want to use it? Is this a reasonable way to implement the app or should it be rewritten in Dart? What's the best practice here?
Hope you can help me, thanks a lot!
The purpose of platform channels is to call platform-specific APIs.
...you'd have to call that code every time you want to use it...
Correct.
...Is this a reasonable way to implement the app or should it be rewritten in dart? What's the best practice here...
Check the source code of any package that relies on platform channels (for example camera, file picker, etc.) and read the official documentation. As a short summary, platform-specific code is written in Kotlin/Java (Android), Swift/Objective-C (iOS) or C/C++. Dart is used for building the UI and communicating with said code via platform channels and messages.
I am working on a project where an old library is being used in Android natively. Because of this, I am forced to work in Kotlin and use a xml file in order to view some elements on the screen. However, I am questioning myself if there is a way to create a custom activity/fragment in Android and call a flutter screen file where I can use all the Flutter widgets within the app. I am currently using method channels in order to communicate between Kotlin and Dart and this is why I came up with this question. Any help is very much appreciated!
It is possible to launch an Android Activity in which Flutter engine will be able to display UI and perform any actions you want within a native Android application.
To do so, you need to create a Flutter module, here is the documentation.
I'm learning flutter right now and I want develop a app that use the smartphone camera, but I'm not finding anything in the flutter docs that help me with native resources of the smartphone. I basically want develop a app that user can use to cut a image and save it.
Can anyone help me with this?
With flutter you use plugins to access native API's.
You can either create your own plugin using the following documentation
https://flutter.dev/docs/development/packages-and-plugins/developing-packages
or run an existing one from www.pub.dev like https://pub.dev/packages/image_picker
The problem
My idea is to create some simple components using Flutter so that they can be used in multiple native iOS and Android projects in our firm.
For example, consider a generic login view. This view can be coded once in Flutter and then can be included later on into projects that have both iOS and Android parts(native) . So such common components can be written once using Flutter instead of twice as native iOS and Android components.
What I have tried so far
I have followed the steps here https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps and it works, but it seems very restricted in functionality.
My questions are
How can I add multiple flutter projects like this, with each project representing a reusable component? Is that even the way to go?
Can I have smaller components- for example in iOS, UIViews instead of FlutterViewController without native hacking ?
How do I handle passing data between the native and Flutter part and also the navigation?
What do you mean by "it seems very restricted in functionality" ?
https://flutter.dev/docs/development/add-to-app lists these limitations:
Running multiple Flutter instances or running in partial screen views may have undefined behavior.
Using Flutter in background mode is still a WIP.
Packing a Flutter library into another sharable library or packing multiple Flutter libraries into an application isn’t supported.
Your 1st question is answered by the 3rd limitation. If you were considering splitting your simple components up into a separate Flutter library each, then no. You would need to package all your components into a single Flutter library, then let the native app pick from the pool.
For your 2nd question, can you be more specific what you mean by "without native hacking" ? Is there a reason you don't/can't use FlutterViewController?
3rd question about passing data between native and Flutter will involve https://flutter.dev/docs/development/platform-integration/platform-channels
From Flutter to native, here's an Android example https://stackoverflow.com/a/49308193/6668797, passing the value "text":
// flutter side, send the data
final String result = await platform.invokeMethod('getBatteryLevel', {"text":text});
// native side, retrieve it
String text = call.argument("text");
From native to Flutter, you use result.success(batteryLevel); to send it back to final String result.
I wrote my app using native method, currently I want to use reactive native in our further development to avoid writing two copies of code for iOS and Android.
The problem is we can't completely rewrite all the code, we want to replace the project piece by piece.
So I am wondering if I can write some views of my project using reactive native and others remain native code that we have used a long time.
If this is possible, is there any existing tutorial about how to do this?
You can keep your originally written native code via native modules. You can port your current project pieces by pieces to native modules, then let React-Native have access to it.
I guess best option is, start a new React-Native project and make it access your native modules, you won't have to rewrite everything then.
Here is more info for Android and iOS
There is official documentation to this. Checkout:
https://facebook.github.io/react-native/docs/integration-with-existing-apps.html