Flutter插件xanno的使用方法

Flutter插件xanno的使用方法

Packages: xanno

xanno

使用注解简化开发 @GApi @GAssets @GColor @GEntity @GFormatter @GHttp @GIconFont @GProject @GRoute @GSize


Flutter插件xanno开始使用

1. 创建项目

通过 [@GProject](/user/GProject)() 注解自动生成项目结构。

import 'package:xanno/xanno.dart';

[@GProject](/user/GProject)()
void main() {}

依赖项

pubspec.yaml 中添加以下依赖项:

dev_dependencies:
  source_gen: ^1.2.7
  build_runner: ^2.3.3
  intl_utils: ^2.8.2
  xanno: ^0.0.9+8

运行以下命令生成项目文件:

flutter pub get && flutter packages pub run build_runner clean && flutter packages pub run build_runner build && flutter pub get

命令 ==》make

2. 网络数据实体对象

使用 [@GEntity](/user/GEntity)() 注解,自动生成 JSON 转换代码,并生成内部使用的 API/Entity_Factory.entity.dart 文件。

示例:[@GEntity](/user/GEntity) 注解使用

[@GEntity](/user/GEntity)(json: '''
{
    "name": "name1",
    "age": 30,
    "wife": {
        "name": "name2",
        "age": 28,
        "beautiful": true
    },
    "childList": [
        {
            "name": "child1",
            "age": 1,
            "sex": "男"
        },
        {
            "name": "child2",
            "age": 2,
            "sex": "女"
        }
    ]
}
''', auto: true)
class JsonEntity {}

3. 页面跳转

为需要跳转的页面添加 [@GRoute](/user/GRoute)() 注解,自动生成路由配置文件 route/main.route.dart

示例:[@GRoute](/user/GRoute) 注解使用

[@GRoute](/user/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](/user/override)
  Widget build(BuildContext context) {
      return Container();
  }
}

4. API 定义

通过 @Interface 注解定义网络请求方法,自动生成 API/Api_Factory.Interface.dartApiFactory 类。

示例:@apiInterface@GET 注解使用

@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: "/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);
  
  // 其他方法省略...
}

其他功能

5. 自动注册资源 [@GAssets](/user/GAssets)

无需手动在 pubspec.yaml 中添加资源,支持自动映射到 common/assets_constant.assets.dart

[@GAssets](/user/GAssets)(path: 'assets')
class AssetsManager {}

6. 大于 300 行代码的格式化与检测 [@GFormatter](/user/GFormatter)

支持对超过 300 行的代码进行格式化和检测。

[@GFormatter](/user/GFormatter)(maxLine: 300)
class CodeFormatter {}

7. 自动注册图标字体 [@GIconFont](/user/GIconFont)

通过指定图标字体 URL,自动生成 widget/icon_font.iconfont

[@GIconfont](/user/GIconfont)(url: '//at.alicdn.com/t/font_xxxx_g48kd9v3h54.js')
class IconFontManager {}

8. 自动生成网络相关资源 [@GApi](/user/GApi)

通过 [@GApi](/user/GApi)() 自动生成 API 相关代码。

[@GApi](/user/GApi)()
class NetworkManager {}

9. 自动收集颜色管理 [@GColor](/user/GColor)

统一管理颜色,自动生成 common/color_constant.color.dart

[@GColor](/user/GColor)()
class ColorManager {}

10. 自动收集尺寸管理 [@GSize](/user/GSize)

统一管理尺寸,自动生成 common/size_constant.size.dart

[@GSize](/user/GSize)()
class SizeManager {}

11. 自动生成 Jenkinsfile @GJenkinsfile

支持持续集成,自动生成 Jenkinsfile 文件。

@GJenkinsfile()
class JenkinsConfig {}

示例代码

[@GApi](/user/GApi)()
[@GColor](/user/GColor)()
[@GSize](/user/GSize)()
[@GAssets](/user/GAssets)(path: 'assets')
[@GFormatter](/user/GFormatter)()
[@GIconfont](/user/GIconfont)(url: '//at.alicdn.com/t/font_xxxx_g48kd9v3h54.js')
class App extends StatefulWidget {
  App({Key key}) : super(key: key);

  [@override](/user/override)
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
        routes: routes(context),
    );
  }
}

更多关于Flutter插件xanno的使用方法的实战教程也可以访问 https://www.itying.com/category-92-b0.html

回到顶部