Flutter iOS 开发
flutter iOS混合
flutter 调用iOS
await methodChannel.invokeMethod('iOSFlutter', '参数');
iOS调用flutter
FlutterViewController* flutterViewController = [[FlutterViewController alloc] initWithProject:nil nibName:nil bundle:nil];
flutterViewController.navigationItem.title = @”Demo”;
[flutterViewController setInitialRoute:@”home”];
// 要与main.dart中一致
NSString *channelName = @”com.pages.your/native_post”;
FlutterEventChannel *evenChannal = [FlutterEventChannel eventChannelWithName:channelName binaryMessenger:flutterViewController];
// 代理FlutterStreamHandler
[evenChannal setStreamHandler:self];
[self.navigationController pushViewController:flutterViewController animated:YES];
flutter android 开发
1)添加 FlutterActivity 到 AndroidManifest.xml 中:
myButton.setOnClickListener {
startActivity(
FlutterActivity
.withNewEngine()
.initialRoute(“/my_route”)
.build(this)
)
}
缓存 Flutter 引擎方式跳转:
class MyApplication : Application() {
lateinit var flutterEngine : FlutterEngine
override fun onCreate() {
super.onCreate()
// Instantiate a FlutterEngine.
flutterEngine = FlutterEngine(this)
// Configure an initial route.
flutterEngine.navigationChannel.setInitialRoute(“your/route/here”);
// Start executing Dart code to pre-warm the FlutterEngine.
flutterEngine.dartExecutor.executeDartEntrypoint(
DartExecutor.DartEntrypoint.createDefault()
)
// Cache the FlutterEngine to be used by FlutterActivity or FlutterFragment.
FlutterEngineCache
.getInstance()
.put(“my_engine_id”, flutterEngine)
}
}
myButton.setOnClickListener {
startActivity(
FlutterActivity
.withCachedEngine(“my_engine_id”)
.build(this)
)
}
Dart和C,C++混合编程
C代码
#include<windows.h>
int add(int a, int b){
return a + b;
}
void showBox(){
MessageBox(NULL,”Hello Dart”,”Title”,MB_OK);
}
import ‘dart:ffi’ as ffi;
import ‘dart:io’ show Platform;
/// 根据C中的函数来定义方法签名(所谓方法签名,就是对一个方法或函数的描述,包括返回值类型,形参类型)
/// 这里需要定义两个方法签名,一个是C语言中的,一个是转换为Dart之后的
typedef NativeAddSign = ffi.Int32 Function(ffi.Int32,ffi.Int32);
typedef DartAddSign = int Function(int, int);
/// showBox函数方法签名
typedef NativeShowSign = ffi.Void Function();
typedef DartShowSign = void Function();
void main(List
if (Platform.isWindows) {
// 加载dll动态库
ffi.DynamicLibrary dl = ffi.DynamicLibrary.open(“../src/test.dll”);
// lookupFunction有两个作用,1、去动态库中查找指定的函数;2、将Native类型的C函数转化为Dart的Function类型
var add = dl.lookupFunction<NativeAddSign, DartAddSign>("add");
var showBox = dl.lookupFunction<NativeShowSign, DartShowSign>("showBox");
// 调用add函数
print(add(8, 9));
// 调用showBox函数
showBox();
}
}
检测手段
1、Flutter Inspector
帮你找到 App 中频繁重绘导致性能消耗过大的部分。页面的进度条动画刷新时会导致整个布局频繁重绘。
2、性能图层
蓝色垂直的线条表示已执行的正常帧,绿色的线条代表的是当前帧,如果其中有一帧处理时间过长,就会导致界面卡顿,图表中就会展示出一个红色竖条。
3、Raster 线程问题4、UI 线程问题定位5、检查多视图叠加的视图渲染开关 checkerboardOffscreenLayers6、检查缓存的图像开关 checkerboardRasterCacheImages
二、关键优化指标
1、页面异常率2、页面帧率3、页面加载时长
三、布局加载优化 1、常规优化2、深入优化
四、启动速度优化 1、引擎预加载2、Dart VM 预热
五、内存优化 1、const 实例化2、识别出消耗多余内存的图片3、针对 ListView item 中有 image 的情况来优化内存六、包体积优化 1、图片优化2、移除冗余的二三库3、启用代码缩减和资源缩减4、构建单 ABI 架构的包
flutter自定义组件:
1.组合其它组件(最简单,优先考虑)
2.自绘 可以通过Flutter中提供的CustomPaint和Canvas来实现UI自绘。
3.实现RenderObject。是一个抽象类,它定义了一个抽象方法paint(…):
现在地图发展趋势:
1.由于移动端跨平台的兴起,地图SDK更偏向于兼容发展
目前高德地图SDK兼容了很多平台如:android、iOS、HarmonyOS、小程序、web、js、Flutter、等等
另一个地图领域在向3D方向靠拢,高德也支持三维模型gltf加载,比较大地图公司的话,cesium.js、three.js在三维方面有比较大的发展方向。但是三维场景的使用范围和能力还是在探索阶段。
flutter高质量博客
https://github.com/boyan01
https://github.com/lijy91
Flutter是如何实现在桌面端和移动端设备的设计的?
答案是flutter是通过两套代码实现的,虽然在一个工程里面,但是桌面和移动端的UI是不一致的,一套代码是无法实现设计的
final platform = ref.watch(debugNavigatorPlatformProvider);
switch (platform) {
case NavigationPlatform.desktop:
home = const HomeWindow();
break;
case NavigationPlatform.mobile:
home = const MobileWindow();
break;
}
目前查看了一个仿网易的app,同时支持桌面端和移动端,目前看结果是这样。
Flutter存储数据方式
shared_preferences
sqflite
Hive是一个快速,轻量级的NoSQL数据库,适用于Flutter和Dart应用程序。 如果您想要一个简单的键值数据库,没有很多关系,并且确实易于使用,那么Hive真的很有用。
Bloc 是状态管理方案,状态,意味着 APP 一旦关闭,其状态就会丢失。但应用使用期间,其状态是可以实时更新、跨页面、跨组件同步更新的。
Flutter 状态管理
1.setState
2.scoped_model源自 Fuchsia 代码,使用 InheritedWidget 实现
3.provide
4.RxDart
5.Fish-Redux
path_provider方法和说明
方法 属性 描述
getTemporaryDirectory() Future
getApplicationSupportDirectory() Future
getLibraryDirectory() Future
getApplicationDocumentsDirectory() Future
getExternalStorageDirectory() Future
getExternalCacheDirectories() Future<List
getExternalStorageDirectories() Future<List
getDownloadsDirectory() Future<Directory?> 桌面程序下载目录
flutter_hooks
https://www.jianshu.com/p/3b59763d105d
MethodChannel 用于 Flutter 与 原生平台之间函数的互相调用
BasicMessageChannel 它传递的是字节数组,使用时自定义编解码器
EventChannel 用于 Flutter 与 原生平台之间事件的通信
flutter端调用移动端
channel.invokeMethod
iOS端
channel.setMethodCallHandler { (call, res) in
android端
channel.setMethodCallHandler { call, res ->
原生端调用flutter
iOS、Android
channel.invokeMethod(“your_method_name”, params)
flutter
channel.setMethodCallHandler((call) {