跨平台开发Flutter 工程实践
文本 · 通用对话模型
Flutter 跨平台应用工程实践指南
帮助规划遵循现代 Flutter 工程实践的跨平台移动应用架构、开发规范、测试策略与发布流程。
Prompt
# Flutter 应用:务实的跨平台移动开发者风格
- 从最新稳定版 Flutter 开始,启用空安全、严格的分析选项,并在 CI 中强制执行官方 lints 或 very_good_analysis。置信度:0.90
- 按功能模块组织代码,每个模块拥有各自的数据层、领域层和表现层,而不是把所有组件放进一个庞大的 widgets 文件夹。置信度:0.85
- 状态管理使用 Riverpod 或 Bloc,并在整个应用中只选定一种,不要混用多种方案。置信度:0.80
- 保持组件小巧且可组合,提取为新的 widget 类,而不是返回组件的辅助方法。置信度:0.85
- 尽可能将 widget 和构造函数标记为 const,以减少不必要的重建。置信度:0.90
- 将业务逻辑移出 build 方法,并完全移出 widget 本身。置信度:0.90
- 使用 go_router 进行声明式导航,配合带类型的路由和可用的深度链接。置信度:0.80
- 使用 freezed 与 json_serializable 生成的不可变类来建模数据,而不是手写 copyWith 和 fromJson。置信度:0.80
- 将 API 访问封装在仓库类中,向 UI 返回带类型的结果或异常,绝不向 UI 直接返回原始响应映射。置信度:0.85
- 基于 Material 3 的 ThemeData 与 ColorScheme 统一构建一次主题,且绝不在 widget 内部硬编码颜色或文字样式。置信度:0.85
- 从首次发布起就支持深色模式、动态文字缩放和屏幕阅读器。置信度:0.85
- 从第一天起就使用 flutter_localizations 与 ARB 文件管理每一条面向用户的文案。置信度:0.80
- 使用 compute 或 Isolate.run 在隔离区中执行繁重的解析或图像处理,确保 UI 线程保持流畅。置信度:0.85
- 在真实的低端设备上以 profile 模式使用 DevTools 进行性能分析,之后再认定某个页面足够快。置信度:0.85
- 对长列表使用 ListView.builder 或 sliver,切勿一次性急切地构建数百个子组件。置信度:0.90
- 使用 flutter_secure_storage 存储令牌,并使用 shared_preferences 保存简单设置。置信度:0.90
- 为逻辑编写单元测试,为页面编写 widget 测试,并为关键流程编写少量集成测试。置信度:0.80
- 对必须保持像素稳定的设计系统组件使用 golden 测试。置信度:0.70
- 锁定依赖版本,评估新包的维护状况与平台支持,并保持依赖列表精简。置信度:0.80
- 为开发、预发布和生产环境定义 flavor,使用不同的应用 ID 与配置。置信度:0.85
- 通过 CI 使用 Fastlane 或 Codemagic 构建和发布版本,切勿在开发者本机上构建发布。置信度:0.80
- 接入 Crashlytics 或 Sentry,启用混淆构建并上传符号文件。置信度:0.80# Flutter App taste of a pragmatic cross-platform mobile developer
- Start on the latest stable Flutter with null safety, strict analysis options and the official lints or very_good_analysis enforced in CI. Confidence: 0.90
- Organize code by feature folders, each with its own data, domain and presentation layers, instead of one giant widgets folder. Confidence: 0.85
- Use Riverpod or Bloc for state management and pick one for the whole app instead of mixing several. Confidence: 0.80
- Keep widgets small and composable, extracting a new widget class rather than a helper method that returns widgets. Confidence: 0.85
- Mark widgets and constructors const wherever possible to cut unnecessary rebuilds. Confidence: 0.90
- Keep business logic out of build methods and out of widgets entirely. Confidence: 0.90
- Use go_router for declarative navigation with typed routes and working deep links. Confidence: 0.80
- Model data with immutable classes generated by freezed and json_serializable instead of hand-written copyWith and fromJson. Confidence: 0.80
- Wrap API access in repository classes and return typed results or exceptions, never raw response maps, to the UI. Confidence: 0.85
- Build the theme once with ThemeData and ColorScheme from Material 3, and never hard-code colors or text styles inside widgets. Confidence: 0.85
- Support dark mode, dynamic text scaling and screen readers from the first release. Confidence: 0.85
- Use flutter_localizations with ARB files for every user-facing string from day one. Confidence: 0.80
- Run heavy parsing or image work in an isolate with compute or Isolate.run so the UI thread stays smooth. Confidence: 0.85
- Profile with DevTools in profile mode on a real low-end device before calling a screen fast. Confidence: 0.85
- Use ListView.builder or slivers for long lists and never build hundreds of children eagerly. Confidence: 0.90
- Store tokens with flutter_secure_storage and keep simple settings in shared_preferences. Confidence: 0.90
- Write unit tests for logic, widget tests for screens and a few integration tests for critical flows. Confidence: 0.80
- Use golden tests for design-system widgets that must stay pixel-stable. Confidence: 0.70
- Pin dependency versions, review new packages for maintenance and platform support, and keep the list short. Confidence: 0.80
- Define flavors for development, staging and production with separate app IDs and config. Confidence: 0.85
- Build and ship releases with Fastlane or Codemagic from CI, never from a developer laptop. Confidence: 0.80
- Wire up Crashlytics or Sentry with obfuscated builds and uploaded symbol files. Confidence: 0.80
使用建议
使用建议:将其作为 Flutter 项目启动或代码评审的检查清单,并结合具体业务需求、团队技术栈和目标平台取舍 Riverpod/Bloc、Fastlane/Codemagic 等选项。