xanno 0.0.2
xanno: ^0.0.2 copied to clipboard
Use annotations to simplify development @GApi @GAssets @GColor @GDoc @GEntity @GFormatter @GHttp @GIconFont @GLocale @GProject @GRoute @GSize
xanno version 0.0.2 #
Use annotations to simplify development @GApi @GAssets @GColor @GDoc @GEntity @GFormatter @GHttp @GIconFont @GLocale @GProject @GRoute @GSize
Getting Started #
命令 ==》./build.sh #
1:网络数据实体对象,使用注解 @GEntity(json: "", auto: true), 生成json to dart的转换,并生成api/entity_factory.entity.dart,网络请求内部使用
@GEntity注解使用示例
import 'package:xanno/anno/entity/entity.dart';
@GEntity(json: '''
{
"name": "爸爸",
"age": 30,
"wife": {
"name": "妈妈",
"age": 28,
"beautiful": true
},
"childList": [
{
"name": "孩子1",
"age": 1,
"sex": "男"
},
{
"name": "孩子2",
"age": 2,
"sex": "女"
}
]
}
''', auto: true)
class JsonEntity {}
2:需要跳转的页面,页面widget用注解 @GRoute('/other', '其它主页'), 生成route/main.route.dart相关方法
@GRoute注解使用示例
@GRoute(url: '/', title: 'main')
class MainPage extends StatefulWidget {
final String title;
MainPage({Key key, this.title}) : super(key: key);
_MainPageState createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
@override
Widget build(BuildContext context) {
return Container();
}
}
3:网络请求在api/api_interface.dart ApiInterface定义方法, 会自动生成api/api_factory.interface.dart APIFactory类
@apiInterface/@GET/@POST注解使用示例
@Interface(host: 'http://yj.xxxx.cn/')
abstract class ApiInterface {
///***************************使用样例***************************
@GET(url: "/tags")
@Extra({'extraKey': 1, 'extraKey2': '2'})
Future<List<dynamic>> getTags(@CancelRequest() CancelToken cancelReq);
@GET(url: "/tags")
Stream<List<String>> getTagsAsStream();
@GET(url: "/tasks")
Future<List<dynamic>> getTasks();
@GET(url: "/tasks/{id}")
Future<dynamic> getTask(@Path("id") String id);
@PATCH(url: "/tasks/{id}")
Future<dynamic> updateTaskPart(
@Path() String id, @Body() Map<String, dynamic> map);
@PUT(url: "/tasks/{id}")
Future<dynamic> updateTask(@Path() String id, @Body() dynamic task);
@DELETE(url: "/tasks/{id}")
Future<void> deleteTask(@Path() String id);
@POST(url: "/tasks")
Future<dynamic> createTask(@Body() dynamic task);
@POST(url: "/tasks")
Future<List<dynamic>> createTasks(@Body() List<dynamic> tasks);
@POST(url: "/tasks")
Future<List<String>> createTaskNames(@Body() List<String> tasks);
@POST(host: "http://httpbin.org/post")
Future<void> createNewTaskFromFile(@Part() File file);
@Headers({"accept": "image/jpeg"})
@GET(host: "http://httpbin.org/image/jpeg")
@DioResponseType(ResponseType.bytes)
Future<List<int>> getFile();
@POST(host: "http://httpbin.org/post")
@FormUrlEncoded()
Future<String> postUrlEncodedFormData(
@Field() String hello, {
@Field() String gg,
});
@HEAD(url: '/')
Future<String> headRequest();
@HEAD(url: '/')
Future headRquest2();
@HEAD(url: '/')
Future<HttpResponse> headRquest3();
@GET(url: "/task/group")
Future<List<dynamic>> grouppedTaskByDate();
@GET(url: "/task")
Future<HttpResponse<List<dynamic>>> getTasksWithReponse();
@DELETE(url: "/tasks/{id}")
Future<HttpResponse<void>> deleteTaskWithResponse(@Path() String id);
@POST(url: "/post")
Future<String> postFormData(@Part() dynamic task, {@Part() File file});
@POST(url: "/post")
Future<String> postFormData2(
@Part() List<Map<String, dynamic>> task,
@Part() List<String> tags,
@Part(contentType: 'application/json') File file);
@POST(url: "/post")
Future<String> postFormData3(
{@Part(value: "customfiles", contentType: 'application/json')
List<File> files,
@Part()
File file});
@POST(url: "/post")
Future<String> postFormData6(
{@Part(value: "customfiles") List<List<int>> files,
@Part() List<int> file});
@POST(url: "/post")
Future<String> postFormData4(@Part() List<dynamic> tasks, @Part() File file);
@POST(url: "/post")
Future<String> postFormData5(
@Part() List<dynamic> tasks,
@Part() Map<String, dynamic> map,
@Part() int a, {
@Part() bool b,
@Part() double c,
@Part() String d,
});
@GET(url: '/demo')
Future<String> queries(@Queries() Map<String, dynamic> queries);
@GET(url: '/enums')
Future<String> queryByEnum(@Query('tasks') dynamic query);
@GET(url: "/get")
Future<String> namedExample(@Query("apikey") String apiKey,
@Query("scope") String scope, @Query("type") String type,
{@Query("from") int from});
@POST(url: "/postfile")
@Headers({
"Content-Type": "application/octet-stream",
"Ocp-Apim-Subscription-Key": "abc"
})
Future<String> postFile({
@Body() File file,
@SendProgress() Function sendProgress,
@ReceiveProgress() Function receiveProgress,
});
@GET(url: "")
Future<String> testCustomOptions(@DioOptions() Options options);
///***************************使用样例***************************
}
4:自动注册@GAssets('assets')资源,不用人为去yaml里面添加资源(目前只支持 assets:),自动映射到common/assets_constant.assets.dart
5:自动格式化和检测最大行数超过300代码@GFormatter(auto: true, maxLine: 300),lib文件加下所有dart源码格式化和检测。
6:自动生成文档@GDoc(auto: true),目前直接开放lib路径访问,之后优化成自定义网页可看文件内容。
7:自动注册@GIconfont(prefix: '', networkUrl: '//at.alicdn.com/t/font_2276902_tfhdmqlk7gd.js'),生成widget/icon_font.iconfont.dart
8:自动生成@GApi(init: true) lib/api 下网络相关资源
9:自动生成@GColor(init: true) 自动收集所有使用Color的地方统一管理common/color_constant.color.dart
10:自动生成@GSize(init: true) 自动收集所有使用Size的地方统一管理common/size_constant.size.dart
11:自动生成@GLocale(init: true, enable: false) 自动收集所有使用I18N.local("key", zh:"", en:"")的地方统一管理
12:自动生成@GProject(init: true) 基础项目代码
注解使用示例
@GApi(init: true)
@GColor(init: true)
@GSize(init: true)
@GLocale(init: true, enable: false)
@GAssets(assetsPath: 'assets')
@GFormatter(auto: true)
@GIconfont(
prefix: '', networkUrl: '//at.alicdn.com/t/font_2276902_tfhdmqlk7gd.js')
class App extends StatefulWidget {
App({Key key}) : super(key: key);
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: routes(context),
);
}
}