Flutter开发之——IOSView,瞬间高大上了
void main() {
runApp(PlatformViewDemo());
}
class PlatformViewDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
Widget? platformView() {
if (defaultTargetPlatform == TargetPlatform.android) {
return AndroidView(
viewType: ‘plugins.flutter.io/custom_platform_view’,
);
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
return UiKitView(
viewType: ‘plugins.flutter.io/custom_platform_view’,
);
}
}
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(“Platform”),
),
body: Center(
child: platformView(),
),
),
);
}
}

3.6 效果图

四 Flutter中嵌入IOS自定义View时设置初始化参数
4.1 Flutter 端修改如下
UiKitView(
viewType: ‘plugins.flutter.io/custom_platform_view’,
creationParams: {‘text’: ‘Flutter传给IOSTextView的参数’},
creationParamsCodec: StandardMessageCodec(),
)
-
creationParams :传递的参数,插件可以将此参数传递给 AndroidView 的构造函数。
-
creationParamsCodec :将 creationParams 编码后再发送给平台侧,它应该与传递给构造函数的编解码器匹配。值的范围
-
StandardMessageCodec
-
JSONMessageCodec
-
StringCodec
-
BinaryCodec
4.2 IOS自定义View接受参数
import Foundation
import Flutter
class MyFlutterView: NSObject,FlutterPlatformView {
let label = UILabel()
init(_ frame: CGRect,viewID: Int64,args :Any?,messenger :FlutterBinaryMessenger) {
super.init()
if(args is NSDictionary){
let dict = args as! NSDictionary
label.text = dict.value(forKey: “text”) as! String
}
}
func view() -> UIView {
return label
}
}
4.3 效果图

5.1 Flutter 端,创建 MethodChannel 用于通信(Intellij端修改)
import ‘package:flutter/foundation.dart’;
import ‘package:flutter/material.dart’;
import ‘package:flutter/services.dart’;
void main() {
runApp(PlatformViewDemo());
}
class PlatformViewDemo extends StatefulWidget {
@override
_PlatformViewDemoState createState() => _PlatformViewDemoState();
}
class _PlatformViewDemoState extends State {
static const platform =
const MethodChannel(‘com.example.flutterios.MyFlutterView’);
@override
Widget build(BuildContext context) {
Widget? platformView() {
if (defaultTargetPlatform == TargetPlatform.android) {
return AndroidView(
viewType: ‘plugins.flutter.io/custom_platform_view’,
creationParams: {‘text’: ‘Flutter传给AndroidTextView的参数’},
creationParamsCodec: StandardMessageCodec(),
);
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
return UiKitView(
viewType: ‘plugins.flutter.io/custom_platform_view’,
creationParams: {‘text’: ‘Flutter传给IOSTextView的参数’},
creationParamsCodec: StandardMessageCodec(),
);
}
}
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: Column(children: [
RaisedButton(
child: Text(‘传递参数给原生View’),
onPressed: () {
platform.invokeMethod(‘setText’, {‘name’: ‘张三’, ‘age’: 18});
},
),
Expanded(child: Center(child: platformView())),
]),
),
);
}
}
说明:
- com.example.flutterios.MyFlutterView:是IOS端端自定义View路径,与IOS端MethodChannel端保持一致
5.2 IOS原生View 中也创建一个 MethodChannel 用于通信(Xcode)
import Foundation
import Flutter
class MyFlutterView: NSObject,FlutterPlatformView {
let label = UILabel()
ini
t(_ frame: CGRect,viewID: Int64,args :Any?,messenger :FlutterBinaryMessenger) {
super.init()
if(args is NSDictionary){
let dict = args as! NSDictionary
label.text = dict.value(forKey: “text”) as! String
}
let methodChannel = FlutterMethodChannel(name: “com.example.flutterios.MyFlutterView”, binaryMessenger: messenger)
methodChannel.setMethodCallHandler { (call, result) in
if (call.method == “setText”) {
if let dict = call.arguments as? Dictionary<String, Any> {
let name:String = dict[“name”] as? String ?? “”
let age:Int = dict[“age”] as? Int ?? -1
self.label.text = “hello,(name),年龄:(age)”
}
}
}
}
func view() -> UIView {
return label
}
methodChannel.setMethodCallHandler { (call, result) in
if (call.method == “setText”) {
if let dict = call.arguments as? Dictionary<String, Any> {
let name:String = dict[“name”] as? String ?? “”
let age:Int = dict[“age”] as? Int ?? -1
self.label.text = “hello,(name),年龄:(age)”
}
}
}
}
func view() -> UIView {
return label
}
更多推荐


所有评论(0)