Files
cattleTransportation/.cursor/rules/frameworks/flutter.mdc
2025-11-04 09:38:19 +08:00

41 lines
1.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
description: 该规则解释了 Flutter 小部件模式和跨平台移动开发的最佳实践。
globs: **/*.dart
alwaysApply: false
---
# Flutter 规则
- 对于没有内部状态的 UI 组件使用 StatelessWidget。
- 对于需要维护状态的组件使用 StatefulWidget
```dart
class Counter extends StatefulWidget {
@override
_CounterState createState() => _CounterState();
}
class _CounterState extends State<Counter> {
int _count = 0;
void _increment() {
setState(() { _count++; });
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Text('Count: $_count'),
ElevatedButton(onPressed: _increment, child: Text('Increment')),
],
);
}
}
```
- 对于复杂应用使用状态管理解决方案Provider、Bloc、Riverpod
- 使用适当的文件夹结构组织代码models、screens、widgets、services
- 使用命名路由和 Navigator.pushNamed() 进行导航。
- 使用 async/await 处理异步操作,并进行适当的错误处理。
- 使用主题themes在整个应用中保持一致的样式。