flutter-dev
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFlutter Development Guide
Flutter开发指南
A practical guide for building cross-platform applications with Flutter 3 and Dart. Focuses on proven patterns, state management, and performance optimization.
这是一份使用Flutter 3和Dart构建跨平台应用的实用指南,重点介绍经过验证的模式、状态管理和性能优化方法。
Quick Reference
快速参考
Widget Patterns
Widget模式
| Purpose | Component |
|---|---|
| State management (simple) | |
| State management (complex) | |
| Async data | |
| Real-time streams | |
| Navigation | |
| Responsive layout | |
| List display | |
| Complex scrolling | |
| Hooks | |
| Forms | |
| 用途 | 组件 |
|---|---|
| 简单状态管理 | |
| 复杂状态管理 | |
| 异步数据处理 | |
| 实时流处理 | |
| 导航 | |
| 响应式布局 | |
| 列表展示 | |
| 复杂滚动 | |
| Hooks | |
| 表单 | |
Performance Patterns
性能模式
| Purpose | Solution |
|---|---|
| Prevent rebuilds | |
| Selective updates | |
| Isolate repaints | |
| Lazy lists | |
| Heavy computation | |
| Image caching | |
| 用途 | 解决方案 |
|---|---|
| 避免重构建 | |
| 选择性更新 | |
| 隔离重绘 | |
| 懒加载列表 | |
| 繁重计算 | |
| 图片缓存 | |
Core Principles
核心原则
Widget Optimization
Widget优化
- Use constructors wherever possible
const - Extract static widgets to separate const classes
- Use for list items (ValueKey, ObjectKey)
Key - Prefer over
ConsumerWidgetfor stateStatefulWidget
- 尽可能使用构造函数
const - 将静态Widget提取到独立的const类中
- 为列表项使用(ValueKey、ObjectKey)
Key - 对于状态依赖的Widget,优先使用而非
ConsumerWidgetStatefulWidget
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 to minimize rebuilds
select()
- 使用Riverpod进行依赖注入和简单状态管理
- 使用Bloc/Cubit处理事件驱动的工作流和复杂逻辑
- 切勿直接修改状态(创建新实例)
- 使用最小化重构建
select()
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
- 采用8pt增量间距(8、16、24、32、48)
- 响应式断点:移动端(<650)、平板端(650-1100)、桌面端(>1100)
- 使用灵活布局适配所有屏幕尺寸
- 遵循Material 3 / Cupertino设计规范
Performance
性能
- Profile with DevTools before optimizing
- Target <16ms frame time for 60fps
- Use for complex animations
RepaintBoundary - Offload heavy work with
compute()
- 优化前先用DevTools进行性能分析
- 目标是60fps,帧渲染时间<16ms
- 为复杂动画使用
RepaintBoundary - 使用卸载繁重工作
compute()
Checklist
检查清单
Widget Best Practices
Widget最佳实践
- constructors on all static widgets
const - Proper on list items
Key - for state-dependent widgets
ConsumerWidget - No widget building inside method
build() - Extract reusable widgets to separate files
- 所有静态Widget使用构造函数
const - 为列表项设置正确的
Key - 状态依赖Widget使用
ConsumerWidget - 不在方法内构建Widget
build() - 将可复用Widget提取到独立文件中
State Management
状态管理
- Immutable state objects
- for granular rebuilds
select() - Proper provider scoping
- Dispose controllers and subscriptions
- Handle loading/error states
- 使用不可变状态对象
- 使用实现细粒度重构建
select() - 正确设置Provider作用域
- 销毁控制器和订阅
- 处理加载/错误状态
Navigation
导航
- GoRouter with typed routes
- Auth guards via redirect
- Deep linking support
- State preservation across routes
- 使用带类型路由的GoRouter
- 通过重定向实现权限守卫
- 支持深度链接
- 跨路由保留状态
Performance
性能
- Profile mode testing ()
flutter run --profile - <16ms frame rendering time
- No unnecessary rebuilds (DevTools check)
- Images cached and resized
- Heavy computation in isolates
- 使用Profile模式测试()
flutter run --profile - 帧渲染时间<16ms
- 无不必要的重构建(DevTools检查)
- 图片已缓存并调整大小
- 繁重计算在隔离区执行
Testing
测试
- Widget tests for UI components
- Unit tests for business logic
- Integration tests for user flows
- Bloc tests with
blocTest()
- 为UI组件编写Widget测试
- 为业务逻辑编写单元测试
- 为用户流程编写集成测试
- 使用编写Bloc测试
blocTest()
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.
| 主题 | 参考链接 |
|---|---|
| Widget模式、const优化、响应式布局 | Widget Patterns |
| Riverpod提供者、通知器、异步状态 | Riverpod State Management |
| Bloc、Cubit、事件驱动状态 | Bloc State Management |
| GoRouter配置、路由、深度链接 | GoRouter Navigation |
| 基于功能的结构、依赖管理 | Project Structure |
| 性能分析、const优化、DevTools | Performance Optimization |
| Widget测试、集成测试、模拟 | Testing Strategies |
| iOS/Android/Web特定实现 | Platform Integration |
| 隐式/显式动画、Hero、过渡 | Animations |
| Dio、拦截器、错误处理、缓存 | Networking |
| 表单验证、FormField、输入格式化器 | Forms |
| 国际化、flutter_localizations、intl | Localization |
Flutter、Dart、Material Design和Cupertino分别是Google LLC和Apple Inc.的商标。Riverpod、Bloc和GoRouter是其各自维护者开发的开源包。