feat: add flutter agent skills support
This commit is contained in:
163
.agents/skills/flutter-add-integration-test/SKILL.md
Normal file
163
.agents/skills/flutter-add-integration-test/SKILL.md
Normal file
@@ -0,0 +1,163 @@
|
||||
---
|
||||
name: flutter-add-integration-test
|
||||
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.
|
||||
metadata:
|
||||
model: models/gemini-3.1-pro-preview
|
||||
last_modified: Tue, 21 Apr 2026 18:29:20 GMT
|
||||
---
|
||||
# Implementing Flutter Integration Tests
|
||||
|
||||
## Contents
|
||||
- [Project Setup and Dependencies](#project-setup-and-dependencies)
|
||||
- [Interactive Exploration via MCP](#interactive-exploration-via-mcp)
|
||||
- [Test Authoring Guidelines](#test-authoring-guidelines)
|
||||
- [Execution and Profiling](#execution-and-profiling)
|
||||
- [Workflow: End-to-End Integration Testing](#workflow-end-to-end-integration-testing)
|
||||
- [Examples](#examples)
|
||||
|
||||
## Project Setup and Dependencies
|
||||
|
||||
Configure the project to support integration testing and Flutter Driver extensions.
|
||||
|
||||
1. Add required development dependencies to `pubspec.yaml`:
|
||||
```bash
|
||||
flutter pub add 'dev:integration_test:{"sdk":"flutter"}'
|
||||
flutter pub add 'dev:flutter_test:{"sdk":"flutter"}'
|
||||
```
|
||||
2. Enable the Flutter Driver extension in your application entry point (typically `lib/main.dart` or a dedicated `lib/main_test.dart`):
|
||||
- Import `package:flutter_driver/driver_extension.dart`.
|
||||
- Call `enableFlutterDriverExtension();` before `runApp()`.
|
||||
3. Add `Key` parameters (e.g., `ValueKey('login_button')`) to critical widgets in the application code to ensure reliable targeting during tests.
|
||||
|
||||
## Interactive Exploration via MCP
|
||||
|
||||
Use the Dart/Flutter MCP server tools to interactively explore and manipulate the application state before writing static tests.
|
||||
|
||||
- **Launch**: Execute `launch_app` with `target: "lib/main_test.dart"` to start the application and acquire the DTD URI.
|
||||
- **Inspect**: Execute `get_widget_tree` to discover available `Key`s, `Text` nodes, and widget `Type`s.
|
||||
- **Interact**: Execute `tap`, `enter_text`, and `scroll` to simulate user flows.
|
||||
- **Wait**: Always execute `waitFor` or verify state with `get_health` when navigating or triggering animations.
|
||||
- **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.
|
||||
|
||||
## Test Authoring Guidelines
|
||||
|
||||
Structure integration tests using the `flutter_test` API paradigm.
|
||||
|
||||
- Create a dedicated `integration_test/` directory at the project root.
|
||||
- Name all test files using the `<name>_test.dart` convention.
|
||||
- Initialize the binding by calling `IntegrationTestWidgetsFlutterBinding.ensureInitialized();` at the start of `main()`.
|
||||
- Load the application UI using `await tester.pumpWidget(MyApp());`.
|
||||
- Trigger frames and wait for animations to complete using `await tester.pumpAndSettle();` after interactions like `tester.tap()`.
|
||||
- Assert widget visibility using `expect(find.byKey(ValueKey('foo')), findsOneWidget);` or `findsNothing`.
|
||||
- Scroll to specific off-screen widgets using `await tester.scrollUntilVisible(itemFinder, 500.0, scrollable: listFinder);`.
|
||||
|
||||
**Conditional Logic for Legacy `flutter_driver`:**
|
||||
- If maintaining or migrating legacy `flutter_driver` tests, use `driver.waitFor()`, `driver.waitForAbsent()`, `driver.tap()`, and `driver.scroll()` instead of the `WidgetTester` APIs.
|
||||
|
||||
## Execution and Profiling
|
||||
|
||||
Execute tests using the `flutter drive` command. Require a host driver script located in `test_driver/integration_test.dart` that calls `integrationDriver()`.
|
||||
|
||||
**Conditional Execution Targets:**
|
||||
- **If testing on Chrome:** Launch `chromedriver --port=4444` in a separate terminal, then run:
|
||||
`flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart -d chrome`
|
||||
- **If testing headless web:** Run with `-d web-server`.
|
||||
- **If testing on Android (Local):** Run `flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart`.
|
||||
- **If testing on Firebase Test Lab (Android):**
|
||||
1. Build debug APK: `flutter build apk --debug`
|
||||
2. Build test APK: `./gradlew app:assembleAndroidTest`
|
||||
3. Upload both APKs to the Firebase Test Lab console.
|
||||
|
||||
## Workflow: End-to-End Integration Testing
|
||||
|
||||
Copy and follow this checklist to implement and verify integration tests.
|
||||
|
||||
- [ ] **Task Progress: Setup**
|
||||
- [ ] Add `integration_test` and `flutter_test` to `pubspec.yaml`.
|
||||
- [ ] Inject `enableFlutterDriverExtension()` into the app entry point.
|
||||
- [ ] Assign `ValueKey`s to target widgets.
|
||||
- [ ] **Task Progress: Exploration**
|
||||
- [ ] Run `launch_app` via MCP.
|
||||
- [ ] Map the widget tree using `get_widget_tree`.
|
||||
- [ ] Validate interaction paths using MCP tools (`tap`, `enter_text`).
|
||||
- [ ] **Task Progress: Authoring**
|
||||
- [ ] Create `integration_test/app_test.dart`.
|
||||
- [ ] Write test cases using `WidgetTester` APIs.
|
||||
- [ ] Create `test_driver/integration_test.dart` with `integrationDriver()`.
|
||||
- [ ] **Task Progress: Execution & Feedback Loop**
|
||||
- [ ] Run `flutter drive --driver=test_driver/integration_test.dart --target=integration_test/app_test.dart`.
|
||||
- [ ] **Feedback Loop**: Review test output -> If `PumpAndSettleTimedOutException` occurs, check for infinite animations -> If widget not found, add `scrollUntilVisible` -> Re-run test until passing.
|
||||
|
||||
## Examples
|
||||
|
||||
### Standard Integration Test (`integration_test/app_test.dart`)
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:integration_test/integration_test.dart';
|
||||
import 'package:my_app/main.dart';
|
||||
|
||||
void main() {
|
||||
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
group('End-to-end test', () {
|
||||
testWidgets('tap on the floating action button, verify counter', (tester) async {
|
||||
// Load app widget.
|
||||
await tester.pumpWidget(const MyApp());
|
||||
|
||||
// Verify the counter starts at 0.
|
||||
expect(find.text('0'), findsOneWidget);
|
||||
|
||||
// Find the floating action button to tap on.
|
||||
final fab = find.byKey(const ValueKey('increment'));
|
||||
|
||||
// Emulate a tap on the floating action button.
|
||||
await tester.tap(fab);
|
||||
|
||||
// Trigger a frame and wait for animations.
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// Verify the counter increments by 1.
|
||||
expect(find.text('1'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
### Host Driver Script (`test_driver/integration_test.dart`)
|
||||
|
||||
```dart
|
||||
import 'package:integration_test/integration_test_driver.dart';
|
||||
|
||||
Future<void> main() => integrationDriver();
|
||||
```
|
||||
|
||||
### Performance Profiling Driver Script (`test_driver/perf_driver.dart`)
|
||||
|
||||
Use this driver script if you wrap your test actions in `binding.traceAction()` to capture performance metrics.
|
||||
|
||||
```dart
|
||||
import 'package:flutter_driver/flutter_driver.dart' as driver;
|
||||
import 'package:integration_test/integration_test_driver.dart';
|
||||
|
||||
Future<void> main() {
|
||||
return integrationDriver(
|
||||
responseDataCallback: (data) async {
|
||||
if (data != null) {
|
||||
final timeline = driver.Timeline.fromJson(
|
||||
data['scrolling_timeline'] as Map<String, dynamic>,
|
||||
);
|
||||
|
||||
final summary = driver.TimelineSummary.summarize(timeline);
|
||||
|
||||
await summary.writeTimelineToFile(
|
||||
'scrolling_timeline',
|
||||
pretty: true,
|
||||
includeSummary: true,
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
```
|
||||
145
.agents/skills/flutter-add-widget-preview/SKILL.md
Normal file
145
.agents/skills/flutter-add-widget-preview/SKILL.md
Normal file
@@ -0,0 +1,145 @@
|
||||
---
|
||||
name: flutter-add-widget-preview
|
||||
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.
|
||||
metadata:
|
||||
model: models/gemini-3.1-pro-preview
|
||||
last_modified: Tue, 21 Apr 2026 20:05:23 GMT
|
||||
---
|
||||
# Previewing Flutter Widgets
|
||||
|
||||
## Contents
|
||||
- [Preview Guidelines](#preview-guidelines)
|
||||
- [Handling Limitations](#handling-limitations)
|
||||
- [Workflows](#workflows)
|
||||
- [Examples](#examples)
|
||||
|
||||
## Preview Guidelines
|
||||
|
||||
Use the Flutter Widget Previewer to render widgets in real-time, isolated from the full application context.
|
||||
|
||||
- **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`.
|
||||
- **Imports:** Always import `package:flutter/widget_previews.dart` to access the preview annotations.
|
||||
- **Custom Annotations:** Extend the `Preview` class to create custom annotations that inject common properties (e.g., themes, wrappers) across multiple widgets.
|
||||
- **Multiple Configurations:** Apply multiple `@Preview` annotations to a single target to generate multiple preview instances. Alternatively, extend `MultiPreview` to encapsulate common multi-preview configurations.
|
||||
- **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).
|
||||
|
||||
## Handling Limitations
|
||||
|
||||
Adhere to the following constraints when authoring previewable widgets, as the Widget Previewer runs in a web environment:
|
||||
|
||||
- **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.
|
||||
- **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`).
|
||||
- **Public Callbacks:** Ensure all callback arguments provided to preview annotations are public and constant to satisfy code generation requirements.
|
||||
- **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.
|
||||
|
||||
## Workflows
|
||||
|
||||
### Creating a Widget Preview
|
||||
Copy and track this checklist when implementing a new widget preview:
|
||||
|
||||
- [ ] Import `package:flutter/widget_previews.dart`.
|
||||
- [ ] Identify a valid target (top-level function, static method, or parameter-less public constructor).
|
||||
- [ ] Apply the `@Preview` annotation to the target.
|
||||
- [ ] Configure preview parameters (`name`, `group`, `size`, `theme`, `brightness`, etc.) as needed.
|
||||
- [ ] If applying the same configuration to multiple widgets, extract the configuration into a custom class extending `Preview`.
|
||||
|
||||
### Interacting with Previews
|
||||
Follow the appropriate conditional workflow to launch and interact with the Widget Previewer:
|
||||
|
||||
**If using a supported IDE (Android Studio, IntelliJ, VS Code with Flutter 3.38+):**
|
||||
1. Launch the IDE. The Widget Previewer starts automatically.
|
||||
2. Open the "Flutter Widget Preview" tab in the sidebar.
|
||||
3. Toggle "Filter previews by selected file" at the bottom left if you want to view previews outside the currently active file.
|
||||
|
||||
**If using the Command Line:**
|
||||
1. Navigate to the Flutter project's root directory.
|
||||
2. Run `flutter widget-preview start`.
|
||||
3. View the automatically opened Chrome environment.
|
||||
|
||||
**Feedback Loop: Preview Iteration**
|
||||
1. Modify the widget code or preview configuration.
|
||||
2. Observe the automatic update in the Widget Previewer.
|
||||
3. If global state (e.g., static initializers) was modified: Click the global hot restart button at the bottom right.
|
||||
4. If only the local widget state needs resetting: Click the individual hot restart button on the specific preview card.
|
||||
5. Review errors in the IDE/CLI console -> fix -> repeat.
|
||||
|
||||
## Examples
|
||||
|
||||
### Basic Preview
|
||||
```dart
|
||||
import 'package:flutter/widget_previews.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
@Preview(name: 'My Sample Text', group: 'Typography')
|
||||
Widget mySampleText() {
|
||||
return const Text('Hello, World!');
|
||||
}
|
||||
```
|
||||
|
||||
### Custom Preview with Runtime Transformation
|
||||
```dart
|
||||
import 'package:flutter/widget_previews.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
final class TransformativePreview extends Preview {
|
||||
const TransformativePreview({
|
||||
super.name,
|
||||
super.group,
|
||||
});
|
||||
|
||||
PreviewThemeData _themeBuilder() {
|
||||
return PreviewThemeData(
|
||||
materialLight: ThemeData.light(),
|
||||
materialDark: ThemeData.dark(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Preview transform() {
|
||||
final originalPreview = super.transform();
|
||||
final builder = originalPreview.toBuilder();
|
||||
|
||||
builder
|
||||
..name = 'Transformed - ${originalPreview.name}'
|
||||
..theme = _themeBuilder;
|
||||
|
||||
return builder.toPreview();
|
||||
}
|
||||
}
|
||||
|
||||
@TransformativePreview(name: 'Custom Themed Button')
|
||||
Widget myButton() => const ElevatedButton(onPressed: null, child: Text('Click'));
|
||||
```
|
||||
|
||||
### MultiPreview Implementation
|
||||
```dart
|
||||
import 'package:flutter/widget_previews.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Creates light and dark mode previews automatically.
|
||||
final class MultiBrightnessPreview extends MultiPreview {
|
||||
const MultiBrightnessPreview({required this.name});
|
||||
|
||||
final String name;
|
||||
|
||||
@override
|
||||
List<Preview> get previews => const [
|
||||
Preview(brightness: Brightness.light),
|
||||
Preview(brightness: Brightness.dark),
|
||||
];
|
||||
|
||||
@override
|
||||
List<Preview> transform() {
|
||||
final previews = super.transform();
|
||||
return previews.map((preview) {
|
||||
final builder = preview.toBuilder()
|
||||
..group = 'Brightness'
|
||||
..name = '$name - ${preview.brightness!.name}';
|
||||
return builder.toPreview();
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@MultiBrightnessPreview(name: 'Primary Card')
|
||||
Widget cardPreview() => const Card(child: Padding(padding: EdgeInsets.all(8.0), child: Text('Content')));
|
||||
```
|
||||
154
.agents/skills/flutter-add-widget-test/SKILL.md
Normal file
154
.agents/skills/flutter-add-widget-test/SKILL.md
Normal file
@@ -0,0 +1,154 @@
|
||||
---
|
||||
name: flutter-add-widget-test
|
||||
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.
|
||||
metadata:
|
||||
model: models/gemini-3.1-pro-preview
|
||||
last_modified: Tue, 21 Apr 2026 21:15:41 GMT
|
||||
---
|
||||
# Writing Flutter Widget Tests
|
||||
|
||||
## Contents
|
||||
- [Setup & Configuration](#setup--configuration)
|
||||
- [Core Components](#core-components)
|
||||
- [Workflow: Implementing a Widget Test](#workflow-implementing-a-widget-test)
|
||||
- [Interaction & State Management](#interaction--state-management)
|
||||
- [Examples](#examples)
|
||||
|
||||
## Setup & Configuration
|
||||
|
||||
Ensure the testing environment is properly configured before authoring widget tests.
|
||||
|
||||
1. Add the `flutter_test` dependency to the `dev_dependencies` section of `pubspec.yaml`.
|
||||
2. Place all test files in the `test/` directory at the root of the project.
|
||||
3. Suffix all test file names with `_test.dart` (e.g., `widget_test.dart`).
|
||||
|
||||
## Core Components
|
||||
|
||||
Utilize the following `flutter_test` components to interact with and validate the widget tree:
|
||||
|
||||
* **`WidgetTester`**: The primary interface for building and interacting with widgets in the test environment. Provided automatically by the `testWidgets()` function.
|
||||
* **`Finder`**: Locates widgets in the test environment (e.g., `find.text('Submit')`, `find.byType(TextField)`, `find.byKey(Key('submit_btn'))`).
|
||||
* **`Matcher`**: Verifies the presence or state of widgets located by a `Finder` (e.g., `findsOneWidget`, `findsNothing`, `findsNWidgets(2)`, `matchesGoldenFile`).
|
||||
|
||||
## Workflow: Implementing a Widget Test
|
||||
|
||||
Copy the following checklist to track progress when implementing a new widget test.
|
||||
|
||||
### Task Progress
|
||||
- [ ] **Step 1: Define the test.** Use `testWidgets('description', (WidgetTester tester) async { ... })`.
|
||||
- [ ] **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.
|
||||
- [ ] **Step 3: Locate elements.** Instantiate `Finder` objects for the target widgets.
|
||||
- [ ] **Step 4: Verify initial state.** Use `expect(finder, matcher)` to validate the initial render.
|
||||
- [ ] **Step 5: Simulate interactions.** Execute gestures or inputs (e.g., `await tester.tap(buttonFinder)`).
|
||||
- [ ] **Step 6: Rebuild the tree.** Call `await tester.pump()` or `await tester.pumpAndSettle()` to process state changes.
|
||||
- [ ] **Step 7: Verify updated state.** Use `expect()` to validate the UI after the interaction.
|
||||
- [ ] **Step 8: Run and validate.** Execute `flutter test test/your_test_file_test.dart`.
|
||||
- [ ] **Step 9: Feedback Loop.** Review test output -> identify failing matchers -> adjust widget logic or test assertions -> re-run until passing.
|
||||
|
||||
## Interaction & State Management
|
||||
|
||||
Apply the following conditional logic based on the type of interaction or state change being tested:
|
||||
|
||||
* **If testing static rendering:** Call `await tester.pumpWidget()` once, then immediately run `expect()` assertions.
|
||||
* **If testing standard state changes (e.g., button taps):**
|
||||
1. Call `await tester.tap(finder)`.
|
||||
2. Call `await tester.pump()` to trigger a single frame rebuild.
|
||||
* **If testing animations, transitions, or asynchronous UI updates:**
|
||||
1. Trigger the action (e.g., `await tester.drag(finder, Offset(500, 0))`).
|
||||
2. Call `await tester.pumpAndSettle()` to repeatedly pump frames until no more frames are scheduled (animation completes).
|
||||
* **If testing text input:** Call `await tester.enterText(textFieldFinder, 'Input string')`.
|
||||
* **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.
|
||||
|
||||
## Examples
|
||||
|
||||
### High-Fidelity Widget Test Implementation
|
||||
|
||||
**Target Widget (`lib/todo_list.dart`):**
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class TodoList extends StatefulWidget {
|
||||
const TodoList({super.key});
|
||||
|
||||
@override
|
||||
State<TodoList> createState() => _TodoListState();
|
||||
}
|
||||
|
||||
class _TodoListState extends State<TodoList> {
|
||||
final todos = <String>[];
|
||||
final controller = TextEditingController();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
home: Scaffold(
|
||||
body: Column(
|
||||
children: [
|
||||
TextField(controller: controller),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
itemCount: todos.length,
|
||||
itemBuilder: (context, index) {
|
||||
final todo = todos[index];
|
||||
return Dismissible(
|
||||
key: Key('$todo$index'),
|
||||
onDismissed: (_) => setState(() => todos.removeAt(index)),
|
||||
child: ListTile(title: Text(todo)),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
todos.add(controller.text);
|
||||
controller.clear();
|
||||
});
|
||||
},
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Test Implementation (`test/todo_list_test.dart`):**
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:my_app/todo_list.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('Add and remove a todo item', (WidgetTester tester) async {
|
||||
// 1. Build the widget
|
||||
await tester.pumpWidget(const TodoList());
|
||||
|
||||
// 2. Verify initial state
|
||||
expect(find.byType(ListTile), findsNothing);
|
||||
|
||||
// 3. Enter text into the TextField
|
||||
await tester.enterText(find.byType(TextField), 'Buy groceries');
|
||||
|
||||
// 4. Tap the add button
|
||||
await tester.tap(find.byType(FloatingActionButton));
|
||||
|
||||
// 5. Rebuild the widget to reflect the new state
|
||||
await tester.pump();
|
||||
|
||||
// 6. Verify the item was added
|
||||
expect(find.text('Buy groceries'), findsOneWidget);
|
||||
|
||||
// 7. Swipe the item to dismiss it
|
||||
await tester.drag(find.byType(Dismissible), const Offset(500, 0));
|
||||
|
||||
// 8. Build the widget until the dismiss animation ends
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
// 9. Verify the item was removed
|
||||
expect(find.text('Buy groceries'), findsNothing);
|
||||
});
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,162 @@
|
||||
---
|
||||
name: flutter-apply-architecture-best-practices
|
||||
description: Architects a Flutter application using the recommended layered approach (UI, Logic, Data). Use when structuring a new project or refactoring for scalability.
|
||||
metadata:
|
||||
model: models/gemini-3.1-pro-preview
|
||||
last_modified: Tue, 21 Apr 2026 20:11:20 GMT
|
||||
---
|
||||
# Architecting Flutter Applications
|
||||
|
||||
## Contents
|
||||
- [Architectural Layers](#architectural-layers)
|
||||
- [Project Structure](#project-structure)
|
||||
- [Workflow: Implementing a New Feature](#workflow-implementing-a-new-feature)
|
||||
- [Examples](#examples)
|
||||
|
||||
## Architectural Layers
|
||||
|
||||
Enforce strict Separation of Concerns by dividing the application into distinct layers. Never mix UI rendering with business logic or data fetching.
|
||||
|
||||
### UI Layer (Presentation)
|
||||
Implement the MVVM (Model-View-ViewModel) pattern to manage UI state and logic.
|
||||
* **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.
|
||||
* **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.
|
||||
|
||||
### Data Layer
|
||||
Implement the Repository pattern to isolate data access logic and create a single source of truth.
|
||||
* **Services:** Create stateless classes to wrap external APIs (HTTP clients, local databases, platform plugins). Return raw API models or `Result` wrappers.
|
||||
* **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.
|
||||
|
||||
### Logic Layer (Domain - Optional)
|
||||
* **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.
|
||||
|
||||
## Project Structure
|
||||
|
||||
Organize the codebase using a hybrid approach: group UI components by feature, and group Data/Domain components by type.
|
||||
|
||||
```text
|
||||
lib/
|
||||
├── data/
|
||||
│ ├── models/ # API models
|
||||
│ ├── repositories/ # Repository implementations
|
||||
│ └── services/ # API clients, local storage wrappers
|
||||
├── domain/
|
||||
│ ├── models/ # Clean domain models
|
||||
│ └── use_cases/ # Optional business logic classes
|
||||
└── ui/
|
||||
├── core/ # Shared widgets, themes, typography
|
||||
└── features/
|
||||
└── [feature_name]/
|
||||
├── view_models/
|
||||
└── views/
|
||||
```
|
||||
|
||||
## Workflow: Implementing a New Feature
|
||||
|
||||
Follow this sequential workflow when adding a new feature to the application. Copy the checklist to track progress.
|
||||
|
||||
### Task Progress
|
||||
- [ ] **Step 1: Define Domain Models.** Create immutable data classes for the feature using `freezed` or `built_value`.
|
||||
- [ ] **Step 2: Implement Services.** Create or update Service classes to handle external API communication.
|
||||
- [ ] **Step 3: Implement Repositories.** Create the Repository to consume Services and return Domain Models.
|
||||
- [ ] **Step 4: Apply Conditional Logic (Domain Layer).**
|
||||
- *If the feature requires complex data transformation or cross-repository logic:* Create a Use Case class.
|
||||
- *If the feature is a simple CRUD operation:* Skip to Step 5.
|
||||
- [ ] **Step 5: Implement the ViewModel.** Create the ViewModel extending `ChangeNotifier`. Inject required Repositories/Use Cases. Expose immutable state and command methods.
|
||||
- [ ] **Step 6: Implement the View.** Create the UI widget. Use `ListenableBuilder` or `AnimatedBuilder` to listen to ViewModel changes.
|
||||
- [ ] **Step 7: Inject Dependencies.** Register the new Service, Repository, and ViewModel in the dependency injection container (e.g., `provider` or `get_it`).
|
||||
- [ ] **Step 8: Run Validator.** Execute unit tests for the ViewModel and Repository.
|
||||
- *Feedback Loop:* Run tests -> Review failures -> Fix logic -> Re-run until passing.
|
||||
|
||||
## Examples
|
||||
|
||||
### Data Layer: Service and Repository
|
||||
|
||||
```dart
|
||||
// 1. Service (Raw API interaction)
|
||||
class ApiClient {
|
||||
Future<UserApiModel> fetchUser(String id) async {
|
||||
// HTTP GET implementation...
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Repository (Single source of truth, returns Domain Model)
|
||||
class UserRepository {
|
||||
UserRepository({required ApiClient apiClient}) : _apiClient = apiClient;
|
||||
|
||||
final ApiClient _apiClient;
|
||||
User? _cachedUser;
|
||||
|
||||
Future<User> getUser(String id) async {
|
||||
if (_cachedUser != null) return _cachedUser!;
|
||||
|
||||
final apiModel = await _apiClient.fetchUser(id);
|
||||
_cachedUser = User(id: apiModel.id, name: apiModel.fullName); // Transform to Domain Model
|
||||
return _cachedUser!;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### UI Layer: ViewModel and View
|
||||
|
||||
```dart
|
||||
// 3. ViewModel (State management and presentation logic)
|
||||
class ProfileViewModel extends ChangeNotifier {
|
||||
ProfileViewModel({required UserRepository userRepository})
|
||||
: _userRepository = userRepository;
|
||||
|
||||
final UserRepository _userRepository;
|
||||
|
||||
User? _user;
|
||||
User? get user => _user;
|
||||
|
||||
bool _isLoading = false;
|
||||
bool get isLoading => _isLoading;
|
||||
|
||||
Future<void> loadProfile(String id) async {
|
||||
_isLoading = true;
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
_user = await _userRepository.getUser(id);
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. View (Dumb UI component)
|
||||
class ProfileView extends StatelessWidget {
|
||||
const ProfileView({super.key, required this.viewModel});
|
||||
|
||||
final ProfileViewModel viewModel;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListenableBuilder(
|
||||
listenable: viewModel,
|
||||
builder: (context, _) {
|
||||
if (viewModel.isLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
final user = viewModel.user;
|
||||
if (user == null) {
|
||||
return const Center(child: Text('User not found'));
|
||||
}
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
Text(user.name),
|
||||
ElevatedButton(
|
||||
onPressed: () => viewModel.loadProfile(user.id),
|
||||
child: const Text('Refresh'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
139
.agents/skills/flutter-build-responsive-layout/SKILL.md
Normal file
139
.agents/skills/flutter-build-responsive-layout/SKILL.md
Normal file
@@ -0,0 +1,139 @@
|
||||
---
|
||||
name: flutter-build-responsive-layout
|
||||
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.
|
||||
metadata:
|
||||
model: models/gemini-3.1-pro-preview
|
||||
last_modified: Tue, 21 Apr 2026 20:17:40 GMT
|
||||
---
|
||||
# Implementing Adaptive Layouts
|
||||
|
||||
## Contents
|
||||
- [Space Measurement Guidelines](#space-measurement-guidelines)
|
||||
- [Widget Sizing and Constraints](#widget-sizing-and-constraints)
|
||||
- [Device and Orientation Behaviors](#device-and-orientation-behaviors)
|
||||
- [Workflow: Constructing an Adaptive Layout](#workflow-constructing-an-adaptive-layout)
|
||||
- [Workflow: Optimizing for Large Screens](#workflow-optimizing-for-large-screens)
|
||||
- [Examples](#examples)
|
||||
|
||||
## Space Measurement Guidelines
|
||||
Determine the available space accurately to ensure layouts adapt to the app window, not just the physical device.
|
||||
|
||||
* **Use `MediaQuery.sizeOf(context)`** to get the size of the entire app window.
|
||||
* **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.
|
||||
* **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.
|
||||
* **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.
|
||||
|
||||
## Widget Sizing and Constraints
|
||||
Understand and apply Flutter's core layout rule: **Constraints go down. Sizes go up. Parent sets position.**
|
||||
|
||||
* **Distribute Space:** Use `Expanded` and `Flexible` within `Row`, `Column`, or `Flex` widgets.
|
||||
* 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).
|
||||
* 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.
|
||||
* **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`.
|
||||
* **Lazy Rendering:** Always use `ListView.builder` or `GridView.builder` when rendering lists with an unknown or large number of items.
|
||||
|
||||
## Device and Orientation Behaviors
|
||||
Ensure the app behaves correctly across all device form factors and input methods.
|
||||
|
||||
* **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.
|
||||
* **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.
|
||||
* **Support Multiple Inputs:** Implement support for basic mice, trackpads, and keyboard shortcuts. Ensure touch targets are appropriately sized and keyboard navigation is accessible.
|
||||
|
||||
## Workflow: Constructing an Adaptive Layout
|
||||
|
||||
Follow this workflow to implement a layout that adapts to the available `BoxConstraints`.
|
||||
|
||||
**Task Progress:**
|
||||
- [ ] Identify the target widget that requires adaptive behavior.
|
||||
- [ ] Wrap the widget tree in a `LayoutBuilder`.
|
||||
- [ ] Extract the `constraints.maxWidth` from the builder callback.
|
||||
- [ ] Define an adaptive breakpoint (e.g., `largeScreenMinWidth = 600`).
|
||||
- [ ] **If `maxWidth > largeScreenMinWidth`:** Return a large-screen layout (e.g., a `Row` placing a navigation sidebar and content area side-by-side).
|
||||
- [ ] **If `maxWidth <= largeScreenMinWidth`:** Return a small-screen layout (e.g., a `Column` or standard navigation-style approach).
|
||||
- [ ] Run validator -> resize the application window -> review layout transitions -> fix overflow errors.
|
||||
|
||||
## Workflow: Optimizing for Large Screens
|
||||
|
||||
Follow this workflow to prevent UI elements from stretching unnaturally on large displays.
|
||||
|
||||
**Task Progress:**
|
||||
- [ ] Identify full-width components (e.g., `ListView`, text blocks, forms).
|
||||
- [ ] **If optimizing a list:** Convert `ListView.builder` to `GridView.builder` using `SliverGridDelegateWithMaxCrossAxisExtent` to automatically adjust column counts based on window size.
|
||||
- [ ] **If optimizing a form or text block:** Wrap the component in a `ConstrainedBox`.
|
||||
- [ ] Apply `BoxConstraints(maxWidth: [optimal_width])` to the `ConstrainedBox`.
|
||||
- [ ] Wrap the `ConstrainedBox` in a `Center` widget to keep the constrained content centered on large screens.
|
||||
- [ ] Run validator -> test on desktop/tablet target -> review horizontal stretching -> adjust `maxWidth` or grid extents.
|
||||
|
||||
## Examples
|
||||
|
||||
### Adaptive Layout using LayoutBuilder
|
||||
Demonstrates switching between a mobile and desktop layout based on available width.
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
const double largeScreenMinWidth = 600.0;
|
||||
|
||||
class AdaptiveLayout extends StatelessWidget {
|
||||
const AdaptiveLayout({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
if (constraints.maxWidth > largeScreenMinWidth) {
|
||||
return _buildLargeScreenLayout();
|
||||
} else {
|
||||
return _buildSmallScreenLayout();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLargeScreenLayout() {
|
||||
return Row(
|
||||
children: [
|
||||
const SizedBox(width: 250, child: Placeholder(color: Colors.blue)),
|
||||
const VerticalDivider(width: 1),
|
||||
Expanded(child: const Placeholder(color: Colors.green)),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSmallScreenLayout() {
|
||||
return const Placeholder(color: Colors.green);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Constraining Width on Large Screens
|
||||
Demonstrates preventing a widget from consuming all horizontal space.
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class ConstrainedContent extends StatelessWidget {
|
||||
const ConstrainedContent({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(
|
||||
maxWidth: 800.0, // Maximum width for readability
|
||||
),
|
||||
child: ListView.builder(
|
||||
itemCount: 50,
|
||||
itemBuilder: (context, index) {
|
||||
return ListTile(
|
||||
title: Text('Item $index'),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
130
.agents/skills/flutter-fix-layout-issues/SKILL.md
Normal file
130
.agents/skills/flutter-fix-layout-issues/SKILL.md
Normal file
@@ -0,0 +1,130 @@
|
||||
---
|
||||
name: flutter-fix-layout-issues
|
||||
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.
|
||||
metadata:
|
||||
model: models/gemini-3.1-pro-preview
|
||||
last_modified: Tue, 21 Apr 2026 19:45:59 GMT
|
||||
---
|
||||
# Resolving Flutter Layout Errors
|
||||
|
||||
## Contents
|
||||
- [Constraint Violation Diagnostics](#constraint-violation-diagnostics)
|
||||
- [Layout Error Resolution Workflow](#layout-error-resolution-workflow)
|
||||
- [Examples](#examples)
|
||||
|
||||
## Constraint Violation Diagnostics
|
||||
|
||||
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.
|
||||
|
||||
Diagnose layout failures using the following error signatures:
|
||||
|
||||
* **"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.
|
||||
* **"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.
|
||||
* **"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.
|
||||
* **"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`).
|
||||
* **"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).
|
||||
|
||||
## Layout Error Resolution Workflow
|
||||
|
||||
Copy and use this checklist to systematically resolve layout constraint violations.
|
||||
|
||||
### Task Progress
|
||||
- [ ] Run the application in debug mode to capture the exact layout exception in the console.
|
||||
- [ ] Identify the primary error message (ignore cascading "RenderBox was not laid out" errors).
|
||||
- [ ] Apply the conditional fix based on the specific error type:
|
||||
- **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.
|
||||
- **If "An InputDecorator...cannot have an unbounded width"**: Wrap the `TextField` or `TextFormField` in an `Expanded` or `Flexible` widget.
|
||||
- **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).
|
||||
- **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`.
|
||||
- [ ] Execute Flutter hot reload.
|
||||
- [ ] 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.
|
||||
|
||||
## Examples
|
||||
|
||||
### Fixing Unbounded Height (ListView in Column)
|
||||
|
||||
**Input (Error State):**
|
||||
```dart
|
||||
// Throws "Vertical viewport was given unbounded height"
|
||||
Column(
|
||||
children: <Widget>[
|
||||
const Text('Header'),
|
||||
ListView(
|
||||
children: const <Widget>[
|
||||
ListTile(title: Text('Item 1')),
|
||||
ListTile(title: Text('Item 2')),
|
||||
],
|
||||
),
|
||||
],
|
||||
)
|
||||
```
|
||||
|
||||
**Output (Resolved State):**
|
||||
```dart
|
||||
// Wrap ListView in Expanded to constrain its height to the remaining Column space
|
||||
Column(
|
||||
children: <Widget>[
|
||||
const Text('Header'),
|
||||
Expanded(
|
||||
child: ListView(
|
||||
children: const <Widget>[
|
||||
ListTile(title: Text('Item 1')),
|
||||
ListTile(title: Text('Item 2')),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
```
|
||||
|
||||
### Fixing Unbounded Width (TextField in Row)
|
||||
|
||||
**Input (Error State):**
|
||||
```dart
|
||||
// Throws "An InputDecorator...cannot have an unbounded width"
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.search),
|
||||
TextField(),
|
||||
],
|
||||
)
|
||||
```
|
||||
|
||||
**Output (Resolved State):**
|
||||
```dart
|
||||
// Wrap TextField in Expanded to constrain its width to the remaining Row space
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.search),
|
||||
Expanded(
|
||||
child: TextField(),
|
||||
),
|
||||
],
|
||||
)
|
||||
```
|
||||
|
||||
### Fixing RenderFlex Overflow
|
||||
|
||||
**Input (Error State):**
|
||||
```dart
|
||||
// Throws "A RenderFlex overflowed by X pixels on the right"
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.info),
|
||||
const Text('This is a very long text string that will definitely overflow the available screen width and cause a RenderFlex error.'),
|
||||
],
|
||||
)
|
||||
```
|
||||
|
||||
**Output (Resolved State):**
|
||||
```dart
|
||||
// Wrap the Text widget in Expanded to force it to wrap within the available constraints
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.info),
|
||||
Expanded(
|
||||
child: const Text('This is a very long text string that will definitely overflow the available screen width and cause a RenderFlex error.'),
|
||||
),
|
||||
],
|
||||
)
|
||||
```
|
||||
153
.agents/skills/flutter-implement-json-serialization/SKILL.md
Normal file
153
.agents/skills/flutter-implement-json-serialization/SKILL.md
Normal file
@@ -0,0 +1,153 @@
|
||||
---
|
||||
name: flutter-implement-json-serialization
|
||||
description: Create model classes with `fromJson` and `toJson` methods using `dart:convert`. Use when manually mapping JSON keys to class properties for simple data structures.
|
||||
metadata:
|
||||
model: models/gemini-3.1-pro-preview
|
||||
last_modified: Tue, 21 Apr 2026 21:44:50 GMT
|
||||
---
|
||||
# Serializing JSON Manually in Flutter
|
||||
|
||||
## Contents
|
||||
- [Core Guidelines](#core-guidelines)
|
||||
- [Workflow: Implementing a Serializable Model](#workflow-implementing-a-serializable-model)
|
||||
- [Workflow: Fetching and Parsing JSON](#workflow-fetching-and-parsing-json)
|
||||
- [Examples](#examples)
|
||||
|
||||
## Core Guidelines
|
||||
|
||||
- **Import `dart:convert`**: Utilize Flutter's built-in `dart:convert` library for manual JSON encoding (`jsonEncode`) and decoding (`jsonDecode`).
|
||||
- **Enforce Type Safety**: Always cast the `dynamic` result of `jsonDecode()` to the expected type, typically `Map<String, dynamic>` for objects or `List<dynamic>` for arrays.
|
||||
- **Encapsulate Serialization Logic**: Define plain model classes containing properties corresponding to the JSON structure. Implement a `fromJson` factory constructor and a `toJson` method within the model.
|
||||
- **Handle Background Parsing**: If parsing large JSON documents (execution time > 16ms), offload the parsing logic to a separate isolate using Flutter's `compute()` function to prevent UI jank.
|
||||
- **Throw Exceptions on Failure**: When handling HTTP responses, throw an exception if the status code is not successful (e.g., not 200 OK or 201 Created). Do not return `null`.
|
||||
|
||||
## Workflow: Implementing a Serializable Model
|
||||
|
||||
Use this checklist to implement manual JSON serialization for a data model.
|
||||
|
||||
**Task Progress:**
|
||||
- [ ] Define the plain model class with `final` properties.
|
||||
- [ ] Implement the `factory Model.fromJson(Map<String, dynamic> json)` constructor.
|
||||
- [ ] Implement the `Map<String, dynamic> toJson()` method.
|
||||
- [ ] Write unit tests for both serialization methods.
|
||||
- [ ] Run validator -> review type mismatch errors -> fix casting logic.
|
||||
|
||||
1. **Define the Model**: Create a class with properties matching the JSON keys.
|
||||
2. **Implement `fromJson`**: Extract values from the `Map` and cast them to the appropriate Dart types. Use pattern matching or explicit casting.
|
||||
3. **Implement `toJson`**: Return a `Map<String, dynamic>` mapping the class properties back to their JSON string keys.
|
||||
4. **Validate**: Execute unit tests to ensure type safety, autocompletion, and compile-time exception handling function correctly.
|
||||
|
||||
## Workflow: Fetching and Parsing JSON
|
||||
|
||||
Use this conditional workflow when retrieving and parsing JSON from a network request.
|
||||
|
||||
**Task Progress:**
|
||||
- [ ] Execute the HTTP request.
|
||||
- [ ] Validate the response status code.
|
||||
- [ ] Determine parsing strategy (Synchronous vs. Isolate).
|
||||
- [ ] Decode and map the JSON to the model.
|
||||
|
||||
1. **Execute Request**: Use the `http` package to perform the network call.
|
||||
2. **Validate Response**:
|
||||
- If `response.statusCode == 200` (or 201 for POST), proceed to parsing.
|
||||
- If the status code indicates failure, throw an `Exception`.
|
||||
3. **Determine Parsing Strategy**:
|
||||
- If parsing a **small payload** (e.g., a single object), parse synchronously on the main thread.
|
||||
- If parsing a **large payload** (e.g., an array of thousands of objects), use `compute(parseFunction, response.body)` to parse in a background isolate.
|
||||
4. **Decode and Map**: Pass the decoded JSON to your model's `fromJson` constructor.
|
||||
|
||||
## Examples
|
||||
|
||||
### High-Fidelity Model Implementation
|
||||
|
||||
```dart
|
||||
import 'dart:convert';
|
||||
|
||||
class User {
|
||||
final int id;
|
||||
final String name;
|
||||
final String email;
|
||||
|
||||
const User({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.email,
|
||||
});
|
||||
|
||||
// Factory constructor for deserialization
|
||||
factory User.fromJson(Map<String, dynamic> json) {
|
||||
return switch (json) {
|
||||
{
|
||||
'id': int id,
|
||||
'name': String name,
|
||||
'email': String email,
|
||||
} =>
|
||||
User(
|
||||
id: id,
|
||||
name: name,
|
||||
email: email,
|
||||
),
|
||||
_ => throw const FormatException('Failed to load User.'),
|
||||
};
|
||||
}
|
||||
|
||||
// Method for serialization
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
'name': name,
|
||||
'email': email,
|
||||
};
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Synchronous Parsing (Small Payload)
|
||||
|
||||
```dart
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
Future<User> fetchUser(http.Client client, int userId) async {
|
||||
final response = await client.get(
|
||||
Uri.parse('https://api.example.com/users/$userId'),
|
||||
headers: {'Accept': 'application/json'},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
// Decode returns dynamic, cast to Map<String, dynamic>
|
||||
final Map<String, dynamic> jsonMap = jsonDecode(response.body) as Map<String, dynamic>;
|
||||
return User.fromJson(jsonMap);
|
||||
} else {
|
||||
throw Exception('Failed to load user');
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Background Parsing (Large Payload)
|
||||
|
||||
```dart
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
// Top-level function required for compute()
|
||||
List<User> parseUsers(String responseBody) {
|
||||
final parsed = (jsonDecode(responseBody) as List<dynamic>).cast<Map<String, dynamic>>();
|
||||
return parsed.map<User>((json) => User.fromJson(json)).toList();
|
||||
}
|
||||
|
||||
Future<List<User>> fetchUsers(http.Client client) async {
|
||||
final response = await client.get(
|
||||
Uri.parse('https://api.example.com/users'),
|
||||
headers: {'Accept': 'application/json'},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
// Offload expensive parsing to a background isolate
|
||||
return compute(parseUsers, response.body);
|
||||
} else {
|
||||
throw Exception('Failed to load users');
|
||||
}
|
||||
}
|
||||
```
|
||||
255
.agents/skills/flutter-setup-declarative-routing/SKILL.md
Normal file
255
.agents/skills/flutter-setup-declarative-routing/SKILL.md
Normal file
@@ -0,0 +1,255 @@
|
||||
---
|
||||
name: flutter-setup-declarative-routing
|
||||
description: Configure `MaterialApp.router` using a package like `go_router` for advanced URL-based navigation. Use when developing web applications or mobile apps that require specific deep linking and browser history support.
|
||||
metadata:
|
||||
model: models/gemini-3.1-pro-preview
|
||||
last_modified: Tue, 21 Apr 2026 21:08:03 GMT
|
||||
---
|
||||
# Implementing Routing and Deep Linking
|
||||
|
||||
## Contents
|
||||
- [Core Concepts](#core-concepts)
|
||||
- [Workflow: Initializing the Application and Router](#workflow-initializing-the-application-and-router)
|
||||
- [Workflow: Configuring Platform Deep Linking](#workflow-configuring-platform-deep-linking)
|
||||
- [Workflow: Implementing Nested Navigation](#workflow-implementing-nested-navigation)
|
||||
- [Examples](#examples)
|
||||
|
||||
## Core Concepts
|
||||
|
||||
Use the `go_router` package for declarative routing in Flutter. It provides a robust API for complex routing scenarios, deep linking, and nested navigation.
|
||||
|
||||
- **GoRouter**: The central configuration object defining the application's route tree.
|
||||
- **GoRoute**: A standard route mapping a URL path to a Flutter screen.
|
||||
- **ShellRoute / StatefulShellRoute**: Wraps child routes in a persistent UI shell (e.g., a `BottomNavigationBar`). `StatefulShellRoute` maintains the state of parallel navigation branches.
|
||||
- **Path URL Strategy**: Removes the default `#` fragment from web URLs, essential for clean deep linking across platforms.
|
||||
|
||||
## Workflow: Initializing the Application and Router
|
||||
|
||||
Follow this workflow to bootstrap a new Flutter application with `go_router` and configure the root routing mechanism.
|
||||
|
||||
### Task Progress
|
||||
- [ ] Create the Flutter application.
|
||||
- [ ] Add the `go_router` dependency.
|
||||
- [ ] Configure the URL strategy for web/deep linking.
|
||||
- [ ] Implement the `GoRouter` configuration.
|
||||
- [ ] Bind the router to `MaterialApp.router`.
|
||||
|
||||
### 1. Scaffold the Application
|
||||
Run the following commands to create the app and add the required routing package:
|
||||
```bash
|
||||
flutter create <app-name>
|
||||
cd <app-name>
|
||||
flutter pub add go_router
|
||||
```
|
||||
|
||||
### 2. Configure the Router
|
||||
Define a top-level `GoRouter` instance. Handle authentication or state-based routing using the `redirect` parameter.
|
||||
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:flutter_web_plugins/url_strategy.dart';
|
||||
|
||||
void main() {
|
||||
// Use path URL strategy to remove the '#' from web URLs
|
||||
usePathUrlStrategy();
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
final GoRouter _router = GoRouter(
|
||||
initialLocation: '/',
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/',
|
||||
builder: (context, state) => const HomeScreen(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: 'details/:id',
|
||||
builder: (context, state) => DetailsScreen(id: state.pathParameters['id']!),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
errorBuilder: (context, state) => ErrorScreen(error: state.error),
|
||||
);
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
const MyApp({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp.router(
|
||||
routerConfig: _router,
|
||||
title: 'Routing App',
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Workflow: Configuring Platform Deep Linking
|
||||
|
||||
Configure the native platforms to intercept specific URLs and route them into the Flutter application.
|
||||
|
||||
### Task Progress
|
||||
- [ ] Determine target platforms (iOS, Android, or both).
|
||||
- [ ] Apply conditional configuration for Android (Manifest + Asset Links).
|
||||
- [ ] Apply conditional configuration for iOS (Plist + Entitlements + AASA).
|
||||
- [ ] Run validator -> review errors -> fix.
|
||||
|
||||
### If configuring for Android:
|
||||
1. **Modify `AndroidManifest.xml`**: Add the intent filter inside the `<activity>` tag for `.MainActivity`.
|
||||
```xml
|
||||
<intent-filter android:autoVerify="true">
|
||||
<action android:name="android.intent.action.VIEW" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
<category android:name="android.intent.category.BROWSABLE" />
|
||||
<data android:scheme="http" android:host="yourdomain.com" />
|
||||
<data android:scheme="https" />
|
||||
</intent-filter>
|
||||
```
|
||||
2. **Host `assetlinks.json`**: Serve the following JSON at `https://yourdomain.com/.well-known/assetlinks.json`.
|
||||
```json
|
||||
[{
|
||||
"relation": ["delegate_permission/common.handle_all_urls"],
|
||||
"target": {
|
||||
"namespace": "android_app",
|
||||
"package_name": "com.yourcompany.yourapp",
|
||||
"sha256_cert_fingerprints": ["YOUR_SHA256_FINGERPRINT"]
|
||||
}
|
||||
}]
|
||||
```
|
||||
|
||||
### If configuring for iOS:
|
||||
1. **Modify `Info.plist`**: Opt-in to Flutter's default deep link handler.
|
||||
*Note: If using a third-party deep linking plugin (e.g., `app_links`), set this to `NO` to prevent conflicts.*
|
||||
```xml
|
||||
<key>FlutterDeepLinkingEnabled</key>
|
||||
<true/>
|
||||
```
|
||||
2. **Modify `Runner.entitlements`**: Add the associated domain.
|
||||
```xml
|
||||
<key>com.apple.developer.associated-domains</key>
|
||||
<array>
|
||||
<string>applinks:yourdomain.com</string>
|
||||
</array>
|
||||
```
|
||||
3. **Host `apple-app-site-association`**: Serve the following JSON (without a `.json` extension) at `https://yourdomain.com/.well-known/apple-app-site-association`.
|
||||
```json
|
||||
{
|
||||
"applinks": {
|
||||
"apps": [],
|
||||
"details": [{
|
||||
"appIDs": ["TEAM_ID.com.yourcompany.yourapp"],
|
||||
"paths": ["*"],
|
||||
"components": [{"/": "/*"}]
|
||||
}]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Validation Loop
|
||||
Run validator -> review errors -> fix.
|
||||
- **Android**: Test using ADB.
|
||||
```bash
|
||||
adb shell 'am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "https://yourdomain.com/details/123"' com.yourcompany.yourapp
|
||||
```
|
||||
- **iOS**: Test using `xcrun` on a booted simulator.
|
||||
```bash
|
||||
xcrun simctl openurl booted https://yourdomain.com/details/123
|
||||
```
|
||||
|
||||
## Workflow: Implementing Nested Navigation
|
||||
|
||||
Use `StatefulShellRoute` to implement persistent UI shells (like a bottom navigation bar) that maintain the state of their child routes.
|
||||
|
||||
### Task Progress
|
||||
- [ ] Define `StatefulShellRoute.indexedStack` in the `GoRouter` configuration.
|
||||
- [ ] Create `StatefulShellBranch` instances for each navigation tab.
|
||||
- [ ] Implement the shell widget using `StatefulNavigationShell`.
|
||||
|
||||
```dart
|
||||
final GoRouter _router = GoRouter(
|
||||
initialLocation: '/home',
|
||||
routes: [
|
||||
StatefulShellRoute.indexedStack(
|
||||
builder: (context, state, navigationShell) {
|
||||
return ScaffoldWithNavBar(navigationShell: navigationShell);
|
||||
},
|
||||
branches: [
|
||||
StatefulShellBranch(
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/home',
|
||||
builder: (context, state) => const HomeScreen(),
|
||||
),
|
||||
],
|
||||
),
|
||||
StatefulShellBranch(
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: '/settings',
|
||||
builder: (context, state) => const SettingsScreen(),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### High-Fidelity Shell Widget Implementation
|
||||
Implement the UI shell that consumes the `StatefulNavigationShell` to handle branch switching.
|
||||
|
||||
```dart
|
||||
class ScaffoldWithNavBar extends StatelessWidget {
|
||||
const ScaffoldWithNavBar({
|
||||
required this.navigationShell,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final StatefulNavigationShell navigationShell;
|
||||
|
||||
void _goBranch(int index) {
|
||||
navigationShell.goBranch(
|
||||
index,
|
||||
// Support navigating to the initial location when tapping the active tab.
|
||||
initialLocation: index == navigationShell.currentIndex,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: navigationShell,
|
||||
bottomNavigationBar: NavigationBar(
|
||||
selectedIndex: navigationShell.currentIndex,
|
||||
onDestinationSelected: _goBranch,
|
||||
destinations: const [
|
||||
NavigationDestination(icon: Icon(Icons.home), label: 'Home'),
|
||||
NavigationDestination(icon: Icon(Icons.settings), label: 'Settings'),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Programmatic Navigation
|
||||
Use the `context.go()` and `context.push()` extension methods provided by `go_router`.
|
||||
|
||||
```dart
|
||||
// Replaces the current route stack with the target route (Declarative)
|
||||
context.go('/details/123');
|
||||
|
||||
// Pushes the target route onto the existing stack (Imperative)
|
||||
context.push('/details/123');
|
||||
|
||||
// Navigates using a named route and path parameters
|
||||
context.goNamed('details', pathParameters: {'id': '123'});
|
||||
|
||||
// Pops the current route
|
||||
context.pop();
|
||||
```
|
||||
210
.agents/skills/flutter-setup-localization/SKILL.md
Normal file
210
.agents/skills/flutter-setup-localization/SKILL.md
Normal file
@@ -0,0 +1,210 @@
|
||||
---
|
||||
name: flutter-setup-localization
|
||||
description: Add `flutter_localizations` and `intl` dependencies, enable "generate true" in `pubspec.yaml`, and create an `l10n.yaml` configuration file. Use when initializing localization support for a new Flutter project.
|
||||
metadata:
|
||||
model: models/gemini-3.1-pro-preview
|
||||
last_modified: Tue, 21 Apr 2026 21:27:35 GMT
|
||||
---
|
||||
# Internationalizing Flutter Applications
|
||||
|
||||
## Contents
|
||||
- [Core Concepts](#core-concepts)
|
||||
- [Setup Workflow](#setup-workflow)
|
||||
- [Implementation Workflow](#implementation-workflow)
|
||||
- [Advanced Formatting](#advanced-formatting)
|
||||
- [Examples](#examples)
|
||||
|
||||
## Core Concepts
|
||||
Flutter handles internationalization (i18n) and localization (l10n) via the `flutter_localizations` and `intl` packages. The standard approach uses App Resource Bundle (`.arb`) files to define localized strings, which are then compiled into a generated `AppLocalizations` class for type-safe access within the widget tree.
|
||||
|
||||
## Setup Workflow
|
||||
|
||||
Copy and track this checklist when initializing internationalization in a Flutter project:
|
||||
|
||||
- [ ] **Task Progress**
|
||||
- [ ] 1. Add dependencies to `pubspec.yaml`.
|
||||
- [ ] 2. Enable the `generate` flag.
|
||||
- [ ] 3. Create the `l10n.yaml` configuration file.
|
||||
- [ ] 4. Configure `MaterialApp` or `CupertinoApp`.
|
||||
|
||||
### 1. Add Dependencies
|
||||
Add the required localization packages to the project. Execute the following commands in the terminal:
|
||||
```bash
|
||||
flutter pub add flutter_localizations --sdk=flutter
|
||||
flutter pub add intl:any
|
||||
```
|
||||
|
||||
Verify your `pubspec.yaml` includes the following under `dependencies`:
|
||||
```yaml
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
flutter_localizations:
|
||||
sdk: flutter
|
||||
intl: any
|
||||
```
|
||||
|
||||
### 2. Enable Code Generation
|
||||
Open `pubspec.yaml` and enable the `generate` flag within the `flutter` section to automate localization tasks:
|
||||
```yaml
|
||||
flutter:
|
||||
generate: true
|
||||
```
|
||||
|
||||
### 3. Create Configuration File
|
||||
Create a new file named `l10n.yaml` in the root directory of the Flutter project. Define the input directory, template file, and output file:
|
||||
```yaml
|
||||
arb-dir: lib/l10n
|
||||
template-arb-file: app_en.arb
|
||||
output-localization-file: app_localizations.dart
|
||||
synthetic-package: true
|
||||
```
|
||||
|
||||
### 4. Configure the App Entry Point
|
||||
Import the generated localizations and the `flutter_localizations` library in your `main.dart`. Inject the delegates and supported locales into your `MaterialApp` or `CupertinoApp`.
|
||||
|
||||
```dart
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; // Adjust path if synthetic-package is false
|
||||
|
||||
// ... inside build method
|
||||
return MaterialApp(
|
||||
localizationsDelegates: const [
|
||||
AppLocalizations.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
],
|
||||
supportedLocales: const [
|
||||
Locale('en'), // English
|
||||
Locale('es'), // Spanish
|
||||
],
|
||||
home: const MyHomePage(),
|
||||
);
|
||||
```
|
||||
|
||||
## Implementation Workflow
|
||||
|
||||
Follow this workflow when adding or modifying localized content.
|
||||
|
||||
### 1. Define ARB Files
|
||||
* **If creating NEW content:** Add the base string to the template file (`lib/l10n/app_en.arb`). Include a description for context.
|
||||
* **If EDITING existing content:** Locate the key in all supported `.arb` files and update the values.
|
||||
|
||||
```json
|
||||
{
|
||||
"helloWorld": "Hello World!",
|
||||
"@helloWorld": {
|
||||
"description": "The conventional newborn programmer greeting"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Create corresponding files for other locales (e.g., `app_es.arb`):
|
||||
```json
|
||||
{
|
||||
"helloWorld": "¡Hola Mundo!"
|
||||
}
|
||||
```
|
||||
|
||||
### 2. Generate Localization Classes
|
||||
Run the following command to trigger code generation:
|
||||
```bash
|
||||
flutter pub get
|
||||
```
|
||||
*Feedback Loop:* Run validator -> review terminal output for ARB syntax errors -> fix missing commas or mismatched placeholders -> re-run `flutter pub get`.
|
||||
|
||||
### 3. Consume Localized Strings
|
||||
Access the localized strings in your widget tree using `AppLocalizations.of(context)`. Ensure the widget calling this is a descendant of `MaterialApp`.
|
||||
|
||||
```dart
|
||||
Text(AppLocalizations.of(context)!.helloWorld)
|
||||
```
|
||||
|
||||
## Advanced Formatting
|
||||
|
||||
Use placeholders for dynamic data, plurals, and conditional selects.
|
||||
|
||||
### Placeholders
|
||||
Define parameters within curly braces and specify their type in the metadata object.
|
||||
```json
|
||||
"hello": "Hello {userName}",
|
||||
"@hello": {
|
||||
"description": "A message with a single parameter",
|
||||
"placeholders": {
|
||||
"userName": {
|
||||
"type": "String",
|
||||
"example": "Bob"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Plurals
|
||||
Use the `plural` syntax to handle quantity-based string variations. The `other` case is mandatory.
|
||||
```json
|
||||
"nWombats": "{count, plural, =0{no wombats} =1{1 wombat} other{{count} wombats}}",
|
||||
"@nWombats": {
|
||||
"description": "A plural message",
|
||||
"placeholders": {
|
||||
"count": {
|
||||
"type": "num",
|
||||
"format": "compact"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Selects
|
||||
Use the `select` syntax for conditional strings, such as gendered text.
|
||||
```json
|
||||
"pronoun": "{gender, select, male{he} female{she} other{they}}",
|
||||
"@pronoun": {
|
||||
"description": "A gendered message",
|
||||
"placeholders": {
|
||||
"gender": {
|
||||
"type": "String"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Complete `l10n.yaml`
|
||||
```yaml
|
||||
arb-dir: lib/l10n
|
||||
template-arb-file: app_en.arb
|
||||
output-localization-file: app_localizations.dart
|
||||
synthetic-package: true
|
||||
use-escaping: true
|
||||
```
|
||||
|
||||
### Complete Widget Implementation
|
||||
```dart
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
|
||||
class GreetingWidget extends StatelessWidget {
|
||||
final String userName;
|
||||
final int notificationCount;
|
||||
|
||||
const GreetingWidget({
|
||||
super.key,
|
||||
required this.userName,
|
||||
required this.notificationCount,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context)!;
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
Text(l10n.hello(userName)),
|
||||
Text(l10n.nWombats(notificationCount)),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
174
.agents/skills/flutter-use-http-package/SKILL.md
Normal file
174
.agents/skills/flutter-use-http-package/SKILL.md
Normal file
@@ -0,0 +1,174 @@
|
||||
---
|
||||
name: flutter-use-http-package
|
||||
description: Use the `http` package to execute GET, POST, PUT, or DELETE requests. Use when you need to fetch from or send data to a REST API.
|
||||
metadata:
|
||||
model: models/gemini-3.1-pro-preview
|
||||
last_modified: Tue, 21 Apr 2026 21:36:42 GMT
|
||||
---
|
||||
# Implementing Flutter Networking
|
||||
|
||||
## Contents
|
||||
- [Configuration & Permissions](#configuration--permissions)
|
||||
- [Request Execution & Response Handling](#request-execution--response-handling)
|
||||
- [Background Parsing](#background-parsing)
|
||||
- [Workflow: Executing Network Operations](#workflow-executing-network-operations)
|
||||
- [Examples](#examples)
|
||||
|
||||
## Configuration & Permissions
|
||||
|
||||
Configure the environment and platform-specific permissions required for network access.
|
||||
|
||||
1. Add the `http` package dependency via the terminal:
|
||||
```bash
|
||||
flutter pub add http
|
||||
```
|
||||
2. Import the package in your Dart files:
|
||||
```dart
|
||||
import 'package:http/http.dart' as http;
|
||||
```
|
||||
3. Configure Android permissions by adding the Internet permission to `android/app/src/main/AndroidManifest.xml`:
|
||||
```xml
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
```
|
||||
4. Configure macOS entitlements by adding the network client key to both `macos/Runner/DebugProfile.entitlements` and `macos/Runner/Release.entitlements`:
|
||||
```xml
|
||||
<key>com.apple.security.network.client</key>
|
||||
<true/>
|
||||
```
|
||||
|
||||
## Request Execution & Response Handling
|
||||
|
||||
Execute HTTP operations and map responses to strongly typed Dart objects.
|
||||
|
||||
* **URIs:** Always parse URL strings using `Uri.parse('your_url')`.
|
||||
* **Headers:** Inject authorization and content-type headers via the `headers` parameter map. Use `HttpHeaders.authorizationHeader` for auth tokens.
|
||||
* **Payloads:** For POST and PUT requests, encode the body using `jsonEncode()` from `dart:convert`.
|
||||
* **Status Validation:** Evaluate `response.statusCode`. Treat `200 OK` (GET/PUT/DELETE) and `201 CREATED` (POST) as success.
|
||||
* **Error Handling:** Throw explicit exceptions for non-success status codes. Never return `null` on failure, as this prevents `FutureBuilder` from triggering its error state and causes infinite loading indicators.
|
||||
* **Deserialization:** Parse the raw string using `jsonDecode(response.body)` and map it to a custom Dart object using a factory constructor (e.g., `fromJson`).
|
||||
|
||||
## Background Parsing
|
||||
|
||||
Offload expensive JSON parsing to a separate Isolate to prevent UI jank (frame drops).
|
||||
|
||||
* Import `package:flutter/foundation.dart`.
|
||||
* Use the `compute()` function to run the parsing logic in a background isolate.
|
||||
* Ensure the parsing function passed to `compute()` is a top-level function or a static method, as closures or instance methods cannot be passed across isolates.
|
||||
|
||||
## Workflow: Executing Network Operations
|
||||
|
||||
Use the following checklist to implement and validate network operations.
|
||||
|
||||
**Task Progress:**
|
||||
- [ ] 1. Define the strongly typed Dart model with a `fromJson` factory constructor.
|
||||
- [ ] 2. Implement the network request method returning a `Future<Model>`.
|
||||
- [ ] 3. Apply conditional logic based on the operation type:
|
||||
- **If fetching data (GET):** Append query parameters to the URI.
|
||||
- **If mutating data (POST/PUT):** Set `'Content-Type': 'application/json; charset=UTF-8'` and attach the `jsonEncode` body.
|
||||
- **If deleting data (DELETE):** Return an empty model instance on success (`200 OK`).
|
||||
- [ ] 4. Validate the `statusCode` and throw an `Exception` on failure.
|
||||
- [ ] 5. Integrate the `Future` into the UI using `FutureBuilder`.
|
||||
- [ ] 6. Handle `snapshot.hasData`, `snapshot.hasError`, and default to a `CircularProgressIndicator`.
|
||||
- [ ] 7. **Feedback Loop:** Run the app -> trigger the network request -> review console for unhandled exceptions -> fix parsing or permission errors.
|
||||
|
||||
## Examples
|
||||
|
||||
### High-Fidelity Implementation: Fetching and Parsing in the Background
|
||||
|
||||
```dart
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
// 1. Top-level parsing function for Isolate
|
||||
List<Photo> parsePhotos(String responseBody) {
|
||||
final parsed = (jsonDecode(responseBody) as List<Object?>)
|
||||
.cast<Map<String, Object?>>();
|
||||
return parsed.map<Photo>(Photo.fromJson).toList();
|
||||
}
|
||||
|
||||
// 2. Network execution with background parsing
|
||||
Future<List<Photo>> fetchPhotos() async {
|
||||
final response = await http.get(
|
||||
Uri.parse('https://jsonplaceholder.typicode.com/photos'),
|
||||
headers: {
|
||||
HttpHeaders.authorizationHeader: 'Bearer your_token_here',
|
||||
HttpHeaders.acceptHeader: 'application/json',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
// Offload heavy parsing to a background isolate
|
||||
return compute(parsePhotos, response.body);
|
||||
} else {
|
||||
throw Exception('Failed to load photos. Status: ${response.statusCode}');
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Strongly typed model
|
||||
class Photo {
|
||||
final int id;
|
||||
final String title;
|
||||
final String thumbnailUrl;
|
||||
|
||||
const Photo({
|
||||
required this.id,
|
||||
required this.title,
|
||||
required this.thumbnailUrl,
|
||||
});
|
||||
|
||||
factory Photo.fromJson(Map<String, dynamic> json) {
|
||||
return Photo(
|
||||
id: json['id'] as int,
|
||||
title: json['title'] as String,
|
||||
thumbnailUrl: json['thumbnailUrl'] as String,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. UI Integration
|
||||
class PhotoGallery extends StatefulWidget {
|
||||
const PhotoGallery({super.key});
|
||||
|
||||
@override
|
||||
State<PhotoGallery> createState() => _PhotoGalleryState();
|
||||
}
|
||||
|
||||
class _PhotoGalleryState extends State<PhotoGallery> {
|
||||
late Future<List<Photo>> _futurePhotos;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Initialize Future once to prevent re-fetching on rebuilds
|
||||
_futurePhotos = fetchPhotos();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<List<Photo>>(
|
||||
future: _futurePhotos,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
final photos = snapshot.data!;
|
||||
return ListView.builder(
|
||||
itemCount: photos.length,
|
||||
itemBuilder: (context, index) => ListTile(
|
||||
leading: Image.network(photos[index].thumbnailUrl),
|
||||
title: Text(photos[index].title),
|
||||
),
|
||||
);
|
||||
} else if (snapshot.hasError) {
|
||||
return Center(child: Text('Error: ${snapshot.error}'));
|
||||
}
|
||||
|
||||
// Default loading state
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
```
|
||||
65
skills-lock.json
Normal file
65
skills-lock.json
Normal file
@@ -0,0 +1,65 @@
|
||||
{
|
||||
"version": 1,
|
||||
"skills": {
|
||||
"flutter-add-integration-test": {
|
||||
"source": "flutter/skills",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/flutter-add-integration-test/SKILL.md",
|
||||
"computedHash": "4246d3ef5f21bdb7945899056b99cf3863e2bff645a132b41634caaded68da6d"
|
||||
},
|
||||
"flutter-add-widget-preview": {
|
||||
"source": "flutter/skills",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/flutter-add-widget-preview/SKILL.md",
|
||||
"computedHash": "369ed3ebdc1f81ee337551ad1d1dd9ec6e768ed2bdf65d32ad442117a6ba79b6"
|
||||
},
|
||||
"flutter-add-widget-test": {
|
||||
"source": "flutter/skills",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/flutter-add-widget-test/SKILL.md",
|
||||
"computedHash": "f4ea905ae155d1bca76f5431bf6ed31f31e3d40146493e7ae4285eac39ba4ffd"
|
||||
},
|
||||
"flutter-apply-architecture-best-practices": {
|
||||
"source": "flutter/skills",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/flutter-apply-architecture-best-practices/SKILL.md",
|
||||
"computedHash": "baeb208b1cab90c559677626dbd101b96ba93f803cbed85122abdadeb3283a8b"
|
||||
},
|
||||
"flutter-build-responsive-layout": {
|
||||
"source": "flutter/skills",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/flutter-build-responsive-layout/SKILL.md",
|
||||
"computedHash": "6d74a9504c4e355c4c62f6fcb6c6dc0df67b56dd6616bea199e4e58f75b7729b"
|
||||
},
|
||||
"flutter-fix-layout-issues": {
|
||||
"source": "flutter/skills",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/flutter-fix-layout-issues/SKILL.md",
|
||||
"computedHash": "b2f9789451224e6df8d1d7ac63854c2f1beb30fa35d13ea06fbae9ba2b1b0a7f"
|
||||
},
|
||||
"flutter-implement-json-serialization": {
|
||||
"source": "flutter/skills",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/flutter-implement-json-serialization/SKILL.md",
|
||||
"computedHash": "c2cf46854472a452dafa11f862f2bca3d9fe7286a5bb45d85b1efbcebc74b74e"
|
||||
},
|
||||
"flutter-setup-declarative-routing": {
|
||||
"source": "flutter/skills",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/flutter-setup-declarative-routing/SKILL.md",
|
||||
"computedHash": "4c2ed2fd729230be581b15d84741688e206632fb2b38af320060cd3518b91179"
|
||||
},
|
||||
"flutter-setup-localization": {
|
||||
"source": "flutter/skills",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/flutter-setup-localization/SKILL.md",
|
||||
"computedHash": "fc0811b1b775c52c1b8f7df600f5c52b83029aea0586b8cc55169812affd408f"
|
||||
},
|
||||
"flutter-use-http-package": {
|
||||
"source": "flutter/skills",
|
||||
"sourceType": "github",
|
||||
"skillPath": "skills/flutter-use-http-package/SKILL.md",
|
||||
"computedHash": "bd169b5cee731751f3b32f43c94cfe9495fc8e6c3eb621b785c69e38b77d0e19"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user