sdd-full 4.6.2 → 4.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin.js +1 -1
- package/package.json +1 -1
- package/skills/.agents/skills/flutter-add-integration-test/SKILL.md +165 -0
- package/skills/.agents/skills/flutter-add-widget-preview/SKILL.md +147 -0
- package/skills/.agents/skills/flutter-add-widget-test/SKILL.md +156 -0
- package/skills/.agents/skills/flutter-apply-architecture-best-practices/SKILL.md +164 -0
- package/skills/.agents/skills/flutter-build-responsive-layout/SKILL.md +141 -0
- package/skills/.agents/skills/flutter-fix-layout-issues/SKILL.md +132 -0
- package/skills/.agents/skills/flutter-implement-json-serialization/SKILL.md +155 -0
- package/skills/.agents/skills/flutter-setup-declarative-routing/SKILL.md +257 -0
- package/skills/.agents/skills/flutter-setup-localization/SKILL.md +212 -0
- package/skills/.agents/skills/flutter-use-http-package/SKILL.md +177 -0
- package/skills/VERSION.md +176 -62
- package/skills/design-planning/ai-coding-rules/SKILL.md +5 -13
- package/skills/design-planning/design-to-code/SKILL.md +5 -14
- package/skills/design-planning/enterprise-spec/SKILL.md +5 -13
- package/skills/design-planning/flutter-av/SKILL.md +5 -16
- package/skills/design-planning/flutter-map/SKILL.md +5 -14
- package/skills/design-planning/function-sdd/SKILL.md +5 -13
- package/skills/design-planning/global-overlay-stack-standard/SKILL.md +73 -0
- package/skills/design-planning/ui-motion-interaction-standard/SKILL.md +69 -0
- package/skills/design-planning/ui-sdd-specialized/SKILL.md +5 -14
- package/skills/development-execution/flutter-errors/SKILL.md +5 -15
- package/skills/flutter-skills/.github/dependabot.yaml +15 -0
- package/skills/flutter-skills/.github/workflows/dart_skills_lint_workflow.yaml +68 -0
- package/skills/flutter-skills/.github/workflows/skills_tool.yaml +51 -0
- package/skills/flutter-skills/CODE_OF_CONDUCT.md +3 -0
- package/skills/flutter-skills/CONTRIBUTING.md +36 -0
- package/skills/flutter-skills/LICENSE +26 -0
- package/skills/flutter-skills/README.md +50 -0
- package/skills/flutter-skills/pubspec.yaml +9 -0
- package/skills/flutter-skills/resources/flutter_skills.yaml +434 -0
- package/skills/flutter-skills/skills/flutter-add-integration-test/SKILL.md +163 -0
- package/skills/flutter-skills/skills/flutter-add-widget-preview/SKILL.md +145 -0
- package/skills/flutter-skills/skills/flutter-add-widget-test/SKILL.md +154 -0
- package/skills/flutter-skills/skills/flutter-apply-architecture-best-practices/SKILL.md +162 -0
- package/skills/flutter-skills/skills/flutter-build-responsive-layout/SKILL.md +139 -0
- package/skills/flutter-skills/skills/flutter-fix-layout-issues/SKILL.md +130 -0
- package/skills/flutter-skills/skills/flutter-implement-json-serialization/SKILL.md +153 -0
- package/skills/flutter-skills/skills/flutter-setup-declarative-routing/SKILL.md +255 -0
- package/skills/flutter-skills/skills/flutter-setup-localization/SKILL.md +210 -0
- package/skills/flutter-skills/skills/flutter-use-http-package/SKILL.md +175 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/add-dart-lint-validation-rule/SKILL.md +196 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-best-practices/SKILL.md +65 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-checks-migration/SKILL.md +158 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-cli-app-best-practices/SKILL.md +168 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-doc-validation/SKILL.md +87 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-long-lines/SKILL.md +101 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-matcher-best-practices/SKILL.md +136 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-modern-features/SKILL.md +266 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-package-maintenance/SKILL.md +92 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-test-coverage/SKILL.md +92 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-test-coverage/example/lib/src/calculator.dart +7 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-test-coverage/example/pubspec.yaml +8 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-test-coverage/example/test/calculator_test.dart +11 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-test-coverage/scripts/interpret_coverage.dart +95 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-test-coverage/scripts/pubspec.yaml +6 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-test-coverage/scripts/test/interpret_coverage_test.dart +93 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/dart-test-fundamentals/SKILL.md +173 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/definition-of-done/SKILL.md +27 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/flutter_skills_ignore.json +3 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/grill-me/SKILL.md +10 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/ignore.json +3 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/test-driven-development/SKILL.md +371 -0
- package/skills/flutter-skills/tool/dart_skills_lint/.agents/skills/test-driven-development/testing-anti-patterns.md +299 -0
- package/skills/flutter-skills/tool/dart_skills_lint/AUTHORS +7 -0
- package/skills/flutter-skills/tool/dart_skills_lint/CHANGELOG.md +12 -0
- package/skills/flutter-skills/tool/dart_skills_lint/CONTRIBUTING.md +51 -0
- package/skills/flutter-skills/tool/dart_skills_lint/LICENSE +27 -0
- package/skills/flutter-skills/tool/dart_skills_lint/README.md +203 -0
- package/skills/flutter-skills/tool/dart_skills_lint/analysis_options.yaml +296 -0
- package/skills/flutter-skills/tool/dart_skills_lint/bench/README.md +23 -0
- package/skills/flutter-skills/tool/dart_skills_lint/bench/baseline_throughput.dart +230 -0
- package/skills/flutter-skills/tool/dart_skills_lint/bin/cli.dart +10 -0
- package/skills/flutter-skills/tool/dart_skills_lint/dart_skills_lint.yaml +14 -0
- package/skills/flutter-skills/tool/dart_skills_lint/documentation/feature_design_docs/PRODUCTION_READYNESS.md +48 -0
- package/skills/flutter-skills/tool/dart_skills_lint/documentation/feature_design_docs/completion_migration_plan.md +99 -0
- package/skills/flutter-skills/tool/dart_skills_lint/documentation/feature_design_docs/legacy_patterns_report.md +110 -0
- package/skills/flutter-skills/tool/dart_skills_lint/documentation/feature_design_docs/pub_vs_skill_report.md +56 -0
- package/skills/flutter-skills/tool/dart_skills_lint/documentation/knowledge/SPECIFICATION.md +79 -0
- package/skills/flutter-skills/tool/dart_skills_lint/documentation/knowledge/architecture_overview.md +64 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/dart_skills_lint.dart +11 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/config_parser.dart +156 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/entry_point.dart +354 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/fixable_rule.dart +20 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/models/analysis_severity.dart +15 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/models/check_type.dart +17 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/models/ignore_entry.dart +34 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/models/ignore_entry.g.dart +19 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/models/skill_context.dart +27 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/models/skill_rule.dart +27 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/models/skills_ignores.dart +26 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/models/skills_ignores.g.dart +24 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/models/validation_error.dart +31 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/rule_registry.dart +79 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/rules/absolute_paths_rule.dart +74 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/rules/description_length_rule.dart +49 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/rules/disallowed_field_rule.dart +61 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/rules/name_format_rule.dart +167 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/rules/relative_paths_rule.dart +72 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/rules/trailing_whitespace_rule.dart +93 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/rules/valid_yaml_metadata_rule.dart +74 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/skills_ignores_storage.dart +36 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/validation_session.dart +559 -0
- package/skills/flutter-skills/tool/dart_skills_lint/lib/src/validator.dart +238 -0
- package/skills/flutter-skills/tool/dart_skills_lint/pubspec.yaml +28 -0
- package/skills/flutter-skills/tool/dart_skills_lint/skills/README.md +10 -0
- package/skills/flutter-skills/tool/dart_skills_lint/skills/dart-skills-lint-validation/SKILL.md +195 -0
- package/skills/flutter-skills/tool/dart_skills_lint/skills-lock.json +75 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/absolute_paths_test.dart +167 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/cli_integration_test.dart +683 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/config_file_test.dart +292 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/custom_rule_test.dart +122 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/directory_structure_test.dart +163 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/field_constraints_test.dart +178 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/fixer_test.dart +172 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/ignore_models_test.dart +63 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/metadata_validation_test.dart +116 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/relative_path_flag_test.dart +70 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/relative_paths_test.dart +172 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/resolve_rules_test.dart +82 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/rule_naming_test.dart +29 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/skills_ignores_storage_test.dart +89 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/test_utils.dart +19 -0
- package/skills/flutter-skills/tool/dart_skills_lint/test/trailing_whitespace_test.dart +152 -0
- package/skills/flutter-skills/tool/generator/README.md +150 -0
- package/skills/flutter-skills/tool/generator/analysis_options.yaml +143 -0
- package/skills/flutter-skills/tool/generator/bin/skills.dart +73 -0
- package/skills/flutter-skills/tool/generator/lib/src/commands/base_skill_command.dart +87 -0
- package/skills/flutter-skills/tool/generator/lib/src/commands/base_yaml_command.dart +83 -0
- package/skills/flutter-skills/tool/generator/lib/src/commands/generate_skill_command.dart +92 -0
- package/skills/flutter-skills/tool/generator/lib/src/commands/update_readme_command.dart +150 -0
- package/skills/flutter-skills/tool/generator/lib/src/commands/update_skill_command.dart +97 -0
- package/skills/flutter-skills/tool/generator/lib/src/commands/validate_skill_command.dart +284 -0
- package/skills/flutter-skills/tool/generator/lib/src/models/skill_params.dart +41 -0
- package/skills/flutter-skills/tool/generator/lib/src/services/gemini_service.dart +310 -0
- package/skills/flutter-skills/tool/generator/lib/src/services/markdown_converter.dart +226 -0
- package/skills/flutter-skills/tool/generator/lib/src/services/prompts.dart +72 -0
- package/skills/flutter-skills/tool/generator/lib/src/services/resource_fetcher_service.dart +84 -0
- package/skills/flutter-skills/tool/generator/lib/src/services/skill_instructions.dart +30 -0
- package/skills/flutter-skills/tool/generator/pubspec.yaml +32 -0
- package/skills/flutter-skills/tool/generator/test/commands/base_skill_command_test.dart +131 -0
- package/skills/flutter-skills/tool/generator/test/commands/validate_skills_input_test.dart +263 -0
- package/skills/flutter-skills/tool/generator/test/custom_skill_rules/last_modified_rule.dart +32 -0
- package/skills/flutter-skills/tool/generator/test/generate_skills_retry_test.dart +105 -0
- package/skills/flutter-skills/tool/generator/test/generate_skills_test.dart +519 -0
- package/skills/flutter-skills/tool/generator/test/lint_skills_test.dart +34 -0
- package/skills/flutter-skills/tool/generator/test/markdown_converter_test.dart +103 -0
- package/skills/flutter-skills/tool/generator/test/markdown_table_test.dart +131 -0
- package/skills/flutter-skills/tool/generator/test/models/skill_params_test.dart +37 -0
- package/skills/flutter-skills/tool/generator/test/services/gemini_service_test.dart +291 -0
- package/skills/flutter-skills/tool/generator/test/services/markdown_converter_test.dart +156 -0
- package/skills/flutter-skills/tool/generator/test/services/resource_fetcher_service_test.dart +188 -0
- package/skills/flutter-skills/tool/generator/test/update_skills_test.dart +241 -0
- package/skills/flutter-skills/tool/generator/test/validate_skills_test.dart +728 -0
- package/skills/quality-assurance/bdd-acceptance/SKILL.md +5 -14
- package/skills/quality-assurance/flutter-test/SKILL.md +5 -16
- package/skills/rules/project_rules.md +538 -127
- package/skills/special-tools/env-check/SKILL.md +5 -13
- package/skills/special-tools/ios-full-auto-debug/SKILL.md +5 -15
- package/skills/writing-skills/SKILL.md +654 -0
- package/skills/writing-skills/anthropic-best-practices.md +1149 -0
- package/skills/writing-skills/examples/CLAUDE_MD_TESTING.md +189 -0
- package/skills/writing-skills/graphviz-conventions.dot +172 -0
- package/skills/writing-skills/persuasion-principles.md +187 -0
- package/skills/writing-skills/render-graphs.js +168 -0
- package/skills/writing-skills/testing-skills-with-subagents.md +384 -0
- package/skills/checklist.md +0 -154
- package/skills/rules/user_rules.md +0 -263
- package/skills//345/256/214/346/225/264/345/274/200/345/217/221/346/265/201/347/250/213/346/211/213/345/206/214.md +0 -454
- package/skills//346/212/200/350/203/275/344/275/223/347/263/273/345/256/214/345/226/204/345/273/272/350/256/256.md +0 -308
- package/skills//346/212/200/350/203/275/344/275/277/347/224/250/346/214/207/345/215/227.md +0 -309
- package/skills//346/212/200/350/203/275/345/206/263/347/255/226/346/240/221.md +0 -338
package/bin.js
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
【claude code调用标识:flutter-add-integration-test】【trae调用标识:flutter-add-integration-test+Flutter开发】【流程场景:1.完整3阶段SDD流程、3.小型功能迭代】
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
name: flutter-add-integration-test
|
|
5
|
+
description: Configures Flutter Driver for app interaction and converts MCP actions into permanent integration tests. Use when adding integration testing to a project, exploring UI components via MCP, or automating user flows with the integration_test package.
|
|
6
|
+
metadata:
|
|
7
|
+
model: models/gemini-3.1-pro-preview
|
|
8
|
+
last_modified: Tue, 21 Apr 2026 18:29:20 GMT
|
|
9
|
+
---
|
|
10
|
+
# Implementing Flutter Integration Tests
|
|
11
|
+
|
|
12
|
+
## Contents
|
|
13
|
+
- [Project Setup and Dependencies](#project-setup-and-dependencies)
|
|
14
|
+
- [Interactive Exploration via MCP](#interactive-exploration-via-mcp)
|
|
15
|
+
- [Test Authoring Guidelines](#test-authoring-guidelines)
|
|
16
|
+
- [Execution and Profiling](#execution-and-profiling)
|
|
17
|
+
- [Workflow: End-to-End Integration Testing](#workflow-end-to-end-integration-testing)
|
|
18
|
+
- [Examples](#examples)
|
|
19
|
+
|
|
20
|
+
## Project Setup and Dependencies
|
|
21
|
+
|
|
22
|
+
Configure the project to support integration testing and Flutter Driver extensions.
|
|
23
|
+
|
|
24
|
+
1. Add required development dependencies to `pubspec.yaml`:
|
|
25
|
+
```bash
|
|
26
|
+
flutter pub add 'dev:integration_test:{"sdk":"flutter"}'
|
|
27
|
+
flutter pub add 'dev:flutter_test:{"sdk":"flutter"}'
|
|
28
|
+
```
|
|
29
|
+
2. Enable the Flutter Driver extension in your application entry point (typically `lib/main.dart` or a dedicated `lib/main_test.dart`):
|
|
30
|
+
- Import `package:flutter_driver/driver_extension.dart`.
|
|
31
|
+
- Call `enableFlutterDriverExtension();` before `runApp()`.
|
|
32
|
+
3. Add `Key` parameters (e.g., `ValueKey('login_button')`) to critical widgets in the application code to ensure reliable targeting during tests.
|
|
33
|
+
|
|
34
|
+
## Interactive Exploration via MCP
|
|
35
|
+
|
|
36
|
+
Use the Dart/Flutter MCP server tools to interactively explore and manipulate the application state before writing static tests.
|
|
37
|
+
|
|
38
|
+
- **Launch**: Execute `launch_app` with `target: "lib/main_test.dart"` to start the application and acquire the DTD URI.
|
|
39
|
+
- **Inspect**: Execute `get_widget_tree` to discover available `Key`s, `Text` nodes, and widget `Type`s.
|
|
40
|
+
- **Interact**: Execute `tap`, `enter_text`, and `scroll` to simulate user flows.
|
|
41
|
+
- **Wait**: Always execute `waitFor` or verify state with `get_health` when navigating or triggering animations.
|
|
42
|
+
- **Troubleshoot Unmounted Widgets**: If a widget is not found in the tree, it may be lazily loaded in a `SliverList` or `ListView`. Execute `scroll` or `scrollIntoView` to force the widget to mount before interacting with it.
|
|
43
|
+
|
|
44
|
+
## Test Authoring Guidelines
|
|
45
|
+
|
|
46
|
+
Structure integration tests using the `flutter_test` API paradigm.
|
|
47
|
+
|
|
48
|
+
- Create a dedicated `integration_test/` directory at the project root.
|
|
49
|
+
- Name all test files using the `<name>_test.dart` convention.
|
|
50
|
+
- Initialize the binding by calling `IntegrationTestWidgetsFlutterBinding.ensureInitialized();` at the start of `main()`.
|
|
51
|
+
- Load the application UI using `await tester.pumpWidget(MyApp());`.
|
|
52
|
+
- Trigger frames and wait for animations to complete using `await tester.pumpAndSettle();` after interactions like `tester.tap()`.
|
|
53
|
+
- Assert widget visibility using `expect(find.byKey(ValueKey('foo')), findsOneWidget);` or `findsNothing`.
|
|
54
|
+
- Scroll to specific off-screen widgets using `await tester.scrollUntilVisible(itemFinder, 500.0, scrollable: listFinder);`.
|
|
55
|
+
|
|
56
|
+
**Conditional Logic for Legacy `flutter_driver`:**
|
|
57
|
+
- If maintaining or migrating legacy `flutter_driver` tests, use `driver.waitFor()`, `driver.waitForAbsent()`, `driver.tap()`, and `driver.scroll()` instead of the `WidgetTester` APIs.
|
|
58
|
+
|
|
59
|
+
## Execution and Profiling
|
|
60
|
+
|
|
61
|
+
Execute tests using the `flutter drive` command. Require a host driver script located in `test_driver/integration_test.dart` that calls `integrationDriver()`.
|
|
62
|
+
|
|
63
|
+
**Conditional Execution Targets:**
|
|
64
|
+
- **If testing on Chrome:** Launch `chromedriver --port=4444` in a separate terminal, then run:
|
|
65
|
+
`flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart -d chrome`
|
|
66
|
+
- **If testing headless web:** Run with `-d web-server`.
|
|
67
|
+
- **If testing on Android (Local):** Run `flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart`.
|
|
68
|
+
- **If testing on Firebase Test Lab (Android):**
|
|
69
|
+
1. Build debug APK: `flutter build apk --debug`
|
|
70
|
+
2. Build test APK: `./gradlew app:assembleAndroidTest`
|
|
71
|
+
3. Upload both APKs to the Firebase Test Lab console.
|
|
72
|
+
|
|
73
|
+
## Workflow: End-to-End Integration Testing
|
|
74
|
+
|
|
75
|
+
Copy and follow this checklist to implement and verify integration tests.
|
|
76
|
+
|
|
77
|
+
- [ ] **Task Progress: Setup**
|
|
78
|
+
- [ ] Add `integration_test` and `flutter_test` to `pubspec.yaml`.
|
|
79
|
+
- [ ] Inject `enableFlutterDriverExtension()` into the app entry point.
|
|
80
|
+
- [ ] Assign `ValueKey`s to target widgets.
|
|
81
|
+
- [ ] **Task Progress: Exploration**
|
|
82
|
+
- [ ] Run `launch_app` via MCP.
|
|
83
|
+
- [ ] Map the widget tree using `get_widget_tree`.
|
|
84
|
+
- [ ] Validate interaction paths using MCP tools (`tap`, `enter_text`).
|
|
85
|
+
- [ ] **Task Progress: Authoring**
|
|
86
|
+
- [ ] Create `integration_test/app_test.dart`.
|
|
87
|
+
- [ ] Write test cases using `WidgetTester` APIs.
|
|
88
|
+
- [ ] Create `test_driver/integration_test.dart` with `integrationDriver()`.
|
|
89
|
+
- [ ] **Task Progress: Execution & Feedback Loop**
|
|
90
|
+
- [ ] Run `flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart`.
|
|
91
|
+
- [ ] **Feedback Loop**: Review test output -> If `PumpAndSettleTimedOutException` occurs, check for infinite animations -> If widget not found, add `scrollUntilVisible` -> Re-run test until passing.
|
|
92
|
+
|
|
93
|
+
## Examples
|
|
94
|
+
|
|
95
|
+
### Standard Integration Test (`integration_test/app_test.dart`)
|
|
96
|
+
|
|
97
|
+
```dart
|
|
98
|
+
import 'package:flutter/material.dart';
|
|
99
|
+
import 'package:flutter_test/flutter_test.dart';
|
|
100
|
+
import 'package:integration_test/integration_test.dart';
|
|
101
|
+
import 'package:my_app/main.dart';
|
|
102
|
+
|
|
103
|
+
void main() {
|
|
104
|
+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
|
|
105
|
+
|
|
106
|
+
group('End-to-end test', () {
|
|
107
|
+
testWidgets('tap on the floating action button, verify counter', (tester) async {
|
|
108
|
+
// Load app widget.
|
|
109
|
+
await tester.pumpWidget(const MyApp());
|
|
110
|
+
|
|
111
|
+
// Verify the counter starts at 0.
|
|
112
|
+
expect(find.text('0'), findsOneWidget);
|
|
113
|
+
|
|
114
|
+
// Find the floating action button to tap on.
|
|
115
|
+
final fab = find.byKey(const ValueKey('increment'));
|
|
116
|
+
|
|
117
|
+
// Emulate a tap on the floating action button.
|
|
118
|
+
await tester.tap(fab);
|
|
119
|
+
|
|
120
|
+
// Trigger a frame and wait for animations.
|
|
121
|
+
await tester.pumpAndSettle();
|
|
122
|
+
|
|
123
|
+
// Verify the counter increments by 1.
|
|
124
|
+
expect(find.text('1'), findsOneWidget);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Host Driver Script (`test_driver/integration_test.dart`)
|
|
131
|
+
|
|
132
|
+
```dart
|
|
133
|
+
import 'package:integration_test/integration_test_driver.dart';
|
|
134
|
+
|
|
135
|
+
Future<void> main() => integrationDriver();
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Performance Profiling Driver Script (`test_driver/perf_driver.dart`)
|
|
139
|
+
|
|
140
|
+
Use this driver script if you wrap your test actions in `binding.traceAction()` to capture performance metrics.
|
|
141
|
+
|
|
142
|
+
```dart
|
|
143
|
+
import 'package:flutter_driver/flutter_driver.dart' as driver;
|
|
144
|
+
import 'package:integration_test/integration_test_driver.dart';
|
|
145
|
+
|
|
146
|
+
Future<void> main() {
|
|
147
|
+
return integrationDriver(
|
|
148
|
+
responseDataCallback: (data) async {
|
|
149
|
+
if (data != null) {
|
|
150
|
+
final timeline = driver.Timeline.fromJson(
|
|
151
|
+
data['scrolling_timeline'] as Map<String, dynamic>,
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
final summary = driver.TimelineSummary.summarize(timeline);
|
|
155
|
+
|
|
156
|
+
await summary.writeTimelineToFile(
|
|
157
|
+
'scrolling_timeline',
|
|
158
|
+
pretty: true,
|
|
159
|
+
includeSummary: true,
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
```
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
【claude code调用标识:flutter-add-widget-preview】【trae调用标识:flutter-add-widget-preview+Flutter开发】【流程场景:1.完整3阶段SDD流程、3.小型功能迭代】
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
name: flutter-add-widget-preview
|
|
5
|
+
description: Adds interactive widget previews to the project using the previews.dart system. Use when creating new UI components or updating existing screens to ensure consistent design and interactive testing.
|
|
6
|
+
metadata:
|
|
7
|
+
model: models/gemini-3.1-pro-preview
|
|
8
|
+
last_modified: Tue, 21 Apr 2026 20:05:23 GMT
|
|
9
|
+
---
|
|
10
|
+
# Previewing Flutter Widgets
|
|
11
|
+
|
|
12
|
+
## Contents
|
|
13
|
+
- [Preview Guidelines](#preview-guidelines)
|
|
14
|
+
- [Handling Limitations](#handling-limitations)
|
|
15
|
+
- [Workflows](#workflows)
|
|
16
|
+
- [Examples](#examples)
|
|
17
|
+
|
|
18
|
+
## Preview Guidelines
|
|
19
|
+
|
|
20
|
+
Use the Flutter Widget Previewer to render widgets in real-time, isolated from the full application context.
|
|
21
|
+
|
|
22
|
+
- **Target Elements:** Apply the `@Preview` annotation to top-level functions, static methods within a class, or public widget constructors/factories that have no required arguments and return a `Widget` or `WidgetBuilder`.
|
|
23
|
+
- **Imports:** Always import `package:flutter/widget_previews.dart` to access the preview annotations.
|
|
24
|
+
- **Custom Annotations:** Extend the `Preview` class to create custom annotations that inject common properties (e.g., themes, wrappers) across multiple widgets.
|
|
25
|
+
- **Multiple Configurations:** Apply multiple `@Preview` annotations to a single target to generate multiple preview instances. Alternatively, extend `MultiPreview` to encapsulate common multi-preview configurations.
|
|
26
|
+
- **Runtime Transformations:** Override the `transform()` method in custom `Preview` or `MultiPreview` classes to modify preview configurations dynamically at runtime (e.g., generating names based on dynamic values, which is impossible in a `const` context).
|
|
27
|
+
|
|
28
|
+
## Handling Limitations
|
|
29
|
+
|
|
30
|
+
Adhere to the following constraints when authoring previewable widgets, as the Widget Previewer runs in a web environment:
|
|
31
|
+
|
|
32
|
+
- **No Native APIs:** Do not use native plugins or APIs from `dart:io` or `dart:ffi`. Widgets with transitive dependencies on `dart:io` or `dart:ffi` will throw exceptions upon invocation. Use conditional imports to mock or bypass these in preview mode.
|
|
33
|
+
- **Asset Paths:** Use package-based paths for assets loaded via `dart:ui` `fromAsset` APIs (e.g., `packages/my_package_name/assets/my_image.png` instead of `assets/my_image.png`).
|
|
34
|
+
- **Public Callbacks:** Ensure all callback arguments provided to preview annotations are public and constant to satisfy code generation requirements.
|
|
35
|
+
- **Constraints:** Apply explicit constraints using the `size` parameter in the `@Preview` annotation if your widget is unconstrained, as the previewer defaults to constraining them to approximately half the viewport.
|
|
36
|
+
|
|
37
|
+
## Workflows
|
|
38
|
+
|
|
39
|
+
### Creating a Widget Preview
|
|
40
|
+
Copy and track this checklist when implementing a new widget preview:
|
|
41
|
+
|
|
42
|
+
- [ ] Import `package:flutter/widget_previews.dart`.
|
|
43
|
+
- [ ] Identify a valid target (top-level function, static method, or parameter-less public constructor).
|
|
44
|
+
- [ ] Apply the `@Preview` annotation to the target.
|
|
45
|
+
- [ ] Configure preview parameters (`name`, `group`, `size`, `theme`, `brightness`, etc.) as needed.
|
|
46
|
+
- [ ] If applying the same configuration to multiple widgets, extract the configuration into a custom class extending `Preview`.
|
|
47
|
+
|
|
48
|
+
### Interacting with Previews
|
|
49
|
+
Follow the appropriate conditional workflow to launch and interact with the Widget Previewer:
|
|
50
|
+
|
|
51
|
+
**If using a supported IDE (Android Studio, IntelliJ, VS Code with Flutter 3.38+):**
|
|
52
|
+
1. Launch the IDE. The Widget Previewer starts automatically.
|
|
53
|
+
2. Open the "Flutter Widget Preview" tab in the sidebar.
|
|
54
|
+
3. Toggle "Filter previews by selected file" at the bottom left if you want to view previews outside the currently active file.
|
|
55
|
+
|
|
56
|
+
**If using the Command Line:**
|
|
57
|
+
1. Navigate to the Flutter project's root directory.
|
|
58
|
+
2. Run `flutter widget-preview start`.
|
|
59
|
+
3. View the automatically opened Chrome environment.
|
|
60
|
+
|
|
61
|
+
**Feedback Loop: Preview Iteration**
|
|
62
|
+
1. Modify the widget code or preview configuration.
|
|
63
|
+
2. Observe the automatic update in the Widget Previewer.
|
|
64
|
+
3. If global state (e.g., static initializers) was modified: Click the global hot restart button at the bottom right.
|
|
65
|
+
4. If only the local widget state needs resetting: Click the individual hot restart button on the specific preview card.
|
|
66
|
+
5. Review errors in the IDE/CLI console -> fix -> repeat.
|
|
67
|
+
|
|
68
|
+
## Examples
|
|
69
|
+
|
|
70
|
+
### Basic Preview
|
|
71
|
+
```dart
|
|
72
|
+
import 'package:flutter/widget_previews.dart';
|
|
73
|
+
import 'package:flutter/material.dart';
|
|
74
|
+
|
|
75
|
+
@Preview(name: 'My Sample Text', group: 'Typography')
|
|
76
|
+
Widget mySampleText() {
|
|
77
|
+
return const Text('Hello, World!');
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Custom Preview with Runtime Transformation
|
|
82
|
+
```dart
|
|
83
|
+
import 'package:flutter/widget_previews.dart';
|
|
84
|
+
import 'package:flutter/material.dart';
|
|
85
|
+
|
|
86
|
+
final class TransformativePreview extends Preview {
|
|
87
|
+
const TransformativePreview({
|
|
88
|
+
super.name,
|
|
89
|
+
super.group,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
PreviewThemeData _themeBuilder() {
|
|
93
|
+
return PreviewThemeData(
|
|
94
|
+
materialLight: ThemeData.light(),
|
|
95
|
+
materialDark: ThemeData.dark(),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
@override
|
|
100
|
+
Preview transform() {
|
|
101
|
+
final originalPreview = super.transform();
|
|
102
|
+
final builder = originalPreview.toBuilder();
|
|
103
|
+
|
|
104
|
+
builder
|
|
105
|
+
..name = 'Transformed - ${originalPreview.name}'
|
|
106
|
+
..theme = _themeBuilder;
|
|
107
|
+
|
|
108
|
+
return builder.toPreview();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@TransformativePreview(name: 'Custom Themed Button')
|
|
113
|
+
Widget myButton() => const ElevatedButton(onPressed: null, child: Text('Click'));
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### MultiPreview Implementation
|
|
117
|
+
```dart
|
|
118
|
+
import 'package:flutter/widget_previews.dart';
|
|
119
|
+
import 'package:flutter/material.dart';
|
|
120
|
+
|
|
121
|
+
/// Creates light and dark mode previews automatically.
|
|
122
|
+
final class MultiBrightnessPreview extends MultiPreview {
|
|
123
|
+
const MultiBrightnessPreview({required this.name});
|
|
124
|
+
|
|
125
|
+
final String name;
|
|
126
|
+
|
|
127
|
+
@override
|
|
128
|
+
List<Preview> get previews => const [
|
|
129
|
+
Preview(brightness: Brightness.light),
|
|
130
|
+
Preview(brightness: Brightness.dark),
|
|
131
|
+
];
|
|
132
|
+
|
|
133
|
+
@override
|
|
134
|
+
List<Preview> transform() {
|
|
135
|
+
final previews = super.transform();
|
|
136
|
+
return previews.map((preview) {
|
|
137
|
+
final builder = preview.toBuilder()
|
|
138
|
+
..group = 'Brightness'
|
|
139
|
+
..name = '$name - ${preview.brightness!.name}';
|
|
140
|
+
return builder.toPreview();
|
|
141
|
+
}).toList();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
@MultiBrightnessPreview(name: 'Primary Card')
|
|
146
|
+
Widget cardPreview() => const Card(child: Padding(padding: EdgeInsets.all(8.0), child: Text('Content')));
|
|
147
|
+
```
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
【claude code调用标识:flutter-add-widget-test】【trae调用标识:flutter-add-widget-test+Flutter开发】【流程场景:1.完整3阶段SDD流程、3.小型功能迭代、4.Bug处理】
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
name: flutter-add-widget-test
|
|
5
|
+
description: Implement a component-level test using `WidgetTester` to verify UI rendering and user interactions (tapping, scrolling, entering text). Use when validating that a specific widget displays correct data and responds to events as expected.
|
|
6
|
+
metadata:
|
|
7
|
+
model: models/gemini-3.1-pro-preview
|
|
8
|
+
last_modified: Tue, 21 Apr 2026 21:15:41 GMT
|
|
9
|
+
---
|
|
10
|
+
# Writing Flutter Widget Tests
|
|
11
|
+
|
|
12
|
+
## Contents
|
|
13
|
+
- [Setup & Configuration](#setup--configuration)
|
|
14
|
+
- [Core Components](#core-components)
|
|
15
|
+
- [Workflow: Implementing a Widget Test](#workflow-implementing-a-widget-test)
|
|
16
|
+
- [Interaction & State Management](#interaction--state-management)
|
|
17
|
+
- [Examples](#examples)
|
|
18
|
+
|
|
19
|
+
## Setup & Configuration
|
|
20
|
+
|
|
21
|
+
Ensure the testing environment is properly configured before authoring widget tests.
|
|
22
|
+
|
|
23
|
+
1. Add the `flutter_test` dependency to the `dev_dependencies` section of `pubspec.yaml`.
|
|
24
|
+
2. Place all test files in the `test/` directory at the root of the project.
|
|
25
|
+
3. Suffix all test file names with `_test.dart` (e.g., `widget_test.dart`).
|
|
26
|
+
|
|
27
|
+
## Core Components
|
|
28
|
+
|
|
29
|
+
Utilize the following `flutter_test` components to interact with and validate the widget tree:
|
|
30
|
+
|
|
31
|
+
* **`WidgetTester`**: The primary interface for building and interacting with widgets in the test environment. Provided automatically by the `testWidgets()` function.
|
|
32
|
+
* **`Finder`**: Locates widgets in the test environment (e.g., `find.text('Submit')`, `find.byType(TextField)`, `find.byKey(Key('submit_btn'))`).
|
|
33
|
+
* **`Matcher`**: Verifies the presence or state of widgets located by a `Finder` (e.g., `findsOneWidget`, `findsNothing`, `findsNWidgets(2)`, `matchesGoldenFile`).
|
|
34
|
+
|
|
35
|
+
## Workflow: Implementing a Widget Test
|
|
36
|
+
|
|
37
|
+
Copy the following checklist to track progress when implementing a new widget test.
|
|
38
|
+
|
|
39
|
+
### Task Progress
|
|
40
|
+
- [ ] **Step 1: Define the test.** Use `testWidgets('description', (WidgetTester tester) async { ... })`.
|
|
41
|
+
- [ ] **Step 2: Build the widget.** Call `await tester.pumpWidget(MyWidget())` to render the UI. Wrap the widget in a `MaterialApp` or `Directionality` widget if it requires inherited directional or theme data.
|
|
42
|
+
- [ ] **Step 3: Locate elements.** Instantiate `Finder` objects for the target widgets.
|
|
43
|
+
- [ ] **Step 4: Verify initial state.** Use `expect(finder, matcher)` to validate the initial render.
|
|
44
|
+
- [ ] **Step 5: Simulate interactions.** Execute gestures or inputs (e.g., `await tester.tap(buttonFinder)`).
|
|
45
|
+
- [ ] **Step 6: Rebuild the tree.** Call `await tester.pump()` or `await tester.pumpAndSettle()` to process state changes.
|
|
46
|
+
- [ ] **Step 7: Verify updated state.** Use `expect()` to validate the UI after the interaction.
|
|
47
|
+
- [ ] **Step 8: Run and validate.** Execute `flutter test test/your_test_file_test.dart`.
|
|
48
|
+
- [ ] **Step 9: Feedback Loop.** Review test output -> identify failing matchers -> adjust widget logic or test assertions -> re-run until passing.
|
|
49
|
+
|
|
50
|
+
## Interaction & State Management
|
|
51
|
+
|
|
52
|
+
Apply the following conditional logic based on the type of interaction or state change being tested:
|
|
53
|
+
|
|
54
|
+
* **If testing static rendering:** Call `await tester.pumpWidget()` once, then immediately run `expect()` assertions.
|
|
55
|
+
* **If testing standard state changes (e.g., button taps):**
|
|
56
|
+
1. Call `await tester.tap(finder)`.
|
|
57
|
+
2. Call `await tester.pump()` to trigger a single frame rebuild.
|
|
58
|
+
* **If testing animations, transitions, or asynchronous UI updates:**
|
|
59
|
+
1. Trigger the action (e.g., `await tester.drag(finder, Offset(500, 0))`).
|
|
60
|
+
2. Call `await tester.pumpAndSettle()` to repeatedly pump frames until no more frames are scheduled (animation completes).
|
|
61
|
+
* **If testing text input:** Call `await tester.enterText(textFieldFinder, 'Input string')`.
|
|
62
|
+
* **If testing items in a dynamic or long list:** Call `await tester.scrollUntilVisible(itemFinder, 500.0, scrollable: listFinder)` to ensure the target widget is rendered before interacting with it.
|
|
63
|
+
|
|
64
|
+
## Examples
|
|
65
|
+
|
|
66
|
+
### High-Fidelity Widget Test Implementation
|
|
67
|
+
|
|
68
|
+
**Target Widget (`lib/todo_list.dart`):**
|
|
69
|
+
```dart
|
|
70
|
+
import 'package:flutter/material.dart';
|
|
71
|
+
|
|
72
|
+
class TodoList extends StatefulWidget {
|
|
73
|
+
const TodoList({super.key});
|
|
74
|
+
|
|
75
|
+
@override
|
|
76
|
+
State<TodoList> createState() => _TodoListState();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
class _TodoListState extends State<TodoList> {
|
|
80
|
+
final todos = <String>[];
|
|
81
|
+
final controller = TextEditingController();
|
|
82
|
+
|
|
83
|
+
@override
|
|
84
|
+
Widget build(BuildContext context) {
|
|
85
|
+
return MaterialApp(
|
|
86
|
+
home: Scaffold(
|
|
87
|
+
body: Column(
|
|
88
|
+
children: [
|
|
89
|
+
TextField(controller: controller),
|
|
90
|
+
Expanded(
|
|
91
|
+
child: ListView.builder(
|
|
92
|
+
itemCount: todos.length,
|
|
93
|
+
itemBuilder: (context, index) {
|
|
94
|
+
final todo = todos[index];
|
|
95
|
+
return Dismissible(
|
|
96
|
+
key: Key('$todo$index'),
|
|
97
|
+
onDismissed: (_) => setState(() => todos.removeAt(index)),
|
|
98
|
+
child: ListTile(title: Text(todo)),
|
|
99
|
+
);
|
|
100
|
+
},
|
|
101
|
+
),
|
|
102
|
+
),
|
|
103
|
+
],
|
|
104
|
+
),
|
|
105
|
+
floatingActionButton: FloatingActionButton(
|
|
106
|
+
onPressed: () {
|
|
107
|
+
setState(() {
|
|
108
|
+
todos.add(controller.text);
|
|
109
|
+
controller.clear();
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
child: const Icon(Icons.add),
|
|
113
|
+
),
|
|
114
|
+
),
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
**Test Implementation (`test/todo_list_test.dart`):**
|
|
121
|
+
```dart
|
|
122
|
+
import 'package:flutter/material.dart';
|
|
123
|
+
import 'package:flutter_test/flutter_test.dart';
|
|
124
|
+
import 'package:my_app/todo_list.dart';
|
|
125
|
+
|
|
126
|
+
void main() {
|
|
127
|
+
testWidgets('Add and remove a todo item', (WidgetTester tester) async {
|
|
128
|
+
// 1. Build the widget
|
|
129
|
+
await tester.pumpWidget(const TodoList());
|
|
130
|
+
|
|
131
|
+
// 2. Verify initial state
|
|
132
|
+
expect(find.byType(ListTile), findsNothing);
|
|
133
|
+
|
|
134
|
+
// 3. Enter text into the TextField
|
|
135
|
+
await tester.enterText(find.byType(TextField), 'Buy groceries');
|
|
136
|
+
|
|
137
|
+
// 4. Tap the add button
|
|
138
|
+
await tester.tap(find.byType(FloatingActionButton));
|
|
139
|
+
|
|
140
|
+
// 5. Rebuild the widget to reflect the new state
|
|
141
|
+
await tester.pump();
|
|
142
|
+
|
|
143
|
+
// 6. Verify the item was added
|
|
144
|
+
expect(find.text('Buy groceries'), findsOneWidget);
|
|
145
|
+
|
|
146
|
+
// 7. Swipe the item to dismiss it
|
|
147
|
+
await tester.drag(find.byType(Dismissible), const Offset(500, 0));
|
|
148
|
+
|
|
149
|
+
// 8. Build the widget until the dismiss animation ends
|
|
150
|
+
await tester.pumpAndSettle();
|
|
151
|
+
|
|
152
|
+
// 9. Verify the item was removed
|
|
153
|
+
expect(find.text('Buy groceries'), findsNothing);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
```
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
【claude code调用标识:flutter-apply-architecture-best-practices】【trae调用标识:flutter-apply-architecture-best-practices+Flutter开发】【流程场景:1.完整3阶段SDD流程、2.从零开始新项目】
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
name: flutter-apply-architecture-best-practices
|
|
5
|
+
description: Architects a Flutter application using the recommended layered approach (UI, Logic, Data). Use when structuring a new project or refactoring for scalability.
|
|
6
|
+
metadata:
|
|
7
|
+
model: models/gemini-3.1-pro-preview
|
|
8
|
+
last_modified: Tue, 21 Apr 2026 20:11:20 GMT
|
|
9
|
+
---
|
|
10
|
+
# Architecting Flutter Applications
|
|
11
|
+
|
|
12
|
+
## Contents
|
|
13
|
+
- [Architectural Layers](#architectural-layers)
|
|
14
|
+
- [Project Structure](#project-structure)
|
|
15
|
+
- [Workflow: Implementing a New Feature](#workflow-implementing-a-new-feature)
|
|
16
|
+
- [Examples](#examples)
|
|
17
|
+
|
|
18
|
+
## Architectural Layers
|
|
19
|
+
|
|
20
|
+
Enforce strict Separation of Concerns by dividing the application into distinct layers. Never mix UI rendering with business logic or data fetching.
|
|
21
|
+
|
|
22
|
+
### UI Layer (Presentation)
|
|
23
|
+
Implement the MVVM (Model-View-ViewModel) pattern to manage UI state and logic.
|
|
24
|
+
* **Views:** Write reusable, lean widgets. Restrict logic in Views to UI-specific operations (e.g., animations, layout constraints, simple routing). Pass all required data from the ViewModel.
|
|
25
|
+
* **ViewModels:** Manage UI state and handle user interactions. Extend `ChangeNotifier` (or use `Listenable`) to expose state. Expose immutable state snapshots to the View. Inject Repositories into ViewModels via the constructor.
|
|
26
|
+
|
|
27
|
+
### Data Layer
|
|
28
|
+
Implement the Repository pattern to isolate data access logic and create a single source of truth.
|
|
29
|
+
* **Services:** Create stateless classes to wrap external APIs (HTTP clients, local databases, platform plugins). Return raw API models or `Result` wrappers.
|
|
30
|
+
* **Repositories:** Consume one or more Services. Transform raw API models into clean Domain Models. Handle caching, offline synchronization, and retry logic. Expose Domain Models to ViewModels.
|
|
31
|
+
|
|
32
|
+
### Logic Layer (Domain - Optional)
|
|
33
|
+
* **Use Cases:** Implement this layer only if the application contains complex business logic that clutters the ViewModel, or if logic must be reused across multiple ViewModels. Extract this logic into dedicated Use Case (interactor) classes that sit between ViewModels and Repositories.
|
|
34
|
+
|
|
35
|
+
## Project Structure
|
|
36
|
+
|
|
37
|
+
Organize the codebase using a hybrid approach: group UI components by feature, and group Data/Domain components by type.
|
|
38
|
+
|
|
39
|
+
```text
|
|
40
|
+
lib/
|
|
41
|
+
├── data/
|
|
42
|
+
│ ├── models/ # API models
|
|
43
|
+
│ ├── repositories/ # Repository implementations
|
|
44
|
+
│ └── services/ # API clients, local storage wrappers
|
|
45
|
+
├── domain/
|
|
46
|
+
│ ├── models/ # Clean domain models
|
|
47
|
+
│ └── use_cases/ # Optional business logic classes
|
|
48
|
+
└── ui/
|
|
49
|
+
├── core/ # Shared widgets, themes, typography
|
|
50
|
+
└── features/
|
|
51
|
+
└── [feature_name]/
|
|
52
|
+
├── view_models/
|
|
53
|
+
└── views/
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Workflow: Implementing a New Feature
|
|
57
|
+
|
|
58
|
+
Follow this sequential workflow when adding a new feature to the application. Copy the checklist to track progress.
|
|
59
|
+
|
|
60
|
+
### Task Progress
|
|
61
|
+
- [ ] **Step 1: Define Domain Models.** Create immutable data classes for the feature using `freezed` or `built_value`.
|
|
62
|
+
- [ ] **Step 2: Implement Services.** Create or update Service classes to handle external API communication.
|
|
63
|
+
- [ ] **Step 3: Implement Repositories.** Create the Repository to consume Services and return Domain Models.
|
|
64
|
+
- [ ] **Step 4: Apply Conditional Logic (Domain Layer).**
|
|
65
|
+
- *If the feature requires complex data transformation or cross-repository logic:* Create a Use Case class.
|
|
66
|
+
- *If the feature is a simple CRUD operation:* Skip to Step 5.
|
|
67
|
+
- [ ] **Step 5: Implement the ViewModel.** Create the ViewModel extending `ChangeNotifier`. Inject required Repositories/Use Cases. Expose immutable state and command methods.
|
|
68
|
+
- [ ] **Step 6: Implement the View.** Create the UI widget. Use `ListenableBuilder` or `AnimatedBuilder` to listen to ViewModel changes.
|
|
69
|
+
- [ ] **Step 7: Inject Dependencies.** Register the new Service, Repository, and ViewModel in the dependency injection container (e.g., `provider` or `get_it`).
|
|
70
|
+
- [ ] **Step 8: Run Validator.** Execute unit tests for the ViewModel and Repository.
|
|
71
|
+
- *Feedback Loop:* Run tests -> Review failures -> Fix logic -> Re-run until passing.
|
|
72
|
+
|
|
73
|
+
## Examples
|
|
74
|
+
|
|
75
|
+
### Data Layer: Service and Repository
|
|
76
|
+
|
|
77
|
+
```dart
|
|
78
|
+
// 1. Service (Raw API interaction)
|
|
79
|
+
class ApiClient {
|
|
80
|
+
Future<UserApiModel> fetchUser(String id) async {
|
|
81
|
+
// HTTP GET implementation...
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 2. Repository (Single source of truth, returns Domain Model)
|
|
86
|
+
class UserRepository {
|
|
87
|
+
UserRepository({required ApiClient apiClient}) : _apiClient = apiClient;
|
|
88
|
+
|
|
89
|
+
final ApiClient _apiClient;
|
|
90
|
+
User? _cachedUser;
|
|
91
|
+
|
|
92
|
+
Future<User> getUser(String id) async {
|
|
93
|
+
if (_cachedUser != null) return _cachedUser!;
|
|
94
|
+
|
|
95
|
+
final apiModel = await _apiClient.fetchUser(id);
|
|
96
|
+
_cachedUser = User(id: apiModel.id, name: apiModel.fullName); // Transform to Domain Model
|
|
97
|
+
return _cachedUser!;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### UI Layer: ViewModel and View
|
|
103
|
+
|
|
104
|
+
```dart
|
|
105
|
+
// 3. ViewModel (State management and presentation logic)
|
|
106
|
+
class ProfileViewModel extends ChangeNotifier {
|
|
107
|
+
ProfileViewModel({required UserRepository userRepository})
|
|
108
|
+
: _userRepository = userRepository;
|
|
109
|
+
|
|
110
|
+
final UserRepository _userRepository;
|
|
111
|
+
|
|
112
|
+
User? _user;
|
|
113
|
+
User? get user => _user;
|
|
114
|
+
|
|
115
|
+
bool _isLoading = false;
|
|
116
|
+
bool get isLoading => _isLoading;
|
|
117
|
+
|
|
118
|
+
Future<void> loadProfile(String id) async {
|
|
119
|
+
_isLoading = true;
|
|
120
|
+
notifyListeners();
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
_user = await _userRepository.getUser(id);
|
|
124
|
+
} finally {
|
|
125
|
+
_isLoading = false;
|
|
126
|
+
notifyListeners();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// 4. View (Dumb UI component)
|
|
132
|
+
class ProfileView extends StatelessWidget {
|
|
133
|
+
const ProfileView({super.key, required this.viewModel});
|
|
134
|
+
|
|
135
|
+
final ProfileViewModel viewModel;
|
|
136
|
+
|
|
137
|
+
@override
|
|
138
|
+
Widget build(BuildContext context) {
|
|
139
|
+
return ListenableBuilder(
|
|
140
|
+
listenable: viewModel,
|
|
141
|
+
builder: (context, _) {
|
|
142
|
+
if (viewModel.isLoading) {
|
|
143
|
+
return const Center(child: CircularProgressIndicator());
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
final user = viewModel.user;
|
|
147
|
+
if (user == null) {
|
|
148
|
+
return const Center(child: Text('User not found'));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return Column(
|
|
152
|
+
children: [
|
|
153
|
+
Text(user.name),
|
|
154
|
+
ElevatedButton(
|
|
155
|
+
onPressed: () => viewModel.loadProfile(user.id),
|
|
156
|
+
child: const Text('Refresh'),
|
|
157
|
+
),
|
|
158
|
+
],
|
|
159
|
+
);
|
|
160
|
+
},
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
```
|