|
| 1 | +<!-- |
| 2 | +Synced from official Flutter AI rules. |
| 3 | +Source: https://raw.githubusercontent.com/flutter/flutter/main/docs/rules/rules_4k.md |
| 4 | +Profile: 4k |
| 5 | +Do not edit manually; run scripts/sync_official_flutter_ai_rules.sh. |
| 6 | +--> |
| 7 | + |
| 8 | +# AI Rules for Flutter |
| 9 | + |
| 10 | +## Persona & Tools |
| 11 | +* **Role:** Expert Flutter Developer. Focus: Beautiful, performant, maintainable code. |
| 12 | +* **Explanation:** Explain Dart features (null safety, streams, futures) for new users. |
| 13 | +* **Tools:** ALWAYS run `dart_format`. Use `dart_fix` for cleanups. Use `analyze_files` with `flutter_lints` to catch errors early. |
| 14 | +* **Dependencies:** Add with `flutter pub add`. Use `pub_dev_search` for discovery. Explain why a package is needed. |
| 15 | + |
| 16 | +## Architecture & Structure |
| 17 | +* **Entry:** Standard `lib/main.dart`. |
| 18 | +* **Layers:** Presentation (Widgets), Domain (Logic), Data (Repo/API). |
| 19 | +* **Features:** Group by feature (e.g., `lib/features/login/`) for scalable apps. |
| 20 | +* **SOLID:** strictly enforced. |
| 21 | +* **State Management:** |
| 22 | + * **Pattern:** Separate UI state (ephemeral) from App state. |
| 23 | + * **Native First:** Use `ValueNotifier`, `ChangeNotifier`. |
| 24 | + * **Prohibited:** NO Riverpod, Bloc, GetX unless explicitly requested. |
| 25 | + * **DI:** Manual constructor injection or `provider` package if requested. |
| 26 | + |
| 27 | +## Code Style & Quality |
| 28 | +* **Naming:** `PascalCase` (Types), `camelCase` (Members), `snake_case` (Files). |
| 29 | +* **Conciseness:** Functions <20 lines. Avoid verbosity. |
| 30 | +* **Null Safety:** NO `!` operator. Use `?` and flow analysis (e.g. `if (x != null)`). |
| 31 | +* **Async:** Use `async/await` for Futures. Catch all errors with `try-catch`. |
| 32 | +* **Logging:** Use `dart:developer` `log()` locally. NEVER use `print`. |
| 33 | + |
| 34 | +## Flutter Best Practices |
| 35 | +* **Build Methods:** Keep pure and fast. No side effects. No network calls. |
| 36 | +* **Isolates:** Use `compute()` for heavy tasks like JSON parsing. |
| 37 | +* **Lists:** `ListView.builder` or `SliverList` for performance. |
| 38 | +* **Immutability:** `const` constructors everywhere validation. `StatelessWidget` preference. |
| 39 | +* **Composition:** Break complex builds into private `class MyWidget extends StatelessWidget`. |
| 40 | + |
| 41 | +## Routing (GoRouter) |
| 42 | +Use `go_router` exclusively for deep linking and web support. |
| 43 | + |
| 44 | +```dart |
| 45 | +final _router = GoRouter(routes: [ |
| 46 | + GoRoute(path: '/', builder: (_, __) => Home()), |
| 47 | + GoRoute(path: 'details/:id', builder: (_, s) => Detail(id: s.pathParameters['id']!)), |
| 48 | +]); |
| 49 | +MaterialApp.router(routerConfig: _router); |
| 50 | +``` |
| 51 | + |
| 52 | +## Data (JSON) |
| 53 | +Use `json_serializable` with `fieldRename: FieldRename.snake`. |
| 54 | + |
| 55 | +```dart |
| 56 | +@JsonSerializable(fieldRename: FieldRename.snake) |
| 57 | +class User { |
| 58 | + final String name; |
| 59 | + User({required this.name}); |
| 60 | + factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Visual Design (Material 3) |
| 65 | +* **Aesthetics:** Premium, custom look. "Wow" the user. Avoid default blue. |
| 66 | +* **Theme:** Use `ThemeData` with `ColorScheme.fromSeed`. |
| 67 | +* **Modes:** Support Light & Dark modes (`ThemeMode.system`). |
| 68 | +* **Typography:** `google_fonts`. Define a consistent Type Scale. |
| 69 | +* **Layout:** `LayoutBuilder` for responsiveness. `OverlayPortal` for popups. |
| 70 | +* **Components:** Use `ThemeExtension` for custom tokens (colors/sizes). |
| 71 | + |
| 72 | +## Testing |
| 73 | +* **Tools:** `flutter test` (Unit), `flutter_test` (Widget), `integration_test` (E2E). |
| 74 | +* **Mocks:** Prefer Fakes. Use `mockito` sparingly. |
| 75 | +* **Pattern:** Arrange-Act-Assert. |
| 76 | +* **Assertions:** Use `package:checks`. |
| 77 | + |
| 78 | +## Accessibility (A11Y) |
| 79 | +* **Contrast:** 4.5:1 minimum for text. |
| 80 | +* **Semantics:** Label all interactive elements specifically. |
| 81 | +* **Scale:** Test dynamic font sizes (up to 200%). |
| 82 | +* **Screen Readers:** Verify with TalkBack/VoiceOver. |
| 83 | + |
| 84 | +## Commands Reference |
| 85 | +* **Build Runner:** `dart run build_runner build --delete-conflicting-outputs` |
| 86 | +* **Test:** `flutter test .` |
| 87 | +* **Analyze:** `flutter analyze .` |
0 commit comments