ToB企服应用市场:ToB评测及商务社交产业平台

标题: Flutter开发之——IOSView [打印本页]

作者: 铁佛    时间: 2024-8-25 06:09
标题: Flutter开发之——IOSView
init(messenger:FlutterBinaryMessenger) {
self.messenger = messenger
super.init()
}
func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
return MyFlutterView(frame,viewID: viewId,args: args,messenger: messenger)
}
func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
return FlutterStandardMessageCodec.sharedInstance()
}
}
3.4 在 AppDelegate 中注册

import UIKit
import Flutter
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
let registrar:FlutterPluginRegistrar = self.registrar(forPlugin: “plugins.flutter.io/custom_platform_view_plugin”)!
let factory = MyFlutterViewFactory(messenger: registrar.messenger())
registrar.register(factory, withId: “plugins.flutter.io/custom_platform_view”)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
说明:

3.5 编辑Flutter页面

在Intellij页面中编辑main.dart
import ‘package:flutter/foundation.dart’;
import ‘package:flutter/material.dart’;
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(),
)

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 结果图


五 Flutter 向 iOS View 发送消息

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())),
]),
),
);
}
}
说明:

5.2 IOS原生View 中也创建一个 MethodChannel 用于通讯(Xcode)

import Foundation
import Flutter
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里不停到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己探索发展或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学结果低效又漫长,而且极易碰到天花板技能停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初志也很简单,就是盼望可以或许帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。





既有适合小白学习的零基础资料,也有适合3年以上履历的小伙伴深入学习提升的进阶课程,根本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比力大,这里只是将部门目录截图出来,每个节点内里都包罗大厂面经、学习条记、源码讲义、实战项目、讲授视频,而且会连续更新!
假如你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
结语




《互联网大厂口试真题剖析、进阶开发焦点学习条记、全套讲授视频、实战项目源码讲义》点击传送门即可获取!
则是移动体系了,移动体系中又是以Android占比绝大部门为前提,所以AndroidNDK技能已经是我们必备技能了。

[外链图片转存中…(img-VuWLwmMJ-1713317913432)]
[外链图片转存中…(img-6n5hBxoF-1713317913433)]
《互联网大厂口试真题剖析、进阶开发焦点学习条记、全套讲授视频、实战项目源码讲义》点击传送门即可获取!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4