使用過(guò)Dart自動(dòng)Json序列化的都用過(guò)自動(dòng)生成代碼俏橘,但是你知道如何自己自定義注解并生成代碼嗎允华?
1. 需要使用 source_gen、build_runner 這兩個(gè)庫(kù)寥掐;
2. 定義注解類例获,配置注解到自動(dòng)代碼的Generator類及Builder類;
綜上所述曹仗,咱需要構(gòu)建兩個(gè)Library庫(kù)來(lái)使用注解自動(dòng)化代碼生成榨汤。
Library1: 注解類定義
Library2: 注解類的代碼自動(dòng)生成Generator及Builder
下面我們就說(shuō)說(shuō)怎么來(lái)做吧!
首先怎茫,假如我們需要實(shí)現(xiàn)一個(gè)自動(dòng)生成MethodChannel調(diào)用Native方法的一個(gè)功能收壕,這里我們叫它NativeCall吧。
-
新建NativeCall庫(kù)轨蛤,編寫NativeCall注解的類蜜宪。
import 'package:native_call/native_method_call_info.dart'; class NativeCall { /// ChannelName final String channelName; const NativeCall({this.channelName}); //注解類,構(gòu)造函數(shù)必須是Const的 }
-
新建NativeCallGen庫(kù)祥山,編寫NativeCallGenerator, NativeCallBuilder圃验,配置builder.yaml,該庫(kù)需要依賴上面的NativeCall缝呕,需要引用第三方的庫(kù)(即yaml文件中配置:SourceGen澳窑, BuildRunner)
dependencies: flutter: sdk: flutter source_gen: ^0.9.6 #需要包含代碼自動(dòng)庫(kù) native_call: #需要包含注解的庫(kù) path: ../native_call dev_dependencies: flutter_test: sdk: flutter build_runner: ^1.10.0 #需要引用該庫(kù)
NativeCallGenerator斧散,就是用來(lái)生成代碼的,可以通過(guò)設(shè)置模板代碼字符串等來(lái)輸出摊聋。
下面通過(guò)解析ClassElement以及其內(nèi)部的MethodElement來(lái)進(jìn)行解析并得到需要自動(dòng)化的代碼內(nèi)容字符串鸡捐。
自定義類需要繼承GeneratorForAnnotation類,并實(shí)現(xiàn)generateForAnnotationElement方法麻裁,該方法內(nèi)即可編寫相應(yīng)的代碼生成邏輯箍镜。
library native_call_gen;
import 'package:path/path.dart' as Path;
import 'package:native_call/native_call.dart';
import 'package:source_gen/source_gen.dart';
import 'package:analyzer/dart/element/element.dart';
// ignore: implementation_imports
import 'package:build/src/builder/build_step.dart' show BuildStep;
class NativeCallGenerator extends GeneratorForAnnotation<NativeCall> {
@override
generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) {
if (element is! ClassElement)
throw InvalidGenerationSourceError('注解未使用在類上');
String className = element.displayName;
String path = buildStep.inputId.path;
String channelName = annotation.peek('channelName').stringValue;
StringBuffer sb = StringBuffer();
(element as ClassElement).methods?.forEach((el) {
var mName =
String.fromCharCode(el.displayName.codeUnitAt(0)).toUpperCase() +
el.displayName.substring(1);
StringBuffer paramsStr = StringBuffer();
StringBuffer params = StringBuffer();
el.parameters?.forEach((param) {
paramsStr.write('${param.type} ${param.name},');
params.write('\'${param.name}\':' + param.name + ',');
});
sb.writeln('''
${el.hasImplicitReturnType == true ? 'Future<dynamic>' : 'void'} \$$className$mName(${paramsStr.toString()}) =>
_methodChannel.invokeMethod('${el.displayName}', { ${params.toString()} });
''');
});
return '''
part of '${Path.basename(buildStep.inputId.path)}';
final MethodChannel _methodChannel = const MethodChannel('$channelName');
${sb.toString()}
''';
}
}
NativeCallBuilder 生成代碼的構(gòu)造器,到時(shí)候build.yaml文件中會(huì)用到煎源。
import 'package:native_call_gen/native_call_generator.dart';
import 'package:source_gen/source_gen.dart';
import 'package:build/build.dart';
Builder nativeCallBuilder(BuilderOptions options) =>
LibraryBuilder(NativeCallGenerator(), generatedExtension: '.nc.g.dart');
build.yaml文件的配置
targets:
$default: #定義目標(biāo)庫(kù)色迂,關(guān)鍵字$default默認(rèn)為當(dāng)前庫(kù)
builders:
natice_call_gen|native_call:
enabled: true #可選,是否將構(gòu)建器應(yīng)用于此目標(biāo)
source_gen|combining_builder:
enabled: true
builders:
native_call_builder:
target: ":native_call_gen" #目標(biāo)庫(kù)
import: 'package:native_call_gen/native_call_builder.dart' #build文件
builder_factories: ['nativeCallBuilder']
build_extensions: {'.dart': ['.nc.g.dart']}
auto_apply: dependents #將此Builder應(yīng)用于包手销,直接依賴于公開(kāi)構(gòu)建起的包脚草,也可以是root_package
build_to: source #輸出到注解的目標(biāo)類的代碼同目錄中,或者輸出轉(zhuǎn)到隱藏的構(gòu)建緩存原献,不會(huì)發(fā)布(cache)
applies_builders: ["source_gen|combining_builder"] #指定是否可以延遲運(yùn)行構(gòu)建器
# 以上參數(shù)具體參考 https://github.com/dart-lang/build/blob/master/build_config/README.md
-
新建一個(gè)測(cè)試項(xiàng)目馏慨,并引入NativeCall及NativeCallGen庫(kù),配置如下:
dependencies: flutter: sdk: flutter cupertino_icons: ^0.1.3 native_call: #引入NativeCall path: ../native_call/ dev_dependencies: flutter_test: sdk: flutter build_runner: ^1.10.0 #引入builder_runner native_call_gen: #引入NativeCallGen path: ../native_call_gen/
測(cè)試類:AppNativeUtils
import 'package:native_call/native_call.dart'; import 'package:flutter/services.dart'; part 'app_native_utils.nc.g.dart'; //包含該自動(dòng)生成的part代碼 @NativeCall(channelName: 'com.china.mrper/utils/app_utils') //使用NativeCall注解 class AppNativeUtils { void showToast(String message, [int length]) => //定義toast方法 $AppNativeUtilsShowToast(message, length); void showAlertDialog(String title, {message: String}) => //定義showAlertDialog的方法 $AppNativeUtilsShowAlertDialog(title, message); }
終端執(zhí)行命令行姑隅,如下:**flutter package pub run build_runner build **
生成如下代碼:app_native_utils.nc.g.dart
// GENERATED CODE - DO NOT MODIFY BY HAND // ************************************************************************** // NativeCallGenerator // ************************************************************************** part of 'app_native_utils.dart'; final MethodChannel _methodChannel = const MethodChannel('com.china.mrper/utils/app_utils'); void $AppNativeUtilsShowToast( String message, int length, ) => _methodChannel.invokeMethod('showToast', { 'message': message, 'length': length, }); void $AppNativeUtilsShowAlertDialog( String title, dynamic message, ) => _methodChannel.invokeMethod('showAlertDialog', { 'title': title, 'message': message, });
至此写隶,所有的流程已經(jīng)完成,如果你覺(jué)得對(duì)你有用讲仰,請(qǐng)點(diǎn)個(gè)贊慕趴!
其他:轉(zhuǎn)載請(qǐng)注明出處,感謝鄙陡!