Scott
Scott

Reputation: 371

Hosting native Android and iOS views in your Flutter app with Platform Views

I'm trying to follow the Flutter tutorial: https://flutter.dev/docs/development/platform-integration/platform-views, but the Java code has errors and doesn't compile. Specifically, NativeViewFactory is defined as having two parameters, but then is called with no parameters. What changes can be made to the code so that it compiles and displays a native Android view in Flutter?

In NativeViewFactory.java:

 @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;
  }

In MainActivity.java:

public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        flutterEngine
            .getPlatformViewsController()
            .getRegistry()
            .registerViewFactory("<platform-view-type>", new NativeViewFactory());
    }

Upvotes: 5

Views: 3985

Answers (1)

Scott
Scott

Reputation: 371

Here is working code to display a native Android Textview in Flutter.

Main.dart

import 'package:flutter/material.dart';


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) => MaterialApp( home: Native() );
}

class Native extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Expanded( child: AndroidView(viewType: "view1",) ),
        ],
      ),
    );
  }
}

MainActivity.java

package com.example.native_view_test;

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;

public class MainActivity extends FlutterActivity {
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        flutterEngine
                .getPlatformViewsController()
                .getRegistry()
                .registerViewFactory("view1", new NativeViewFactory());
    }
}

NativeViewFactory.java

package com.example.native_view_test;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Map;
import io.flutter.plugin.common.StandardMessageCodec;
import io.flutter.plugin.platform.PlatformView;
import io.flutter.plugin.platform.PlatformViewFactory;

class NativeViewFactory extends PlatformViewFactory {

    NativeViewFactory() {
        super(StandardMessageCodec.INSTANCE);
    }

    @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);
    }
}

NativeView.java

package com.example.native_view_test;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;
import java.util.Map;
import io.flutter.plugin.platform.PlatformView;

class NativeView implements PlatformView {
    private final TextView textView;

    NativeView(Context context, int id, Map<String, Object> creationParams) {
        textView = new TextView(context);
        textView.setTextSize(42);
        textView.setBackgroundColor(Color.rgb(255, 255, 255));
        textView.setText("Rendered on a native Android view (id: " + id + ")");
    }

    @Override
    public View getView() {
        return textView;
    }

    @Override
    public void dispose() {}
}

Upvotes: 6

Related Questions