gpt4 book ai didi

android - 使用 Platform Views 在 Flutter 应用中托管原生 Android 和 iOS View

转载 作者:行者123 更新时间:2023-12-04 23:48:48 34 4
gpt4 key购买 nike

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

最佳答案

这是在 Flutter 中显示原生 Android Textview 的工作代码。
主要.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() {}
}

关于android - 使用 Platform Views 在 Flutter 应用中托管原生 Android 和 iOS View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65207626/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com