sdd-full 4.6.2 → 4.8.1
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/index.js +18 -12
- package/package.json +1 -1
- package/skills/VERSION.md +3 -3
- package/skills/design-planning/global-overlay-stack-standard/SKILL.md +83 -0
- package/skills/design-planning/ui-motion-interaction-standard/SKILL.md +79 -0
- package/skills/flutter/skills/flutter-add-integration-test/SKILL.md +165 -0
- package/skills/flutter/skills/flutter-add-widget-preview/SKILL.md +147 -0
- package/skills/flutter/skills/flutter-add-widget-test/SKILL.md +156 -0
- package/skills/flutter/skills/flutter-apply-architecture-best-practices/SKILL.md +164 -0
- package/skills/flutter/skills/flutter-build-responsive-layout/SKILL.md +141 -0
- package/skills/flutter/skills/flutter-fix-layout-issues/SKILL.md +132 -0
- package/skills/flutter/skills/flutter-implement-json-serialization/SKILL.md +155 -0
- package/skills/flutter/skills/flutter-setup-declarative-routing/SKILL.md +257 -0
- package/skills/flutter/skills/flutter-setup-localization/SKILL.md +212 -0
- package/skills/flutter/skills/flutter-use-http-package/SKILL.md +177 -0
- package/skills/requirement-analysis/sdd/mock_sdd.md +156 -0
- 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//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,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
|
+
```
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
【claude code调用标识:flutter-build-responsive-layout】【trae调用标识:flutter-build-responsive-layout+Flutter开发】【流程场景:1.完整3阶段SDD流程、3.小型功能迭代】
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
name: flutter-build-responsive-layout
|
|
5
|
+
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.
|
|
6
|
+
metadata:
|
|
7
|
+
model: models/gemini-3.1-pro-preview
|
|
8
|
+
last_modified: Tue, 21 Apr 2026 20:17:40 GMT
|
|
9
|
+
---
|
|
10
|
+
# Implementing Adaptive Layouts
|
|
11
|
+
|
|
12
|
+
## Contents
|
|
13
|
+
- [Space Measurement Guidelines](#space-measurement-guidelines)
|
|
14
|
+
- [Widget Sizing and Constraints](#widget-sizing-and-constraints)
|
|
15
|
+
- [Device and Orientation Behaviors](#device-and-orientation-behaviors)
|
|
16
|
+
- [Workflow: Constructing an Adaptive Layout](#workflow-constructing-an-adaptive-layout)
|
|
17
|
+
- [Workflow: Optimizing for Large Screens](#workflow-optimizing-for-large-screens)
|
|
18
|
+
- [Examples](#examples)
|
|
19
|
+
|
|
20
|
+
## Space Measurement Guidelines
|
|
21
|
+
Determine the available space accurately to ensure layouts adapt to the app window, not just the physical device.
|
|
22
|
+
|
|
23
|
+
* **Use `MediaQuery.sizeOf(context)`** to get the size of the entire app window.
|
|
24
|
+
* **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.
|
|
25
|
+
* **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.
|
|
26
|
+
* **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.
|
|
27
|
+
|
|
28
|
+
## Widget Sizing and Constraints
|
|
29
|
+
Understand and apply Flutter's core layout rule: **Constraints go down. Sizes go up. Parent sets position.**
|
|
30
|
+
|
|
31
|
+
* **Distribute Space:** Use `Expanded` and `Flexible` within `Row`, `Column`, or `Flex` widgets.
|
|
32
|
+
* 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).
|
|
33
|
+
* 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.
|
|
34
|
+
* **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`.
|
|
35
|
+
* **Lazy Rendering:** Always use `ListView.builder` or `GridView.builder` when rendering lists with an unknown or large number of items.
|
|
36
|
+
|
|
37
|
+
## Device and Orientation Behaviors
|
|
38
|
+
Ensure the app behaves correctly across all device form factors and input methods.
|
|
39
|
+
|
|
40
|
+
* **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.
|
|
41
|
+
* **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.
|
|
42
|
+
* **Support Multiple Inputs:** Implement support for basic mice, trackpads, and keyboard shortcuts. Ensure touch targets are appropriately sized and keyboard navigation is accessible.
|
|
43
|
+
|
|
44
|
+
## Workflow: Constructing an Adaptive Layout
|
|
45
|
+
|
|
46
|
+
Follow this workflow to implement a layout that adapts to the available `BoxConstraints`.
|
|
47
|
+
|
|
48
|
+
**Task Progress:**
|
|
49
|
+
- [ ] Identify the target widget that requires adaptive behavior.
|
|
50
|
+
- [ ] Wrap the widget tree in a `LayoutBuilder`.
|
|
51
|
+
- [ ] Extract the `constraints.maxWidth` from the builder callback.
|
|
52
|
+
- [ ] Define an adaptive breakpoint (e.g., `largeScreenMinWidth = 600`).
|
|
53
|
+
- [ ] **If `maxWidth > largeScreenMinWidth`:** Return a large-screen layout (e.g., a `Row` placing a navigation sidebar and content area side-by-side).
|
|
54
|
+
- [ ] **If `maxWidth <= largeScreenMinWidth`:** Return a small-screen layout (e.g., a `Column` or standard navigation-style approach).
|
|
55
|
+
- [ ] Run validator -> resize the application window -> review layout transitions -> fix overflow errors.
|
|
56
|
+
|
|
57
|
+
## Workflow: Optimizing for Large Screens
|
|
58
|
+
|
|
59
|
+
Follow this workflow to prevent UI elements from stretching unnaturally on large displays.
|
|
60
|
+
|
|
61
|
+
**Task Progress:**
|
|
62
|
+
- [ ] Identify full-width components (e.g., `ListView`, text blocks, forms).
|
|
63
|
+
- [ ] **If optimizing a list:** Convert `ListView.builder` to `GridView.builder` using `SliverGridDelegateWithMaxCrossAxisExtent` to automatically adjust column counts based on window size.
|
|
64
|
+
- [ ] **If optimizing a form or text block:** Wrap the component in a `ConstrainedBox`.
|
|
65
|
+
- [ ] Apply `BoxConstraints(maxWidth: [optimal_width])` to the `ConstrainedBox`.
|
|
66
|
+
- [ ] Wrap the `ConstrainedBox` in a `Center` widget to keep the constrained content centered on large screens.
|
|
67
|
+
- [ ] Run validator -> test on desktop/tablet target -> review horizontal stretching -> adjust `maxWidth` or grid extents.
|
|
68
|
+
|
|
69
|
+
## Examples
|
|
70
|
+
|
|
71
|
+
### Adaptive Layout using LayoutBuilder
|
|
72
|
+
Demonstrates switching between a mobile and desktop layout based on available width.
|
|
73
|
+
|
|
74
|
+
```dart
|
|
75
|
+
import 'package:flutter/material.dart';
|
|
76
|
+
|
|
77
|
+
const double largeScreenMinWidth = 600.0;
|
|
78
|
+
|
|
79
|
+
class AdaptiveLayout extends StatelessWidget {
|
|
80
|
+
const AdaptiveLayout({super.key});
|
|
81
|
+
|
|
82
|
+
@override
|
|
83
|
+
Widget build(BuildContext context) {
|
|
84
|
+
return LayoutBuilder(
|
|
85
|
+
builder: (context, constraints) {
|
|
86
|
+
if (constraints.maxWidth > largeScreenMinWidth) {
|
|
87
|
+
return _buildLargeScreenLayout();
|
|
88
|
+
} else {
|
|
89
|
+
return _buildSmallScreenLayout();
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
Widget _buildLargeScreenLayout() {
|
|
96
|
+
return Row(
|
|
97
|
+
children: [
|
|
98
|
+
const SizedBox(width: 250, child: Placeholder(color: Colors.blue)),
|
|
99
|
+
const VerticalDivider(width: 1),
|
|
100
|
+
Expanded(child: const Placeholder(color: Colors.green)),
|
|
101
|
+
],
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
Widget _buildSmallScreenLayout() {
|
|
106
|
+
return const Placeholder(color: Colors.green);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Constraining Width on Large Screens
|
|
112
|
+
Demonstrates preventing a widget from consuming all horizontal space.
|
|
113
|
+
|
|
114
|
+
```dart
|
|
115
|
+
import 'package:flutter/material.dart';
|
|
116
|
+
|
|
117
|
+
class ConstrainedContent extends StatelessWidget {
|
|
118
|
+
const ConstrainedContent({super.key});
|
|
119
|
+
|
|
120
|
+
@override
|
|
121
|
+
Widget build(BuildContext context) {
|
|
122
|
+
return Scaffold(
|
|
123
|
+
body: Center(
|
|
124
|
+
child: ConstrainedBox(
|
|
125
|
+
constraints: const BoxConstraints(
|
|
126
|
+
maxWidth: 800.0, // Maximum width for readability
|
|
127
|
+
),
|
|
128
|
+
child: ListView.builder(
|
|
129
|
+
itemCount: 50,
|
|
130
|
+
itemBuilder: (context, index) {
|
|
131
|
+
return ListTile(
|
|
132
|
+
title: Text('Item $index'),
|
|
133
|
+
);
|
|
134
|
+
},
|
|
135
|
+
),
|
|
136
|
+
),
|
|
137
|
+
),
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
```
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
【claude code调用标识:flutter-fix-layout-issues】【trae调用标识:flutter-fix-layout-issues+Flutter开发】【流程场景:4.Bug处理】
|
|
2
|
+
|
|
3
|
+
---
|
|
4
|
+
name: flutter-fix-layout-issues
|
|
5
|
+
description: Fixes Flutter layout errors (overflows, unbounded constraints) using Dart and Flutter MCP tools. Use when addressing "RenderFlex overflowed", "Vertical viewport was given unbounded height", or similar layout issues.
|
|
6
|
+
metadata:
|
|
7
|
+
model: models/gemini-3.1-pro-preview
|
|
8
|
+
last_modified: Tue, 21 Apr 2026 19:45:59 GMT
|
|
9
|
+
---
|
|
10
|
+
# Resolving Flutter Layout Errors
|
|
11
|
+
|
|
12
|
+
## Contents
|
|
13
|
+
- [Constraint Violation Diagnostics](#constraint-violation-diagnostics)
|
|
14
|
+
- [Layout Error Resolution Workflow](#layout-error-resolution-workflow)
|
|
15
|
+
- [Examples](#examples)
|
|
16
|
+
|
|
17
|
+
## Constraint Violation Diagnostics
|
|
18
|
+
|
|
19
|
+
Flutter layout operates on a strict rule: **Constraints go down. Sizes go up. Parent sets position.** Layout errors occur when this negotiation fails, typically due to unbounded constraints or unconstrained children.
|
|
20
|
+
|
|
21
|
+
Diagnose layout failures using the following error signatures:
|
|
22
|
+
|
|
23
|
+
* **"Vertical viewport was given unbounded height"**: Triggered when a scrollable widget (`ListView`, `GridView`) is placed inside an unconstrained vertical parent (`Column`). The parent provides infinite height, and the child attempts to expand infinitely.
|
|
24
|
+
* **"An InputDecorator...cannot have an unbounded width"**: Triggered when a `TextField` or `TextFormField` is placed inside an unconstrained horizontal parent (`Row`). The text field attempts to determine its width based on infinite available space.
|
|
25
|
+
* **"RenderFlex overflowed"**: Triggered when a child of a `Row` or `Column` requests a size larger than the parent's allocated constraints. Visually indicated by yellow and black warning stripes.
|
|
26
|
+
* **"Incorrect use of ParentData widget"**: Triggered when a `ParentDataWidget` is not a direct descendant of its required ancestor. (e.g., `Expanded` outside a `Flex`, `Positioned` outside a `Stack`).
|
|
27
|
+
* **"RenderBox was not laid out"**: A cascading side-effect error. Ignore this and look further up the stack trace for the primary constraint violation (usually an unbounded height/width error).
|
|
28
|
+
|
|
29
|
+
## Layout Error Resolution Workflow
|
|
30
|
+
|
|
31
|
+
Copy and use this checklist to systematically resolve layout constraint violations.
|
|
32
|
+
|
|
33
|
+
### Task Progress
|
|
34
|
+
- [ ] Run the application in debug mode to capture the exact layout exception in the console.
|
|
35
|
+
- [ ] Identify the primary error message (ignore cascading "RenderBox was not laid out" errors).
|
|
36
|
+
- [ ] Apply the conditional fix based on the specific error type:
|
|
37
|
+
- **If "Vertical viewport was given unbounded height"**: Wrap the scrollable child (`ListView`, `GridView`) in an `Expanded` widget to consume remaining space, or wrap it in a `SizedBox` to provide an absolute height constraint.
|
|
38
|
+
- **If "An InputDecorator...cannot have an unbounded width"**: Wrap the `TextField` or `TextFormField` in an `Expanded` or `Flexible` widget.
|
|
39
|
+
- **If "RenderFlex overflowed"**: Constrain the overflowing child by wrapping it in an `Expanded` widget (to force it to fit) or a `Flexible` widget (to allow it to be smaller than the allocated space).
|
|
40
|
+
- **If "Incorrect use of ParentData widget"**: Move the `ParentDataWidget` to be a direct child of its required parent. Ensure `Expanded`/`Flexible` are direct children of `Row`/`Column`/`Flex`. Ensure `Positioned` is a direct child of `Stack`.
|
|
41
|
+
- [ ] Execute Flutter hot reload.
|
|
42
|
+
- [ ] Run validator -> review errors -> fix: Inspect the UI to verify the red/grey error screen or yellow/black overflow stripes are resolved. If new layout errors appear, repeat the workflow.
|
|
43
|
+
|
|
44
|
+
## Examples
|
|
45
|
+
|
|
46
|
+
### Fixing Unbounded Height (ListView in Column)
|
|
47
|
+
|
|
48
|
+
**Input (Error State):**
|
|
49
|
+
```dart
|
|
50
|
+
// Throws "Vertical viewport was given unbounded height"
|
|
51
|
+
Column(
|
|
52
|
+
children: <Widget>[
|
|
53
|
+
const Text('Header'),
|
|
54
|
+
ListView(
|
|
55
|
+
children: const <Widget>[
|
|
56
|
+
ListTile(title: Text('Item 1')),
|
|
57
|
+
ListTile(title: Text('Item 2')),
|
|
58
|
+
],
|
|
59
|
+
),
|
|
60
|
+
],
|
|
61
|
+
)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**Output (Resolved State):**
|
|
65
|
+
```dart
|
|
66
|
+
// Wrap ListView in Expanded to constrain its height to the remaining Column space
|
|
67
|
+
Column(
|
|
68
|
+
children: <Widget>[
|
|
69
|
+
const Text('Header'),
|
|
70
|
+
Expanded(
|
|
71
|
+
child: ListView(
|
|
72
|
+
children: const <Widget>[
|
|
73
|
+
ListTile(title: Text('Item 1')),
|
|
74
|
+
ListTile(title: Text('Item 2')),
|
|
75
|
+
],
|
|
76
|
+
),
|
|
77
|
+
),
|
|
78
|
+
],
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Fixing Unbounded Width (TextField in Row)
|
|
83
|
+
|
|
84
|
+
**Input (Error State):**
|
|
85
|
+
```dart
|
|
86
|
+
// Throws "An InputDecorator...cannot have an unbounded width"
|
|
87
|
+
Row(
|
|
88
|
+
children: [
|
|
89
|
+
const Icon(Icons.search),
|
|
90
|
+
TextField(),
|
|
91
|
+
],
|
|
92
|
+
)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
**Output (Resolved State):**
|
|
96
|
+
```dart
|
|
97
|
+
// Wrap TextField in Expanded to constrain its width to the remaining Row space
|
|
98
|
+
Row(
|
|
99
|
+
children: [
|
|
100
|
+
const Icon(Icons.search),
|
|
101
|
+
Expanded(
|
|
102
|
+
child: TextField(),
|
|
103
|
+
),
|
|
104
|
+
],
|
|
105
|
+
)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Fixing RenderFlex Overflow
|
|
109
|
+
|
|
110
|
+
**Input (Error State):**
|
|
111
|
+
```dart
|
|
112
|
+
// Throws "A RenderFlex overflowed by X pixels on the right"
|
|
113
|
+
Row(
|
|
114
|
+
children: [
|
|
115
|
+
const Icon(Icons.info),
|
|
116
|
+
const Text('This is a very long text string that will definitely overflow the available screen width and cause a RenderFlex error.'),
|
|
117
|
+
],
|
|
118
|
+
)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**Output (Resolved State):**
|
|
122
|
+
```dart
|
|
123
|
+
// Wrap the Text widget in Expanded to force it to wrap within the available constraints
|
|
124
|
+
Row(
|
|
125
|
+
children: [
|
|
126
|
+
const Icon(Icons.info),
|
|
127
|
+
Expanded(
|
|
128
|
+
child: const Text('This is a very long text string that will definitely overflow the available screen width and cause a RenderFlex error.'),
|
|
129
|
+
),
|
|
130
|
+
],
|
|
131
|
+
)
|
|
132
|
+
```
|