Transfer Android Implementation to Flutter Application
You could try doing this using a PlatformView
in flutter.
You can convert your android/ios features into their respective own separate views/controllers and add the view to the flutter widget tree using the AndroidView
(Android) or UiKitView
(iOS).
Platform views allow to embed native views in a Flutter app, so you can apply transforms, clips, and opacity to the native view from Dart. This allows you, for example, to use the native Google Maps from the Android and iOS SDKs directly inside your Flutter app, by using Platform Views.
You can refer to the official docs for PlatformView
here
Following is an example with a simple android view:
Flutter:
Widget build(BuildContext context) { // This is used in the platform side to register the view. final String viewType = "NativeView"; // Pass parameters to the platform side. final Map<String, dynamic> creationParams = <String, dynamic>{}; return AndroidView( viewType: viewType, layoutDirection: TextDirection.ltr, creationParams: creationParams, creationParamsCodec: const StandardMessageCodec(), );}
Android:Create the native View class on android and implement the PlatformView
from the android flutter plugin
class NativeView implements PlatformView { @NonNull private final TextView textView; NativeView(@NonNull Context context, int id, @Nullable Map<String, Object> creationParams) { textView = new TextView(context); textView.setTextSize(72); textView.setBackgroundColor(Color.rgb(255, 255, 255)); textView.setText("Rendered on a native Android view (id: " + id + ")"); } @NonNull @Override public View getView() { return textView; } @Override public void dispose() {}}
Create a factory class for the above native view class by implementing the PlatformViewFactory
class NativeViewFactory extends PlatformViewFactory { @NonNull private final BinaryMessenger messenger; @NonNull private final View containerView; NativeViewFactory(@NonNull BinaryMessenger messenger, @NonNull View containerView) { super(StandardMessageCodec.INSTANCE); this.messenger = messenger; this.containerView = containerView; } @NonNull @Override public PlatformView create(@NonNull Context context, int id, @Nullable Object args) { final Map<String, Object> creationParams = (Map<String, Object>) args; return new NativeView(context, id, creationParams); }}
And finally just register the PlatformView
in your FlutterActivity
class:
public class MainActivity extends FlutterActivity { @Override public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { flutterEngine .getPlatformViewsController() .getRegistry() .registerViewFactory("NativeView", new NativeViewFactory()); }}