色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Flutter中嵌入Android 原生TextView實(shí)例教程

瀏覽:32日期:2022-09-22 15:17:06

前言

本篇文章 中寫(xiě)到的是 flutter 調(diào)用了Android 原生的 TextView 案例

添加原生組件的流程基本上可以描述為:

1 android 端實(shí)現(xiàn)原生組件PlatformView提供原生view

2 android 端創(chuàng)建PlatformViewFactory用于生成PlatformView

3 android 端創(chuàng)建FlutterPlugin用于注冊(cè)原生組件

4 flutter 平臺(tái)嵌入 原生view

1 創(chuàng)建原生組件

創(chuàng)建在fLutter工程時(shí)會(huì)生成幾個(gè)文件夾,lib是放flutter工程代碼,android和ios文件夾分別是對(duì)應(yīng)的雙平臺(tái)的原生工程。

在這里直接打開(kāi)Android工程目錄,項(xiàng)目默認(rèn)生成了GeneratedPluginRegistrant和MainActivity兩個(gè)文件,GeneratedPluginRegistrant不要?jiǎng)樱珿eneratedPluginRegistrant是flutter中配制使用其他插件時(shí),程序在編譯時(shí)自動(dòng)進(jìn)行插件注冊(cè)使用的類(lèi)。

在MainActivity的包下新建自定義View,F(xiàn)lutter的原生View不能直接繼承自View,需要實(shí)現(xiàn)提供的PlatformView接口:

public class TestTextView implements PlatformView r{private final TextView mTestTextView;/** * * @param context * @param messenger * @param id * @param params 初始化時(shí) flutter 傳遞過(guò)來(lái)的參數(shù) */TestTextView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params) {//創(chuàng)建 TextViewTextView lTextView = new TextView(context);lTextView.setText('Android的原生TextView');this.mTestTextView = lTextView;//flutter 傳遞過(guò)來(lái)的參數(shù)if (params!=null&&params.containsKey('content')) {String myContent = (String) params.get('content');lTextView.setText(myContent);}}@Overridepublic View getView() {return mTestTextView;}@Overridepublic void dispose() {}}

2 創(chuàng)建PlatformViewFactory

import android.content.Context;import java.util.Map;import io.flutter.plugin.common.BinaryMessenger;import io.flutter.plugin.common.StandardMessageCodec;import io.flutter.plugin.platform.PlatformView;import io.flutter.plugin.platform.PlatformViewFactory;public class TestViewFactory extends PlatformViewFactory {private final BinaryMessenger messenger;public TestViewFactory(BinaryMessenger messenger) {super(StandardMessageCodec.INSTANCE);this.messenger = messenger;}/** * * @param context * @param id * @param args args是由Flutter傳過(guò)來(lái)的自定義參數(shù) * @return */@SuppressWarnings('unchecked')@Overridepublic PlatformView create(Context context, int id, Object args) {//flutter 傳遞過(guò)來(lái)的參數(shù)Map<String, Object> params = (Map<String, Object>) args;//創(chuàng)建 TestTextViewreturn new TestTextView(context, messenger, id, params);}

3 創(chuàng)建Plugin并在ManActivity中注冊(cè)插件

/** * flutter 調(diào)用 android 原生view * */public class TestFluttertoAndroidTextViewPlugin {public static void registerWith(PluginRegistry registry) {//防止多次注冊(cè)final String key = TestFluttertoAndroidTextViewPlugin.class.getCanonicalName();if (registry.hasPlugin(key)) return;//初始化 PluginRegistryPluginRegistry.Registrar registrar = registry.registrarFor(key);//設(shè)置標(biāo)識(shí)registrar.platformViewRegistry().registerViewFactory('com.flutter_to_native_test_textview', new TestViewFactory(registrar.messenger()));}}

MainActivity 中注冊(cè)

import android.os.Bundleimport io.flutter.app.FlutterActivityimport io.flutter.plugins.FlutterToAndroidPluginsimport io.flutter.plugins.GeneratedPluginRegistrantclass MainActivity: FlutterActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) //flutter 項(xiàng)目工程中默認(rèn)生成的 GeneratedPluginRegistrant.registerWith(this) //這是我們新創(chuàng)建的插件 TestFluttertoAndroidTextViewPlugin.registerWith(this) } override fun onDestroy() { super.onDestroy() }}

4 flutter頁(yè)面中嵌入android 原生Textview

4.1 最簡(jiǎn)單的調(diào)用

Flutter中嵌入Android 原生TextView實(shí)例教程

//這里設(shè)置的 viewType值與 android 中插件注冊(cè)的標(biāo)識(shí) 一至//registrar.platformViewRegistry().registerViewFactory('com.flutter_to_native_test_textview', new TestViewFactory(registrar.messenger()));mTextWidget = Container( height: 200, child: AndroidView( //設(shè)置標(biāo)識(shí) viewType: 'com.flutter_to_native_test_textview', ), );@override Widget build(BuildContext context) { // TODO: implement build return Scaffold( appBar: appBar, //顯示的頁(yè)面 body: mTextWidget, ); }

4.2 flutter 調(diào)用 原生view并傳參數(shù)

Flutter中嵌入Android 原生TextView實(shí)例教程

mTextWidget = Container( height: 200, child: AndroidView( //標(biāo)識(shí) viewType: 'com.flutter_to_native_test_textview', creationParams: { 'content': 'flutter 傳入的文本內(nèi)容', }, //參數(shù)的編碼方式 creationParamsCodec: const StandardMessageCodec(), ), );

android 原生中的接收(只會(huì)接收一次)

... ...TestTextView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params) {... ..//flutter 傳遞過(guò)來(lái)的參數(shù)if (params!=null&&!params.isEmpty()&&params.containsKey('content')) {String myContent = (String) params.get('content');lTextView.setText(myContent);}... ...}

4.3 flutter 更新 原生view 中的數(shù)據(jù)

原生組件初始化的參數(shù)并不會(huì)隨著setState重復(fù)賦值,可以通過(guò)MethodCall來(lái)實(shí)現(xiàn)更新數(shù)據(jù)。

首先讓原生view組件實(shí)現(xiàn)MethodCallHandler接口:

public class TestTextView implements PlatformView , MethodChannel.MethodCallHandler{private final TextView mTestTextView;TestTextView(Context context, BinaryMessenger messenger, int id, Map<String, Object> params) {... ...//com.flutter_to_native_test_view_ 是更新數(shù)據(jù)的通信標(biāo)識(shí)MethodChannel methodChannel = new MethodChannel(messenger, 'com.flutter_to_native_test_textview_' + id);methodChannel.setMethodCallHandler(this);}... ...@Overridepublic void onMethodCall(MethodCall methodCall, MethodChannel.Result result) { //updateText 是flutter 中調(diào)用的方法名稱(chēng),可以隨意定義if ('updateText'.equals(methodCall.method)) {String text = (String) methodCall.arguments;this.mTestTextView .setText(text);//對(duì)flutter 的回調(diào)result.success(null);}}}

flutter 中調(diào)用 android 原生view

MethodChannel _channel; int viewId=0;

mTextWidget = Container( height: 200, child: AndroidView( //標(biāo)識(shí) viewType: 'com.flutter_to_native_test_textview', creationParams: { 'content': 'flutter 傳入的文本內(nèi)容', }, //參數(shù)的編碼方式 creationParamsCodec: const StandardMessageCodec(), //view創(chuàng)建完成時(shí)的回調(diào) onPlatformViewCreated: (id) { viewId = id; }, ), );

更新數(shù)據(jù)

//這里設(shè)置的標(biāo)識(shí) MethodChannel(’com.flutter_to_native_test_textview_$viewId’);// 與android MethodChannel methodChannel = new MethodChannel(messenger, 'com.flutter_to_native_test_textview_' + id); 中注冊(cè)的一至void clickUpdtae(){_channel = new MethodChannel(’com.flutter_to_native_test_textview_$viewId’); updateTextView();}//這里的標(biāo)識(shí) updateText//與android 中接收消息的方法中//if ('updateText'.equals(methodCall.method)) {...} 一至void updateTextView() async { return _channel.invokeMethod(’updateText’, '更新內(nèi)容'); }

通過(guò)onPlatformViewCreated回調(diào),監(jiān)聽(tīng)原始組件成功創(chuàng)建,并能夠在回調(diào)方法的參數(shù)中拿到當(dāng)前組件的id,這個(gè)id是系統(tǒng)隨機(jī)分配的,然后通過(guò)這個(gè)分配的id加上我們的組件名稱(chēng)最為前綴創(chuàng)建一個(gè)和組件通訊的MethodChannel,拿到channel對(duì)象之后就可以通過(guò)invokeMethod方法向原生組件發(fā)送消息了,這里這里調(diào)用的是‘updateText’這個(gè)方法,參數(shù)是一個(gè)String

總結(jié)

到此這篇關(guān)于Flutter中嵌入Android 原生TextView實(shí)例教程的文章就介紹到這了,更多相關(guān)Flutter嵌入Android 原生TextView內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 国内精品久久久久影院网站 | 精品欧美成人高清视频在线观看 | 美女黄色在线观看 | 日韩成人精品日本亚洲 | 三级国产精品 | 久久久久久网址 | 成人18视频在线 | 全部孕妇毛片丰满孕妇孕交 | 午夜欧美日韩在线视频播放 | 国产精品久久久久久久福利院 | 97青娱国产盛宴精品视频 | 亚洲成a人不卡在线观看 | 女人张开腿让男人 | 欧美日韩高清不卡免费观看 | 成人午夜在线播放 | 91网站国产 | 精品国产亚洲一区二区三区 | 一级片免 | 亚洲免费观看视频 | 正在播放国产精品 | 久久久久久久久久久久久久久久久 | 日韩中文字幕在线观看 | 亚洲最大福利视频 | 久久一本一区二区三区 | 日韩色视频一区二区三区亚洲 | 亚洲免费视频一区二区三区 | 91香蕉国产在线观看免费永久 | 99精品小视频 | 97精品国产综合久久久久久欧美 | 精品久久成人 | 久久久久久久国产精品视频 | 国产dvd毛片在线视频 | 99久久国产免费福利 | 97免费视频在线观看 | 超级香蕉97视频在线观看一区 | 久久视奸| 扒开双腿猛进入爽爽在线观看 | 精品国产高清a毛片无毒不卡 | 韩国一级淫片视频免费播放 | 成年免费大片黄在线观看一 | 露脸 在线 国产 眼镜 |