1 Star 2 Fork 2

请叫我code哥 / xanno

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
README-ZH.md 6.91 KB
Copy Edit Web IDE Raw Blame History
请叫我code哥 authored 2021-07-14 14:44 . 0.0.5+4

Language: English | 中文简体

Packages: xanno

xanno

Use annotations to simplify development @GApi @GAssets @GColor @GEntity @GFormatter @GHttp @GIconFont @GProject @GRoute @GSize

Getting Started

1:自动生成基础项目, 使用注解@GProject(),

import 'package:xanno/xanno.dart';

@GProject()
void main() {}
  • dev_dependencies:
    • source_gen: ^1.0.0
    • build_runner: ^1.12.2
    • xanno: ^0.0.5+4
  • 执行: flutter pub get
  • 执行: flutter pub get && flutter packages pub run build_runner build && flutter pub get

命令 ==》./build.sh

2:网络数据实体对象,使用注解 @GEntity(json: "", auto: true), 生成json to dart的转换,并生成api/entity_factory.entity.dart,网络请求内部使用

@GEntity注解使用示例
@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 {}

3:需要跳转的页面,页面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();
  }
}

4:网络请求在api/api_interface.dart ApiInterface定义方法, 会自动生成api/api_factory.interface.dart APIFactory类

@apiInterface/@GET/@POST注解使用示例
@Interface(host: 'https://api.muxiaoguo.cn/api/')
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")
  @Extra({'isLoading': true})
  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(int, int)? sendProgress,
    @ReceiveProgress() Function(int, int)? receiveProgress,
  });

  @GET(url: "")
  Future<String> testCustomOptions(@DioOptions() Options options);

  ///***************************使用样例***************************
}
5:自动注册@GAssets('assets')资源,不用人为去yaml里面添加资源(目前只支持 assets:),自动映射到common/assets_constant.assets.dart
6:自动格式化和检测最大行数超过300代码@GFormatter(maxLine: 300),lib文件加下所有dart源码格式化和检测。
7:自动注册@GIconfont(url: '//at.alicdn.com/t/font_2276902_tfhdmqlk7gd.js'),生成widget/icon_font.iconfont.dart
8:自动生成@GApi() lib/api 下网络相关资源
9:自动生成@GColor() 自动收集所有使用Color的地方统一管理common/color_constant.color.dart
10:自动生成@GSize() 自动收集所有使用Size的地方统一管理common/size_constant.size.dart
注解使用示例
@GApi()
@GColor()
@GSize()
@GAssets(path: 'assets')
@GFormatter()
@GIconfont(url: '//at.alicdn.com/t/font_xxxx_g48kd9v3h54.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),
    );
  }
}

相关引用库和项目

  • observable_builder (轻量级Flutter状态管理库,支持本地刷新和状态缓存.)

Comment ( 0 )

Sign in for post a comment

Dart
1
https://gitee.com/337317439/xanno.git
git@gitee.com:337317439/xanno.git
337317439
xanno
xanno
master

Search