# flutter-template **Repository Path**: At-ease/flutter-template ## Basic Information - **Project Name**: flutter-template - **Description**: flutter模版 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-04-13 - **Last Updated**: 2026-11-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: Flutter ## README # flutter_app 一个基于 Flutter + GetX 的个人工具箱模板。本文档覆盖 `lib/components`、`lib/core`、`lib/utils` 三个目录下所有模块的说明与使用方法。 [编译运行文档](./docs/README.md) ## 目录结构 ``` lib/ ├── components/ # 通用 UI 组件 │ └── appbar.dart # 统一风格 AppBar 构建函数 ├── core/ # 核心能力(网络、路由、状态、主题、日志、图标) │ ├── console.dart # 日志打印 │ ├── controller.dart # 全局状态控制器 │ ├── iconfont.dart # 自定义 iconfont 图标 │ ├── request.dart # Dio 封装的 HTTP 客户端 │ ├── routers.dart # 路由表 │ └── theme.dart # 主题与配色 ├── utils/ # 工具类 │ ├── local_authentication.dart # 生物识别(指纹)认证 │ └── local_file.dart # 本地 JSON 文件读写 └── views/ # 页面 ``` --- ## components/appbar.dart ### `appBar()` 统一风格的 AppBar 构建函数,自带居中标题、左侧返回按钮、可选图标与右侧操作区。 **参数** | 参数 | 类型 | 必填 | 说明 | | --------- | --------------- | --- | -------------------------- | | `context` | `BuildContext` | ✅ | 上下文,用于左侧返回 `Navigator.pop` | | `title` | `String` | ✅ | 标题文本 | | `icon` | `IconData?` | ❌ | 标题左侧的图标(带底色容器) | | `color` | `Color?` | ❌ | 标题与返回图标颜色 | | `bgColor` | `Color?` | ❌ | 顶部背景色,默认白色 | | `actions` | `List?` | ❌ | 右侧操作按钮,默认空 | **示例** ```dart import 'package:flutter_app/components/appbar.dart'; import 'package:flutter_app/core/theme.dart'; Scaffold( appBar: appBar( context: context, title: '首页', color: Colors.white, bgColor: AppTheme.primaryLight, actions: [ IconButton( icon: const Icon(Icons.settings), onPressed: () {}, ), ], ), body: ..., ); ``` > 注意:左侧返回按钮始终存在,调用 `Navigator.pop(context)`。若为根页面请自行处理返回逻辑。 --- ## core/console.dart ### `Console` 基于 `logger` 封装的静态日志类,按级别输出带颜色与 emoji 的日志。 | 方法 | 级别 | 颜色 | | ------------------------ | ------- | --- | | `Console.debug(message)` | debug | 蓝色 | | `Console.info(message)` | info | 绿色 | | `Console.warn(message)` | warning | 黄色 | | `Console.err(message)` | error | 红色 | | `Console.fatal(message)` | fatal | 紫色 | **示例** ```dart import 'package:flutter_app/core/console.dart'; Console.info('用户登录成功'); Console.warn('未登录'); Console.err('请求失败: $e'); ``` --- ## core/iconfont.dart ### `Iconfont` 自定义 iconfont 图标常量(字体族 `iconfont`,需在 `pubspec.yaml` 注册 `assets/fonts/iconfont.ttf`)。 **示例** ```dart Icon(Iconfont.camera, size: 24, color: AppTheme.primary); ``` --- ## core/routers.dart ### `AppRouter` 集中式路由表,需在 `main.dart` 的 `GetMaterialApp.getPages` 中注册。 **示例** ```dart import 'package:flutter_app/core/routers.dart'; GetMaterialApp( initialRoute: '/home', getPages: AppRouter.routers, ... ); ``` 页面路由建议采用「init 文件 + 静态路由」模式(参考 `views/home/init.dart`): ```dart class HomeRouter { HomeRouter._(); static const String name = '/home'; static final route = GetPage(name: name, page: () => HomePage()); } // 在 AppRouter 中汇总 static final routers = [HomeRouter.route]; ``` 跳转: ```dart Get.toNamed('/home'); ``` --- ## core/request.dart 基于 `dio` 封装的网络请求模块,包含异常、配置、拦截器与 HTTP 客户端。 ### `HttpClient` **全局实例** `request.dart` 已导出一个开箱即用的全局实例: ```dart import 'package:flutter_app/core/request.dart'; const def = await http.get('/list', queryParameters: {'page': 1}); final res = await http.post('/login', data: {'username': 'a', 'password': 'b'}); final path = await http.download('https://x.com/a.pdf', '/storage/a.pdf'); ``` **修改服务地址** 文件底部: ```dart const baseUrl = 'http://localhost:0000'; final http = HttpClient( config: HttpConfig( baseUrl: baseUrl, connectTimeout: Duration(seconds: 15), receiveTimeout: Duration(seconds: 15), defaultHeaders: {'Content-Type': 'application/json'}, ), ); ``` 按需替换 `baseUrl` 即可。 --- ## utils/local_authentication.dart ### `LocalsAuthentication` 封装 `local_auth` 的生物识别(指纹)认证。 - `authenticate()`:检查设备是否支持并注册生物识别,弹出验证框,返回 `AuthResult`。 ### `AuthResult` | 字段 | 类型 | 说明 | | --------- | -------- | ------- | | `success` | `bool` | 是否认证成功 | | `message` | `String` | 失败/提示信息 | **示例** ```dart import 'package:flutter_app/utils/local_authentication.dart'; final result = await LocalsAuthentication().authenticate(); if (result.success) { // 认证成功 } else { Console.warn(result.message); } ``` > 平台配置:Android 需在 `AndroidManifest.xml` 声明 `USE_BIOMETRIC` 权限;iOS 需在 `Info.plist` 配置 `NSFaceIDUsageDescription`。 --- ## utils/local_file.dart ### `LocalFileStorage` 基于 `path_provider` 的应用文档目录 JSON 文件读写(文件固定为 `accunt_record.json`)。 | 方法 | 说明 | | -------------------------------------------- | ------------------------ | | `writeJson(List> data)` | 写入 JSON 数据 | | `readJson()` | 读取 JSON 数据,失败时返回空列表 `[]` | **示例** ```dart import 'package:flutter_app/utils/local_file.dart'; // 写入 await LocalFileStorage.writeJson([ {'id': 1, 'name': '记录一'}, ]); // 读取 final records = await LocalFileStorage.readJson(); ``` --- ## 依赖速览(pubspec.yaml) | 依赖 | 用途 | | ----------------------------------- | ---------------- | | `get` | 状态管理 / 路由 / 依赖注入 | | `get_storage` | 轻量本地键值存储 | | `dio` | 网络请求 | | `logger` | 日志 | | `local_auth` / `local_auth_android` | 生物识别认证 | | `path_provider` | 本地文件目录 | | `permission_handler` | 权限申请 | | `camera` | 相机 | | `flutter_launcher_icons` | 应用图标生成 | 资源已在 `pubspec.yaml` 注册: ```yaml assets: - assets/images/ fonts: - family: iconfont fonts: - asset: assets/fonts/iconfont.ttf ```