【问题标题】:Hosting native Android and iOS views in your Flutter app with Platform Views使用 Platform Views 在 Flutter 应用中托管原生 Android 和 iOS 视图
【发布时间】:2021-03-20 06:55:00
【问题描述】:

我正在尝试遵循 Flutter 教程:https://flutter.dev/docs/development/platform-integration/platform-views,但 Java 代码有错误并且无法编译。具体来说,NativeViewFactory 被定义为有两个参数,但随后调用时不带参数。可以对代码进行哪些更改,以便在 Flutter 中编译和显示原生 Android 视图?

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

在 MainActivity.java 中:

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

【问题讨论】:

    标签: android flutter


    【解决方案1】:

    这是在 Flutter 中显示原生 Android Textview 的工作代码。

    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() {}
    }
    
    

    【讨论】:

    • 这对我有用,我只需要更改文本 BackgroundColor。因为flutter默认背景是白色,android textview默认颜色是白色。所以最初你可能看不到文本,但它正在呈现。
    • 这会占据 Flutter 应用的整个屏幕。有没有办法以固定大小(如 textView.height = 80)使用它,就像 Flutter 的普通 Text Widget 一样?
    猜你喜欢
    • 2018-10-05
    • 2019-05-01
    • 2019-06-25
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 2020-03-03
    • 2021-04-02
    • 2019-05-08
    相关资源
    最近更新 更多