flutter 代码生成

获取 AST builder 中对象解析是基于 analyzer 模块实现的,对于 Element 对象未封装的元数据, 可以直接通过访问其 AST 获取。 在 LibraryElement 对象中提供了 session 属性用于访问解析库的会话 AnalysisSession 对象, 这里可以查询已解析的元素语法树。 // 初始化参数从 AST 中取 final libraryResult = library.session.getParsedLibraryByElement(library) as ParsedLibraryResult; final declarationResult = libraryResult.getElementDeclaration(field); if (declarationResult != null) { final variableDeclaration = declarationResult.node as VariableDeclaration; if (variableDeclaration.initializer != null) { builder.assignment = code.Code(variableDeclaration.initializer!.toSource()); } } 在 VSCode 中调试生成器 build 库通过生成 .dart_tool/build/entrypoint/build.dart 执行代码生成,调试这个 dart 文件就可以加载自己写的生成器。 在 .vscode 的 launch.json 文件中增加 { "name": "model_gen_example model_gen", "cwd": "packages/model_gen_example", "request": "launch", "program": "....

July 4, 2022 · 1 min · lyincc

Flutter 手势识别源码探究

引擎层 Flutter Engine 暴露了 FlutterEngineSendPointerEvent 接口供平台层调用。通过向该接口传递 FlutterPointerEvent 对象告知引擎当前指针状态。 FLUTTER_EXPORT FlutterEngineResult FlutterEngineSendPointerEvent( FLUTTER_API_SYMBOL(FlutterEngine) engine, const FlutterPointerEvent* events, size_t events_count); SDK 层 事件源 Flutter SDK 则通过 GestureBinding 类管理手势事件。GestureBinding 在初始化时在 window 对象中设置了 _handlePointerDataPacket 回调接收引擎层传回的指针状态。 @override void initInstances() { super.initInstances(); _instance = this; window.onPointerDataPacket = _handlePointerDataPacket; } 经过指针事件队列 _pendingPointerEvents 缓冲后,handlePointerEvent 将对事件进行重采样处理。之后在 _handlePointerEventImmediately 过程中进行实际事件分发。 命中测试 在 _handlePointerEventImmediately 过程中事件分发前需要先对 PointerDownEvent 事件构造 HitTestResult。 // @@ void _handlePointerEventImmediately(PointerEvent event) { if (event is PointerDownEvent || event is PointerSignalEvent || event is PointerHoverEvent) { assert(!...

January 28, 2021 · 3 min · lyincc

Flutter 引擎层

获取 源码位于 Github 仓库,但需要使用 gclient 组织工程 https://github.com/flutter/engine 部分依赖的 githook 脚本不支持 python3,需要强制 gclient 使用 python2 set GCLIENT_PY3=0 参考 https://github.com/flutter/flutter/wiki/Setting-up-the-Engine-development-environment 源码拉取后需要切换分支。需要对应 Flutter SDK 版本。在 %FLUTTER_SDK%\bin\internal\engine.version 中查看对应引擎版本。 拉取指定版本引擎配置: solutions = [ { "managed": False, "name": "src/flutter", "url": "https://github.com/flutter/engine.git@07c1eed46b9d9b58df78566e9b8b2e42e80d3380", "custom_deps": {}, "deps_file": "DEPS", "safesync_url": "", }, ] 执行 gclient sync 同步源码。 编译 参考 https://github.com/flutter/flutter/wiki/Compiling-the-engine#compiling-for-windows 切换到 src 目录,使用 .\flutter\tools\gn 脚本创建 ninja 工程。 编译 windowns-x64 release 版本 .\flutter\tools\gn --target-os=win --windows-cpu=x64 --runtime-mode=release 编译 debug 版本,可以使用 --unoptimized 关闭代码优化...

September 1, 2020 · 1 min · lyincc