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
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: flutter-add-widget-preview
|
|
3
|
+
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.
|
|
4
|
+
metadata:
|
|
5
|
+
model: models/gemini-3.1-pro-preview
|
|
6
|
+
last_modified: Tue, 21 Apr 2026 20:05:23 GMT
|
|
7
|
+
---
|
|
8
|
+
# Previewing Flutter Widgets
|
|
9
|
+
|
|
10
|
+
## Contents
|
|
11
|
+
- [Preview Guidelines](#preview-guidelines)
|
|
12
|
+
- [Handling Limitations](#handling-limitations)
|
|
13
|
+
- [Workflows](#workflows)
|
|
14
|
+
- [Examples](#examples)
|
|
15
|
+
|
|
16
|
+
## Preview Guidelines
|
|
17
|
+
|
|
18
|
+
Use the Flutter Widget Previewer to render widgets in real-time, isolated from the full application context.
|
|
19
|
+
|
|
20
|
+
- **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`.
|
|
21
|
+
- **Imports:** Always import `package:flutter/widget_previews.dart` to access the preview annotations.
|
|
22
|
+
- **Custom Annotations:** Extend the `Preview` class to create custom annotations that inject common properties (e.g., themes, wrappers) across multiple widgets.
|
|
23
|
+
- **Multiple Configurations:** Apply multiple `@Preview` annotations to a single target to generate multiple preview instances. Alternatively, extend `MultiPreview` to encapsulate common multi-preview configurations.
|
|
24
|
+
- **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).
|
|
25
|
+
|
|
26
|
+
## Handling Limitations
|
|
27
|
+
|
|
28
|
+
Adhere to the following constraints when authoring previewable widgets, as the Widget Previewer runs in a web environment:
|
|
29
|
+
|
|
30
|
+
- **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.
|
|
31
|
+
- **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`).
|
|
32
|
+
- **Public Callbacks:** Ensure all callback arguments provided to preview annotations are public and constant to satisfy code generation requirements.
|
|
33
|
+
- **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.
|
|
34
|
+
|
|
35
|
+
## Workflows
|
|
36
|
+
|
|
37
|
+
### Creating a Widget Preview
|
|
38
|
+
Copy and track this checklist when implementing a new widget preview:
|
|
39
|
+
|
|
40
|
+
- [ ] Import `package:flutter/widget_previews.dart`.
|
|
41
|
+
- [ ] Identify a valid target (top-level function, static method, or parameter-less public constructor).
|
|
42
|
+
- [ ] Apply the `@Preview` annotation to the target.
|
|
43
|
+
- [ ] Configure preview parameters (`name`, `group`, `size`, `theme`, `brightness`, etc.) as needed.
|
|
44
|
+
- [ ] If applying the same configuration to multiple widgets, extract the configuration into a custom class extending `Preview`.
|
|
45
|
+
|
|
46
|
+
### Interacting with Previews
|
|
47
|
+
Follow the appropriate conditional workflow to launch and interact with the Widget Previewer:
|
|
48
|
+
|
|
49
|
+
**If using a supported IDE (Android Studio, IntelliJ, VS Code with Flutter 3.38+):**
|
|
50
|
+
1. Launch the IDE. The Widget Previewer starts automatically.
|
|
51
|
+
2. Open the "Flutter Widget Preview" tab in the sidebar.
|
|
52
|
+
3. Toggle "Filter previews by selected file" at the bottom left if you want to view previews outside the currently active file.
|
|
53
|
+
|
|
54
|
+
**If using the Command Line:**
|
|
55
|
+
1. Navigate to the Flutter project's root directory.
|
|
56
|
+
2. Run `flutter widget-preview start`.
|
|
57
|
+
3. View the automatically opened Chrome environment.
|
|
58
|
+
|
|
59
|
+
**Feedback Loop: Preview Iteration**
|
|
60
|
+
1. Modify the widget code or preview configuration.
|
|
61
|
+
2. Observe the automatic update in the Widget Previewer.
|
|
62
|
+
3. If global state (e.g., static initializers) was modified: Click the global hot restart button at the bottom right.
|
|
63
|
+
4. If only the local widget state needs resetting: Click the individual hot restart button on the specific preview card.
|
|
64
|
+
5. Review errors in the IDE/CLI console -> fix -> repeat.
|
|
65
|
+
|
|
66
|
+
## Examples
|
|
67
|
+
|
|
68
|
+
### Basic Preview
|
|
69
|
+
```dart
|
|
70
|
+
import 'package:flutter/widget_previews.dart';
|
|
71
|
+
import 'package:flutter/material.dart';
|
|
72
|
+
|
|
73
|
+
@Preview(name: 'My Sample Text', group: 'Typography')
|
|
74
|
+
Widget mySampleText() {
|
|
75
|
+
return const Text('Hello, World!');
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Custom Preview with Runtime Transformation
|
|
80
|
+
```dart
|
|
81
|
+
import 'package:flutter/widget_previews.dart';
|
|
82
|
+
import 'package:flutter/material.dart';
|
|
83
|
+
|
|
84
|
+
final class TransformativePreview extends Preview {
|
|
85
|
+
const TransformativePreview({
|
|
86
|
+
super.name,
|
|
87
|
+
super.group,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
PreviewThemeData _themeBuilder() {
|
|
91
|
+
return PreviewThemeData(
|
|
92
|
+
materialLight: ThemeData.light(),
|
|
93
|
+
materialDark: ThemeData.dark(),
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@override
|
|
98
|
+
Preview transform() {
|
|
99
|
+
final originalPreview = super.transform();
|
|
100
|
+
final builder = originalPreview.toBuilder();
|
|
101
|
+
|
|
102
|
+
builder
|
|
103
|
+
..name = 'Transformed - ${originalPreview.name}'
|
|
104
|
+
..theme = _themeBuilder;
|
|
105
|
+
|
|
106
|
+
return builder.toPreview();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@TransformativePreview(name: 'Custom Themed Button')
|
|
111
|
+
Widget myButton() => const ElevatedButton(onPressed: null, child: Text('Click'));
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### MultiPreview Implementation
|
|
115
|
+
```dart
|
|
116
|
+
import 'package:flutter/widget_previews.dart';
|
|
117
|
+
import 'package:flutter/material.dart';
|
|
118
|
+
|
|
119
|
+
/// Creates light and dark mode previews automatically.
|
|
120
|
+
final class MultiBrightnessPreview extends MultiPreview {
|
|
121
|
+
const MultiBrightnessPreview({required this.name});
|
|
122
|
+
|
|
123
|
+
final String name;
|
|
124
|
+
|
|
125
|
+
@override
|
|
126
|
+
List<Preview> get previews => const [
|
|
127
|
+
Preview(brightness: Brightness.light),
|
|
128
|
+
Preview(brightness: Brightness.dark),
|
|
129
|
+
];
|
|
130
|
+
|
|
131
|
+
@override
|
|
132
|
+
List<Preview> transform() {
|
|
133
|
+
final previews = super.transform();
|
|
134
|
+
return previews.map((preview) {
|
|
135
|
+
final builder = preview.toBuilder()
|
|
136
|
+
..group = 'Brightness'
|
|
137
|
+
..name = '$name - ${preview.brightness!.name}';
|
|
138
|
+
return builder.toPreview();
|
|
139
|
+
}).toList();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
@MultiBrightnessPreview(name: 'Primary Card')
|
|
144
|
+
Widget cardPreview() => const Card(child: Padding(padding: EdgeInsets.all(8.0), child: Text('Content')));
|
|
145
|
+
```
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: flutter-add-widget-test
|
|
3
|
+
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.
|
|
4
|
+
metadata:
|
|
5
|
+
model: models/gemini-3.1-pro-preview
|
|
6
|
+
last_modified: Tue, 21 Apr 2026 21:15:41 GMT
|
|
7
|
+
---
|
|
8
|
+
# Writing Flutter Widget Tests
|
|
9
|
+
|
|
10
|
+
## Contents
|
|
11
|
+
- [Setup & Configuration](#setup--configuration)
|
|
12
|
+
- [Core Components](#core-components)
|
|
13
|
+
- [Workflow: Implementing a Widget Test](#workflow-implementing-a-widget-test)
|
|
14
|
+
- [Interaction & State Management](#interaction--state-management)
|
|
15
|
+
- [Examples](#examples)
|
|
16
|
+
|
|
17
|
+
## Setup & Configuration
|
|
18
|
+
|
|
19
|
+
Ensure the testing environment is properly configured before authoring widget tests.
|
|
20
|
+
|
|
21
|
+
1. Add the `flutter_test` dependency to the `dev_dependencies` section of `pubspec.yaml`.
|
|
22
|
+
2. Place all test files in the `test/` directory at the root of the project.
|
|
23
|
+
3. Suffix all test file names with `_test.dart` (e.g., `widget_test.dart`).
|
|
24
|
+
|
|
25
|
+
## Core Components
|
|
26
|
+
|
|
27
|
+
Utilize the following `flutter_test` components to interact with and validate the widget tree:
|
|
28
|
+
|
|
29
|
+
* **`WidgetTester`**: The primary interface for building and interacting with widgets in the test environment. Provided automatically by the `testWidgets()` function.
|
|
30
|
+
* **`Finder`**: Locates widgets in the test environment (e.g., `find.text('Submit')`, `find.byType(TextField)`, `find.byKey(Key('submit_btn'))`).
|
|
31
|
+
* **`Matcher`**: Verifies the presence or state of widgets located by a `Finder` (e.g., `findsOneWidget`, `findsNothing`, `findsNWidgets(2)`, `matchesGoldenFile`).
|
|
32
|
+
|
|
33
|
+
## Workflow: Implementing a Widget Test
|
|
34
|
+
|
|
35
|
+
Copy the following checklist to track progress when implementing a new widget test.
|
|
36
|
+
|
|
37
|
+
### Task Progress
|
|
38
|
+
- [ ] **Step 1: Define the test.** Use `testWidgets('description', (WidgetTester tester) async { ... })`.
|
|
39
|
+
- [ ] **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.
|
|
40
|
+
- [ ] **Step 3: Locate elements.** Instantiate `Finder` objects for the target widgets.
|
|
41
|
+
- [ ] **Step 4: Verify initial state.** Use `expect(finder, matcher)` to validate the initial render.
|
|
42
|
+
- [ ] **Step 5: Simulate interactions.** Execute gestures or inputs (e.g., `await tester.tap(buttonFinder)`).
|
|
43
|
+
- [ ] **Step 6: Rebuild the tree.** Call `await tester.pump()` or `await tester.pumpAndSettle()` to process state changes.
|
|
44
|
+
- [ ] **Step 7: Verify updated state.** Use `expect()` to validate the UI after the interaction.
|
|
45
|
+
- [ ] **Step 8: Run and validate.** Execute `flutter test test/your_test_file_test.dart`.
|
|
46
|
+
- [ ] **Step 9: Feedback Loop.** Review test output -> identify failing matchers -> adjust widget logic or test assertions -> re-run until passing.
|
|
47
|
+
|
|
48
|
+
## Interaction & State Management
|
|
49
|
+
|
|
50
|
+
Apply the following conditional logic based on the type of interaction or state change being tested:
|
|
51
|
+
|
|
52
|
+
* **If testing static rendering:** Call `await tester.pumpWidget()` once, then immediately run `expect()` assertions.
|
|
53
|
+
* **If testing standard state changes (e.g., button taps):**
|
|
54
|
+
1. Call `await tester.tap(finder)`.
|
|
55
|
+
2. Call `await tester.pump()` to trigger a single frame rebuild.
|
|
56
|
+
* **If testing animations, transitions, or asynchronous UI updates:**
|
|
57
|
+
1. Trigger the action (e.g., `await tester.drag(finder, Offset(500, 0))`).
|
|
58
|
+
2. Call `await tester.pumpAndSettle()` to repeatedly pump frames until no more frames are scheduled (animation completes).
|
|
59
|
+
* **If testing text input:** Call `await tester.enterText(textFieldFinder, 'Input string')`.
|
|
60
|
+
* **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.
|
|
61
|
+
|
|
62
|
+
## Examples
|
|
63
|
+
|
|
64
|
+
### High-Fidelity Widget Test Implementation
|
|
65
|
+
|
|
66
|
+
**Target Widget (`lib/todo_list.dart`):**
|
|
67
|
+
```dart
|
|
68
|
+
import 'package:flutter/material.dart';
|
|
69
|
+
|
|
70
|
+
class TodoList extends StatefulWidget {
|
|
71
|
+
const TodoList({super.key});
|
|
72
|
+
|
|
73
|
+
@override
|
|
74
|
+
State<TodoList> createState() => _TodoListState();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
class _TodoListState extends State<TodoList> {
|
|
78
|
+
final todos = <String>[];
|
|
79
|
+
final controller = TextEditingController();
|
|
80
|
+
|
|
81
|
+
@override
|
|
82
|
+
Widget build(BuildContext context) {
|
|
83
|
+
return MaterialApp(
|
|
84
|
+
home: Scaffold(
|
|
85
|
+
body: Column(
|
|
86
|
+
children: [
|
|
87
|
+
TextField(controller: controller),
|
|
88
|
+
Expanded(
|
|
89
|
+
child: ListView.builder(
|
|
90
|
+
itemCount: todos.length,
|
|
91
|
+
itemBuilder: (context, index) {
|
|
92
|
+
final todo = todos[index];
|
|
93
|
+
return Dismissible(
|
|
94
|
+
key: Key('$todo$index'),
|
|
95
|
+
onDismissed: (_) => setState(() => todos.removeAt(index)),
|
|
96
|
+
child: ListTile(title: Text(todo)),
|
|
97
|
+
);
|
|
98
|
+
},
|
|
99
|
+
),
|
|
100
|
+
),
|
|
101
|
+
],
|
|
102
|
+
),
|
|
103
|
+
floatingActionButton: FloatingActionButton(
|
|
104
|
+
onPressed: () {
|
|
105
|
+
setState(() {
|
|
106
|
+
todos.add(controller.text);
|
|
107
|
+
controller.clear();
|
|
108
|
+
});
|
|
109
|
+
},
|
|
110
|
+
child: const Icon(Icons.add),
|
|
111
|
+
),
|
|
112
|
+
),
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Test Implementation (`test/todo_list_test.dart`):**
|
|
119
|
+
```dart
|
|
120
|
+
import 'package:flutter/material.dart';
|
|
121
|
+
import 'package:flutter_test/flutter_test.dart';
|
|
122
|
+
import 'package:my_app/todo_list.dart';
|
|
123
|
+
|
|
124
|
+
void main() {
|
|
125
|
+
testWidgets('Add and remove a todo item', (WidgetTester tester) async {
|
|
126
|
+
// 1. Build the widget
|
|
127
|
+
await tester.pumpWidget(const TodoList());
|
|
128
|
+
|
|
129
|
+
// 2. Verify initial state
|
|
130
|
+
expect(find.byType(ListTile), findsNothing);
|
|
131
|
+
|
|
132
|
+
// 3. Enter text into the TextField
|
|
133
|
+
await tester.enterText(find.byType(TextField), 'Buy groceries');
|
|
134
|
+
|
|
135
|
+
// 4. Tap the add button
|
|
136
|
+
await tester.tap(find.byType(FloatingActionButton));
|
|
137
|
+
|
|
138
|
+
// 5. Rebuild the widget to reflect the new state
|
|
139
|
+
await tester.pump();
|
|
140
|
+
|
|
141
|
+
// 6. Verify the item was added
|
|
142
|
+
expect(find.text('Buy groceries'), findsOneWidget);
|
|
143
|
+
|
|
144
|
+
// 7. Swipe the item to dismiss it
|
|
145
|
+
await tester.drag(find.byType(Dismissible), const Offset(500, 0));
|
|
146
|
+
|
|
147
|
+
// 8. Build the widget until the dismiss animation ends
|
|
148
|
+
await tester.pumpAndSettle();
|
|
149
|
+
|
|
150
|
+
// 9. Verify the item was removed
|
|
151
|
+
expect(find.text('Buy groceries'), findsNothing);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
```
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: flutter-apply-architecture-best-practices
|
|
3
|
+
description: Architects a Flutter application using the recommended layered approach (UI, Logic, Data). Use when structuring a new project or refactoring for scalability.
|
|
4
|
+
metadata:
|
|
5
|
+
model: models/gemini-3.1-pro-preview
|
|
6
|
+
last_modified: Tue, 21 Apr 2026 20:11:20 GMT
|
|
7
|
+
---
|
|
8
|
+
# Architecting Flutter Applications
|
|
9
|
+
|
|
10
|
+
## Contents
|
|
11
|
+
- [Architectural Layers](#architectural-layers)
|
|
12
|
+
- [Project Structure](#project-structure)
|
|
13
|
+
- [Workflow: Implementing a New Feature](#workflow-implementing-a-new-feature)
|
|
14
|
+
- [Examples](#examples)
|
|
15
|
+
|
|
16
|
+
## Architectural Layers
|
|
17
|
+
|
|
18
|
+
Enforce strict Separation of Concerns by dividing the application into distinct layers. Never mix UI rendering with business logic or data fetching.
|
|
19
|
+
|
|
20
|
+
### UI Layer (Presentation)
|
|
21
|
+
Implement the MVVM (Model-View-ViewModel) pattern to manage UI state and logic.
|
|
22
|
+
* **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.
|
|
23
|
+
* **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.
|
|
24
|
+
|
|
25
|
+
### Data Layer
|
|
26
|
+
Implement the Repository pattern to isolate data access logic and create a single source of truth.
|
|
27
|
+
* **Services:** Create stateless classes to wrap external APIs (HTTP clients, local databases, platform plugins). Return raw API models or `Result` wrappers.
|
|
28
|
+
* **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.
|
|
29
|
+
|
|
30
|
+
### Logic Layer (Domain - Optional)
|
|
31
|
+
* **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.
|
|
32
|
+
|
|
33
|
+
## Project Structure
|
|
34
|
+
|
|
35
|
+
Organize the codebase using a hybrid approach: group UI components by feature, and group Data/Domain components by type.
|
|
36
|
+
|
|
37
|
+
```text
|
|
38
|
+
lib/
|
|
39
|
+
├── data/
|
|
40
|
+
│ ├── models/ # API models
|
|
41
|
+
│ ├── repositories/ # Repository implementations
|
|
42
|
+
│ └── services/ # API clients, local storage wrappers
|
|
43
|
+
├── domain/
|
|
44
|
+
│ ├── models/ # Clean domain models
|
|
45
|
+
│ └── use_cases/ # Optional business logic classes
|
|
46
|
+
└── ui/
|
|
47
|
+
├── core/ # Shared widgets, themes, typography
|
|
48
|
+
└── features/
|
|
49
|
+
└── [feature_name]/
|
|
50
|
+
├── view_models/
|
|
51
|
+
└── views/
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Workflow: Implementing a New Feature
|
|
55
|
+
|
|
56
|
+
Follow this sequential workflow when adding a new feature to the application. Copy the checklist to track progress.
|
|
57
|
+
|
|
58
|
+
### Task Progress
|
|
59
|
+
- [ ] **Step 1: Define Domain Models.** Create immutable data classes for the feature using `freezed` or `built_value`.
|
|
60
|
+
- [ ] **Step 2: Implement Services.** Create or update Service classes to handle external API communication.
|
|
61
|
+
- [ ] **Step 3: Implement Repositories.** Create the Repository to consume Services and return Domain Models.
|
|
62
|
+
- [ ] **Step 4: Apply Conditional Logic (Domain Layer).**
|
|
63
|
+
- *If the feature requires complex data transformation or cross-repository logic:* Create a Use Case class.
|
|
64
|
+
- *If the feature is a simple CRUD operation:* Skip to Step 5.
|
|
65
|
+
- [ ] **Step 5: Implement the ViewModel.** Create the ViewModel extending `ChangeNotifier`. Inject required Repositories/Use Cases. Expose immutable state and command methods.
|
|
66
|
+
- [ ] **Step 6: Implement the View.** Create the UI widget. Use `ListenableBuilder` or `AnimatedBuilder` to listen to ViewModel changes.
|
|
67
|
+
- [ ] **Step 7: Inject Dependencies.** Register the new Service, Repository, and ViewModel in the dependency injection container (e.g., `provider` or `get_it`).
|
|
68
|
+
- [ ] **Step 8: Run Validator.** Execute unit tests for the ViewModel and Repository.
|
|
69
|
+
- *Feedback Loop:* Run tests -> Review failures -> Fix logic -> Re-run until passing.
|
|
70
|
+
|
|
71
|
+
## Examples
|
|
72
|
+
|
|
73
|
+
### Data Layer: Service and Repository
|
|
74
|
+
|
|
75
|
+
```dart
|
|
76
|
+
// 1. Service (Raw API interaction)
|
|
77
|
+
class ApiClient {
|
|
78
|
+
Future<UserApiModel> fetchUser(String id) async {
|
|
79
|
+
// HTTP GET implementation...
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// 2. Repository (Single source of truth, returns Domain Model)
|
|
84
|
+
class UserRepository {
|
|
85
|
+
UserRepository({required ApiClient apiClient}) : _apiClient = apiClient;
|
|
86
|
+
|
|
87
|
+
final ApiClient _apiClient;
|
|
88
|
+
User? _cachedUser;
|
|
89
|
+
|
|
90
|
+
Future<User> getUser(String id) async {
|
|
91
|
+
if (_cachedUser != null) return _cachedUser!;
|
|
92
|
+
|
|
93
|
+
final apiModel = await _apiClient.fetchUser(id);
|
|
94
|
+
_cachedUser = User(id: apiModel.id, name: apiModel.fullName); // Transform to Domain Model
|
|
95
|
+
return _cachedUser!;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### UI Layer: ViewModel and View
|
|
101
|
+
|
|
102
|
+
```dart
|
|
103
|
+
// 3. ViewModel (State management and presentation logic)
|
|
104
|
+
class ProfileViewModel extends ChangeNotifier {
|
|
105
|
+
ProfileViewModel({required UserRepository userRepository})
|
|
106
|
+
: _userRepository = userRepository;
|
|
107
|
+
|
|
108
|
+
final UserRepository _userRepository;
|
|
109
|
+
|
|
110
|
+
User? _user;
|
|
111
|
+
User? get user => _user;
|
|
112
|
+
|
|
113
|
+
bool _isLoading = false;
|
|
114
|
+
bool get isLoading => _isLoading;
|
|
115
|
+
|
|
116
|
+
Future<void> loadProfile(String id) async {
|
|
117
|
+
_isLoading = true;
|
|
118
|
+
notifyListeners();
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
_user = await _userRepository.getUser(id);
|
|
122
|
+
} finally {
|
|
123
|
+
_isLoading = false;
|
|
124
|
+
notifyListeners();
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// 4. View (Dumb UI component)
|
|
130
|
+
class ProfileView extends StatelessWidget {
|
|
131
|
+
const ProfileView({super.key, required this.viewModel});
|
|
132
|
+
|
|
133
|
+
final ProfileViewModel viewModel;
|
|
134
|
+
|
|
135
|
+
@override
|
|
136
|
+
Widget build(BuildContext context) {
|
|
137
|
+
return ListenableBuilder(
|
|
138
|
+
listenable: viewModel,
|
|
139
|
+
builder: (context, _) {
|
|
140
|
+
if (viewModel.isLoading) {
|
|
141
|
+
return const Center(child: CircularProgressIndicator());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
final user = viewModel.user;
|
|
145
|
+
if (user == null) {
|
|
146
|
+
return const Center(child: Text('User not found'));
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return Column(
|
|
150
|
+
children: [
|
|
151
|
+
Text(user.name),
|
|
152
|
+
ElevatedButton(
|
|
153
|
+
onPressed: () => viewModel.loadProfile(user.id),
|
|
154
|
+
child: const Text('Refresh'),
|
|
155
|
+
),
|
|
156
|
+
],
|
|
157
|
+
);
|
|
158
|
+
},
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: flutter-build-responsive-layout
|
|
3
|
+
description: Use `LayoutBuilder`, `MediaQuery`, or `Expanded/Flexible` to create a layout that adapts to different screen sizes. Use when you need the UI to look good on both mobile and tablet/desktop form factors.
|
|
4
|
+
metadata:
|
|
5
|
+
model: models/gemini-3.1-pro-preview
|
|
6
|
+
last_modified: Tue, 21 Apr 2026 20:17:40 GMT
|
|
7
|
+
---
|
|
8
|
+
# Implementing Adaptive Layouts
|
|
9
|
+
|
|
10
|
+
## Contents
|
|
11
|
+
- [Space Measurement Guidelines](#space-measurement-guidelines)
|
|
12
|
+
- [Widget Sizing and Constraints](#widget-sizing-and-constraints)
|
|
13
|
+
- [Device and Orientation Behaviors](#device-and-orientation-behaviors)
|
|
14
|
+
- [Workflow: Constructing an Adaptive Layout](#workflow-constructing-an-adaptive-layout)
|
|
15
|
+
- [Workflow: Optimizing for Large Screens](#workflow-optimizing-for-large-screens)
|
|
16
|
+
- [Examples](#examples)
|
|
17
|
+
|
|
18
|
+
## Space Measurement Guidelines
|
|
19
|
+
Determine the available space accurately to ensure layouts adapt to the app window, not just the physical device.
|
|
20
|
+
|
|
21
|
+
* **Use `MediaQuery.sizeOf(context)`** to get the size of the entire app window.
|
|
22
|
+
* **Use `LayoutBuilder`** to make layout decisions based on the parent widget's allocated space. Evaluate `constraints.maxWidth` to determine the appropriate widget tree to return.
|
|
23
|
+
* **Do not use `MediaQuery.orientationOf` or `OrientationBuilder`** near the top of the widget tree to switch layouts. Device orientation does not accurately reflect the available app window space.
|
|
24
|
+
* **Do not check for hardware types** (e.g., "phone" vs. "tablet"). Flutter apps run in resizable windows, multi-window modes, and picture-in-picture. Base all layout decisions strictly on available window space.
|
|
25
|
+
|
|
26
|
+
## Widget Sizing and Constraints
|
|
27
|
+
Understand and apply Flutter's core layout rule: **Constraints go down. Sizes go up. Parent sets position.**
|
|
28
|
+
|
|
29
|
+
* **Distribute Space:** Use `Expanded` and `Flexible` within `Row`, `Column`, or `Flex` widgets.
|
|
30
|
+
* Use `Expanded` to force a child to fill all remaining available space (equivalent to `Flexible` with `fit: FlexFit.tight` and a `flex` factor of 1.0).
|
|
31
|
+
* Use `Flexible` to allow a child to size itself up to a specific limit while still expanding/contracting. Use the `flex` factor to define the ratio of space consumption among siblings.
|
|
32
|
+
* **Constrain Width:** Prevent widgets from consuming all horizontal space on large screens. Wrap widgets like `GridView` or `ListView` in a `ConstrainedBox` or `Container` and define a `maxWidth` in the `BoxConstraints`.
|
|
33
|
+
* **Lazy Rendering:** Always use `ListView.builder` or `GridView.builder` when rendering lists with an unknown or large number of items.
|
|
34
|
+
|
|
35
|
+
## Device and Orientation Behaviors
|
|
36
|
+
Ensure the app behaves correctly across all device form factors and input methods.
|
|
37
|
+
|
|
38
|
+
* **Do not lock screen orientation.** Locking orientation causes severe layout issues on foldable devices, often resulting in letterboxing (the app centered with black borders). Android large format tiers require both portrait and landscape support.
|
|
39
|
+
* **Fallback for Locked Orientation:** If business requirements strictly mandate a locked orientation, use the `Display API` to retrieve physical screen dimensions instead of `MediaQuery`. `MediaQuery` fails to receive the larger window size in compatibility modes.
|
|
40
|
+
* **Support Multiple Inputs:** Implement support for basic mice, trackpads, and keyboard shortcuts. Ensure touch targets are appropriately sized and keyboard navigation is accessible.
|
|
41
|
+
|
|
42
|
+
## Workflow: Constructing an Adaptive Layout
|
|
43
|
+
|
|
44
|
+
Follow this workflow to implement a layout that adapts to the available `BoxConstraints`.
|
|
45
|
+
|
|
46
|
+
**Task Progress:**
|
|
47
|
+
- [ ] Identify the target widget that requires adaptive behavior.
|
|
48
|
+
- [ ] Wrap the widget tree in a `LayoutBuilder`.
|
|
49
|
+
- [ ] Extract the `constraints.maxWidth` from the builder callback.
|
|
50
|
+
- [ ] Define an adaptive breakpoint (e.g., `largeScreenMinWidth = 600`).
|
|
51
|
+
- [ ] **If `maxWidth > largeScreenMinWidth`:** Return a large-screen layout (e.g., a `Row` placing a navigation sidebar and content area side-by-side).
|
|
52
|
+
- [ ] **If `maxWidth <= largeScreenMinWidth`:** Return a small-screen layout (e.g., a `Column` or standard navigation-style approach).
|
|
53
|
+
- [ ] Run validator -> resize the application window -> review layout transitions -> fix overflow errors.
|
|
54
|
+
|
|
55
|
+
## Workflow: Optimizing for Large Screens
|
|
56
|
+
|
|
57
|
+
Follow this workflow to prevent UI elements from stretching unnaturally on large displays.
|
|
58
|
+
|
|
59
|
+
**Task Progress:**
|
|
60
|
+
- [ ] Identify full-width components (e.g., `ListView`, text blocks, forms).
|
|
61
|
+
- [ ] **If optimizing a list:** Convert `ListView.builder` to `GridView.builder` using `SliverGridDelegateWithMaxCrossAxisExtent` to automatically adjust column counts based on window size.
|
|
62
|
+
- [ ] **If optimizing a form or text block:** Wrap the component in a `ConstrainedBox`.
|
|
63
|
+
- [ ] Apply `BoxConstraints(maxWidth: [optimal_width])` to the `ConstrainedBox`.
|
|
64
|
+
- [ ] Wrap the `ConstrainedBox` in a `Center` widget to keep the constrained content centered on large screens.
|
|
65
|
+
- [ ] Run validator -> test on desktop/tablet target -> review horizontal stretching -> adjust `maxWidth` or grid extents.
|
|
66
|
+
|
|
67
|
+
## Examples
|
|
68
|
+
|
|
69
|
+
### Adaptive Layout using LayoutBuilder
|
|
70
|
+
Demonstrates switching between a mobile and desktop layout based on available width.
|
|
71
|
+
|
|
72
|
+
```dart
|
|
73
|
+
import 'package:flutter/material.dart';
|
|
74
|
+
|
|
75
|
+
const double largeScreenMinWidth = 600.0;
|
|
76
|
+
|
|
77
|
+
class AdaptiveLayout extends StatelessWidget {
|
|
78
|
+
const AdaptiveLayout({super.key});
|
|
79
|
+
|
|
80
|
+
@override
|
|
81
|
+
Widget build(BuildContext context) {
|
|
82
|
+
return LayoutBuilder(
|
|
83
|
+
builder: (context, constraints) {
|
|
84
|
+
if (constraints.maxWidth > largeScreenMinWidth) {
|
|
85
|
+
return _buildLargeScreenLayout();
|
|
86
|
+
} else {
|
|
87
|
+
return _buildSmallScreenLayout();
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
Widget _buildLargeScreenLayout() {
|
|
94
|
+
return Row(
|
|
95
|
+
children: [
|
|
96
|
+
const SizedBox(width: 250, child: Placeholder(color: Colors.blue)),
|
|
97
|
+
const VerticalDivider(width: 1),
|
|
98
|
+
Expanded(child: const Placeholder(color: Colors.green)),
|
|
99
|
+
],
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
Widget _buildSmallScreenLayout() {
|
|
104
|
+
return const Placeholder(color: Colors.green);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Constraining Width on Large Screens
|
|
110
|
+
Demonstrates preventing a widget from consuming all horizontal space.
|
|
111
|
+
|
|
112
|
+
```dart
|
|
113
|
+
import 'package:flutter/material.dart';
|
|
114
|
+
|
|
115
|
+
class ConstrainedContent extends StatelessWidget {
|
|
116
|
+
const ConstrainedContent({super.key});
|
|
117
|
+
|
|
118
|
+
@override
|
|
119
|
+
Widget build(BuildContext context) {
|
|
120
|
+
return Scaffold(
|
|
121
|
+
body: Center(
|
|
122
|
+
child: ConstrainedBox(
|
|
123
|
+
constraints: const BoxConstraints(
|
|
124
|
+
maxWidth: 800.0, // Maximum width for readability
|
|
125
|
+
),
|
|
126
|
+
child: ListView.builder(
|
|
127
|
+
itemCount: 50,
|
|
128
|
+
itemBuilder: (context, index) {
|
|
129
|
+
return ListTile(
|
|
130
|
+
title: Text('Item $index'),
|
|
131
|
+
);
|
|
132
|
+
},
|
|
133
|
+
),
|
|
134
|
+
),
|
|
135
|
+
),
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
```
|