Flutter Development Guide
A practical guide for building cross-platform applications with Flutter 3 and Dart. Focuses on proven patterns, state management, and performance optimization.
Quick Reference
Widget Patterns
| Purpose | Component |
|---|
| State management (simple) | INLINECODE0 + INLINECODE1 |
| State management (complex) |
NotifierProvider /
Bloc |
| Async data |
FutureProvider /
AsyncNotifierProvider |
| Real-time streams |
StreamProvider |
| Navigation |
GoRouter +
context.go/push |
| Responsive layout |
LayoutBuilder + breakpoints |
| List display |
ListView.builder |
| Complex scrolling |
CustomScrollView + Slivers |
| Hooks |
HookWidget +
useState/useEffect |
| Forms |
Form +
TextFormField + validation |
Performance Patterns
| Purpose | Solution |
|---|
| Prevent rebuilds | INLINECODE16 constructors |
| Selective updates |
ref.watch(provider.select(...)) |
| Isolate repaints |
RepaintBoundary |
| Lazy lists |
ListView.builder |
| Heavy computation |
compute() isolate |
| Image caching |
cached_network_image |
Core Principles
Widget Optimization
- - Use
const constructors wherever possible - Extract static widgets to separate const classes
- Use
Key for list items (ValueKey, ObjectKey) - Prefer
ConsumerWidget over StatefulWidget for state
State Management
- - Riverpod for dependency injection and simple state
- Bloc/Cubit for event-driven workflows and complex logic
- Never mutate state directly (create new instances)
- Use
select() to minimize rebuilds
Layout
- - 8pt spacing increments (8, 16, 24, 32, 48)
- Responsive breakpoints: mobile (<650), tablet (650-1100), desktop (>1100)
- Support all screen sizes with flexible layouts
- Follow Material 3 / Cupertino design guidelines
Performance
- - Profile with DevTools before optimizing
- Target <16ms frame time for 60fps
- Use
RepaintBoundary for complex animations - Offload heavy work with INLINECODE28
Checklist
Widget Best Practices
- - [ ]
const constructors on all static widgets - [ ] Proper
Key on list items - [ ]
ConsumerWidget for state-dependent widgets - [ ] No widget building inside
build() method - [ ] Extract reusable widgets to separate files
State Management
- - [ ] Immutable state objects
- [ ]
select() for granular rebuilds - [ ] Proper provider scoping
- [ ] Dispose controllers and subscriptions
- [ ] Handle loading/error states
Navigation
- - [ ] GoRouter with typed routes
- [ ] Auth guards via redirect
- [ ] Deep linking support
- [ ] State preservation across routes
Performance
- - [ ] Profile mode testing (
flutter run --profile) - [ ] <16ms frame rendering time
- [ ] No unnecessary rebuilds (DevTools check)
- [ ] Images cached and resized
- [ ] Heavy computation in isolates
Testing
- - [ ] Widget tests for UI components
- [ ] Unit tests for business logic
- [ ] Integration tests for user flows
- [ ] Bloc tests with INLINECODE35
References
| Topic | Reference |
|---|
| Widget patterns, const optimization, responsive layout | Widget Patterns |
| Riverpod providers, notifiers, async state |
Riverpod State Management |
| Bloc, Cubit, event-driven state |
Bloc State Management |
| GoRouter setup, routes, deep linking |
GoRouter Navigation |
| Feature-based structure, dependencies |
Project Structure |
| Profiling, const optimization, DevTools |
Performance Optimization |
| Widget tests, integration tests, mocking |
Testing Strategies |
| iOS/Android/Web specific implementations |
Platform Integration |
| Implicit/explicit animations, Hero, transitions |
Animations |
| Dio, interceptors, error handling, caching |
Networking |
| Form validation, FormField, input formatters |
Forms |
| i18n, flutter
localizations, intl | Localization |
Flutter, Dart, Material Design, and Cupertino are trademarks of Google LLC and Apple Inc. respectively. Riverpod, Bloc, and GoRouter are open-source packages by their respective maintainers.
Flutter 开发指南
使用 Flutter 3 和 Dart 构建跨平台应用的实用指南。重点介绍经过验证的模式、状态管理和性能优化。
快速参考
组件模式
| 用途 | 组件 |
|---|
| 状态管理(简单) | StateProvider + ConsumerWidget |
| 状态管理(复杂) |
NotifierProvider / Bloc |
| 异步数据 | FutureProvider / AsyncNotifierProvider |
| 实时流 | StreamProvider |
| 导航 | GoRouter + context.go/push |
| 响应式布局 | LayoutBuilder + 断点 |
| 列表展示 | ListView.builder |
| 复杂滚动 | CustomScrollView + Slivers |
| 钩子 | HookWidget + useState/useEffect |
| 表单 | Form + TextFormField + 验证 |
性能模式
| 用途 | 解决方案 |
|---|
| 防止重建 | const 构造函数 |
| 选择性更新 |
ref.watch(provider.select(...)) |
| 隔离重绘 | RepaintBoundary |
| 懒加载列表 | ListView.builder |
| 繁重计算 | compute() 隔离 |
| 图片缓存 | cached
networkimage |
核心原则
组件优化
- - 尽可能使用 const 构造函数
- 将静态组件提取到单独的 const 类中
- 为列表项使用 Key(ValueKey、ObjectKey)
- 状态管理优先使用 ConsumerWidget 而非 StatefulWidget
状态管理
- - Riverpod 用于依赖注入和简单状态
- Bloc/Cubit 用于事件驱动工作流和复杂逻辑
- 绝不直接修改状态(创建新实例)
- 使用 select() 最小化重建
布局
- - 8pt 间距增量(8、16、24、32、48)
- 响应式断点:移动端(<650)、平板(650-1100)、桌面端(>1100)
- 使用灵活布局支持所有屏幕尺寸
- 遵循 Material 3 / Cupertino 设计指南
性能
- - 优化前使用 DevTools 进行分析
- 60fps 目标帧渲染时间 <16ms
- 复杂动画使用 RepaintBoundary
- 使用 compute() 卸载繁重工作
检查清单
组件最佳实践
- - [ ] 所有静态组件使用 const 构造函数
- [ ] 列表项使用正确的 Key
- [ ] 状态相关组件使用 ConsumerWidget
- [ ] 不在 build() 方法内构建组件
- [ ] 将可复用组件提取到单独文件
状态管理
- - [ ] 不可变状态对象
- [ ] 使用 select() 实现精细重建
- [ ] 正确的 provider 作用域
- [ ] 释放控制器和订阅
- [ ] 处理加载/错误状态
导航
- - [ ] 使用 GoRouter 和类型化路由
- [ ] 通过重定向实现认证守卫
- [ ] 支持深度链接
- [ ] 跨路由保持状态
性能
- - [ ] Profile 模式测试(flutter run --profile)
- [ ] 帧渲染时间 <16ms
- [ ] 无不必要的重建(DevTools 检查)
- [ ] 图片已缓存并调整大小
- [ ] 繁重计算在隔离中执行
测试
- - [ ] UI 组件的组件测试
- [ ] 业务逻辑的单元测试
- [ ] 用户流程的集成测试
- [ ] 使用 blocTest() 进行 Bloc 测试
参考
| 主题 | 参考 |
|---|
| 组件模式、const 优化、响应式布局 | 组件模式 |
| Riverpod providers、notifiers、异步状态 |
Riverpod 状态管理 |
| Bloc、Cubit、事件驱动状态 |
Bloc 状态管理 |
| GoRouter 设置、路由、深度链接 |
GoRouter 导航 |
| 基于功能的结构、依赖关系 |
项目结构 |
| 性能分析、const 优化、DevTools |
性能优化 |
| 组件测试、集成测试、模拟 |
测试策略 |
| iOS/Android/Web 特定实现 |
平台集成 |
| 隐式/显式动画、Hero、过渡 |
动画 |
| Dio、拦截器、错误处理、缓存 |
网络 |
| 表单验证、FormField、输入格式化 |
表单 |
| i18n、flutter
localizations、intl | 本地化 |
Flutter、Dart、Material Design 和 Cupertino 分别是 Google LLC 和 Apple Inc. 的商标。Riverpod、Bloc 和 GoRouter 是其各自维护者的开源包。