From 846e39e97966f7b2a5d96d85b4e922795e6b0765 Mon Sep 17 00:00:00 2001 From: Chun-Heng Tai Date: Wed, 2 Dec 2026 12:11:56 -0800 Subject: [PATCH] Guides agent away from deprecated material and cupertino libraries --- resources/flutter_skills.yaml | 22 ++++++-------- rules/flutter-ui-packages.md | 30 +++++++++++++++++++ rules/flutter-ui-packages.mdc | 30 +++++++++++++++++++ skills/flutter-add-integration-test/SKILL.md | 2 +- skills/flutter-add-widget-preview/SKILL.md | 6 ++-- skills/flutter-add-widget-test/SKILL.md | 4 +-- .../flutter-build-responsive-layout/SKILL.md | 4 +-- .../SKILL.md | 8 ++--- skills/flutter-setup-localization/SKILL.md | 10 +++---- skills/flutter-use-http-package/SKILL.md | 2 +- 10 files changed, 86 insertions(+), 32 deletions(-) create mode 100644 rules/flutter-ui-packages.md create mode 100644 rules/flutter-ui-packages.mdc diff --git a/resources/flutter_skills.yaml b/resources/flutter_skills.yaml index f055e473..f84858c1 100644 --- a/resources/flutter_skills.yaml +++ b/resources/flutter_skills.yaml @@ -107,10 +107,10 @@ 1. Defining Widget Previews: * To preview a widget, use the `@Preview` annotation defined in `package:flutter/widget_previews.dart`. * This annotation can be applied to top-level functions that return a `Widget` or `WidgetBuilder`, static methods within a class that return a `Widget` or `WidgetBuilder`, or public `Widget` constructors and factories with no required arguments. - * A basic example involves importing `package:flutter/widget_previews.dart` and `package:flutter/material.dart`, then defining a function annotated with `@Preview` that returns a `Widget`, such as a `Text` widget. For instance: + * A basic example involves importing `package:flutter/widget_previews.dart` and `package:flutter/widgets.dart` (or `package:material_ui/material_ui.dart` / `package:cupertino_ui/cupertino_ui.dart` when previewing design widgets), then defining a function annotated with `@Preview` that returns a `Widget`, such as a `Text` widget. For instance: ```dart import 'package:flutter/widget_previews.dart'; - import 'package:flutter/material.dart'; + import 'package:flutter/widgets.dart'; @Preview(name: 'My Sample Text') Widget mySampleText() { @@ -234,11 +234,11 @@ 1. **Create a new Flutter application**: * Use the command `flutter create `. For example, `flutter create deeplink_cookbook`. - 2. **Add the `go_router` package as a dependency**: - * Run `flutter pub add go_router`. The Flutter team maintains the `go_router` package, which offers a simple API for complex routing scenarios. + 2. **Add the `go_router` and `material_ui` packages as dependencies**: + * Run `flutter pub add go_router material_ui`. The Flutter team maintains the `go_router` package, which offers a simple API for complex routing scenarios. 3. **Configure `MaterialApp.router` with a `GoRouter` object**: - * In your `main.dart` file, import `package:go_router/go_router.dart'` and `package:flutter/material.dart'`. + * In your `main.dart` file, import `package:go_router/go_router.dart` and `package:material_ui/material_ui.dart`. * Create a `GoRouter` object that defines your application's routes. This object will handle routing, including paths like '/' and '/details'. * Set up your `main` function to run `MaterialApp.router` and provide it with the `routerConfig` property, which should be your `GoRouter` object. * For example, you can define routes within the `GoRouter` object using `GoRoute` for different paths and their corresponding builders. @@ -352,18 +352,14 @@ * This file can be used to specify options like `synthetic-package: false` to generate localized messages directly into source files instead of a synthetic package. You can also specify `arb-dir` or `output-dir` in this file. 4. **Configure `MaterialApp` or `CupertinoApp`**: - * Import the `flutter_localizations` library. - * Specify `localizationsDelegates` and `supportedLocales` for your `MaterialApp` or `CupertinoApp`. For example: + * Import `package:material_ui/material_ui.dart` (for `MaterialApp`) or `package:cupertino_ui/cupertino_ui.dart` (for `CupertinoApp`), rather than importing Material or Cupertino delegates from `flutter_localizations`. + * Specify `localizationsDelegates` and `supportedLocales` for your `MaterialApp` or `CupertinoApp` using `GlobalMaterialLocalizations.delegates` or `GlobalCupertinoLocalizations.delegates`. For example: ```dart - import 'package:flutter_localizations/flutter_localizations.dart'; + import 'package:material_ui/material_ui.dart'; // ... return const MaterialApp( title: 'Localizations Sample App', - localizationsDelegates: [ - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, - ], + localizationsDelegates: GlobalMaterialLocalizations.delegates, supportedLocales: [ Locale('en'), // English Locale('es'), // Spanish diff --git a/rules/flutter-ui-packages.md b/rules/flutter-ui-packages.md new file mode 100644 index 00000000..d656147e --- /dev/null +++ b/rules/flutter-ui-packages.md @@ -0,0 +1,30 @@ +--- +description: Use standalone material_ui and cupertino_ui packages for Material Design and Cupertino APIs in Flutter code instead of legacy SDK imports. +trigger: glob +globs: "**/*.dart" +--- + +# Use Standalone Material and Cupertino Packages + +Material Design and Cupertino libraries live in the standalone `material_ui` and `cupertino_ui` packages, separate from the core `flutter` SDK package. + +When writing or editing Flutter `.dart` code: + +1. **Material Design (`material_ui`)**: + - Add `material_ui` to `pubspec.yaml` (`flutter pub add material_ui`) if not already present. + - Import `package:material_ui/material_ui.dart`. Do not import `package:flutter/material.dart`. + +2. **Cupertino (`cupertino_ui`)**: + - Add `cupertino_ui` to `pubspec.yaml` (`flutter pub add cupertino_ui`) if not already present. + - Import `package:cupertino_ui/cupertino_ui.dart`. Do not import `package:flutter/cupertino.dart`. + +3. **Core Widgets**: + - When a file only uses design-agnostic framework APIs (such as `Widget`, `StatelessWidget`, `BuildContext`, `Key`, or layout primitives), import `package:flutter/widgets.dart` instead of adding a design package import. + +4. **Localization Delegates**: + - Import `GlobalMaterialLocalizations` from `package:material_ui/material_ui.dart` (typically via `GlobalMaterialLocalizations.delegates`) and `GlobalCupertinoLocalizations` from `package:cupertino_ui/cupertino_ui.dart` (typically via `GlobalCupertinoLocalizations.delegates`). + - Do not import `GlobalMaterialLocalizations` or `GlobalCupertinoLocalizations` from `package:flutter_localizations/flutter_localizations.dart`. Use `flutter_localizations` only for `GlobalWidgetsLocalizations` and SDK localization tooling. + +5. **Scope and Older SDK Projects**: + - Skip this rule for pure Dart packages without Flutter UI. + - If making targeted edits in a project pinned to an older Flutter SDK that still uses `package:flutter/material.dart` or `package:flutter/cupertino.dart`, avoid mixing types from those legacy SDK libraries with `material_ui` or `cupertino_ui` types in the same widget subtree unless migrating the project. diff --git a/rules/flutter-ui-packages.mdc b/rules/flutter-ui-packages.mdc new file mode 100644 index 00000000..57bf6f02 --- /dev/null +++ b/rules/flutter-ui-packages.mdc @@ -0,0 +1,30 @@ +--- +description: Use standalone material_ui and cupertino_ui packages for Material Design and Cupertino APIs in Flutter code instead of legacy SDK imports. +globs: "**/*.dart" +alwaysApply: false +--- + +# Use Standalone Material and Cupertino Packages + +Material Design and Cupertino libraries live in the standalone `material_ui` and `cupertino_ui` packages, separate from the core `flutter` SDK package. + +When writing or editing Flutter `.dart` code: + +1. **Material Design (`material_ui`)**: + - Add `material_ui` to `pubspec.yaml` (`flutter pub add material_ui`) if not already present. + - Import `package:material_ui/material_ui.dart`. Do not import `package:flutter/material.dart`. + +2. **Cupertino (`cupertino_ui`)**: + - Add `cupertino_ui` to `pubspec.yaml` (`flutter pub add cupertino_ui`) if not already present. + - Import `package:cupertino_ui/cupertino_ui.dart`. Do not import `package:flutter/cupertino.dart`. + +3. **Core Widgets**: + - When a file only uses design-agnostic framework APIs (such as `Widget`, `StatelessWidget`, `BuildContext`, `Key`, or layout primitives), import `package:flutter/widgets.dart` instead of adding a design package import. + +4. **Localization Delegates**: + - Import `GlobalMaterialLocalizations` from `package:material_ui/material_ui.dart` (typically via `GlobalMaterialLocalizations.delegates`) and `GlobalCupertinoLocalizations` from `package:cupertino_ui/cupertino_ui.dart` (typically via `GlobalCupertinoLocalizations.delegates`). + - Do not import `GlobalMaterialLocalizations` or `GlobalCupertinoLocalizations` from `package:flutter_localizations/flutter_localizations.dart`. Use `flutter_localizations` only for `GlobalWidgetsLocalizations` and SDK localization tooling. + +5. **Scope and Older SDK Projects**: + - Skip this rule for pure Dart packages without Flutter UI. + - If making targeted edits in a project pinned to an older Flutter SDK that still uses `package:flutter/material.dart` or `package:flutter/cupertino.dart`, avoid mixing types from those legacy SDK libraries with `material_ui` or `cupertino_ui` types in the same widget subtree unless migrating the project. diff --git a/skills/flutter-add-integration-test/SKILL.md b/skills/flutter-add-integration-test/SKILL.md index 60902f1a..0e986239 100644 --- a/skills/flutter-add-integration-test/SKILL.md +++ b/skills/flutter-add-integration-test/SKILL.md @@ -93,7 +93,7 @@ Copy and follow this checklist to implement and verify integration tests. ### Standard Integration Test (`integration_test/app_test.dart`) ```dart -import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; import 'package:my_app/main.dart'; diff --git a/skills/flutter-add-widget-preview/SKILL.md b/skills/flutter-add-widget-preview/SKILL.md index 89db1321..83e16b6b 100644 --- a/skills/flutter-add-widget-preview/SKILL.md +++ b/skills/flutter-add-widget-preview/SKILL.md @@ -83,7 +83,7 @@ Follow the appropriate conditional workflow to launch and interact with the Widg ### Basic Preview ```dart import 'package:flutter/widget_previews.dart'; -import 'package:flutter/material.dart'; +import 'package:material_ui/material_ui.dart'; @Preview(name: 'Submit Button', group: 'Form Controls') Widget submitButtonPreview() => const ElevatedButton( @@ -95,7 +95,7 @@ Widget submitButtonPreview() => const ElevatedButton( ### Custom Preview with PreviewThemeData and Transformation ```dart import 'package:flutter/widget_previews.dart'; -import 'package:flutter/material.dart'; +import 'package:material_ui/material_ui.dart'; final class ThemedPreview extends Preview { const ThemedPreview({ @@ -156,7 +156,7 @@ Widget themedButtonPreview() => const ElevatedButton( ### MultiPreview Implementation ```dart import 'package:flutter/widget_previews.dart'; -import 'package:flutter/material.dart'; +import 'package:material_ui/material_ui.dart'; /// Creates light and dark mode previews automatically. final class MultiBrightnessPreview extends MultiPreview { diff --git a/skills/flutter-add-widget-test/SKILL.md b/skills/flutter-add-widget-test/SKILL.md index 01ac7ac6..e54a51be 100644 --- a/skills/flutter-add-widget-test/SKILL.md +++ b/skills/flutter-add-widget-test/SKILL.md @@ -65,7 +65,7 @@ Apply the following conditional logic based on the type of interaction or state **Target Widget (`lib/todo_list.dart`):** ```dart -import 'package:flutter/material.dart'; +import 'package:material_ui/material_ui.dart'; class TodoList extends StatefulWidget { const TodoList({super.key}); @@ -117,8 +117,8 @@ class _TodoListState extends State { **Test Implementation (`test/todo_list_test.dart`):** ```dart -import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; import 'package:my_app/todo_list.dart'; void main() { diff --git a/skills/flutter-build-responsive-layout/SKILL.md b/skills/flutter-build-responsive-layout/SKILL.md index b85bfd7e..f6dd824a 100644 --- a/skills/flutter-build-responsive-layout/SKILL.md +++ b/skills/flutter-build-responsive-layout/SKILL.md @@ -70,7 +70,7 @@ Follow this workflow to prevent UI elements from stretching unnaturally on large Demonstrates switching between a mobile and desktop layout based on available width. ```dart -import 'package:flutter/material.dart'; +import 'package:material_ui/material_ui.dart'; const double largeScreenMinWidth = 600.0; @@ -110,7 +110,7 @@ class AdaptiveLayout extends StatelessWidget { Demonstrates preventing a widget from consuming all horizontal space. ```dart -import 'package:flutter/material.dart'; +import 'package:material_ui/material_ui.dart'; class ConstrainedContent extends StatelessWidget { const ConstrainedContent({super.key}); diff --git a/skills/flutter-setup-declarative-routing/SKILL.md b/skills/flutter-setup-declarative-routing/SKILL.md index 27203118..29497174 100644 --- a/skills/flutter-setup-declarative-routing/SKILL.md +++ b/skills/flutter-setup-declarative-routing/SKILL.md @@ -29,7 +29,7 @@ Follow this workflow to bootstrap a new Flutter application with `go_router` and ### Task Progress - [ ] Create the Flutter application. -- [ ] Add the `go_router` dependency. +- [ ] Add the `go_router` and `material_ui` dependencies. - [ ] Configure the URL strategy for web/deep linking. - [ ] Implement the `GoRouter` configuration. - [ ] Bind the router to `MaterialApp.router`. @@ -39,16 +39,16 @@ Run the following commands to create the app and add the required routing packag ```bash flutter create cd -flutter pub add go_router +flutter pub add go_router material_ui ``` ### 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'; +import 'package:go_router/go_router.dart'; +import 'package:material_ui/material_ui.dart'; void main() { // Use path URL strategy to remove the '#' from web URLs diff --git a/skills/flutter-setup-localization/SKILL.md b/skills/flutter-setup-localization/SKILL.md index d3dd4596..fd835d40 100644 --- a/skills/flutter-setup-localization/SKILL.md +++ b/skills/flutter-setup-localization/SKILL.md @@ -61,19 +61,17 @@ 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`. +Import the generated localizations alongside `package:material_ui/material_ui.dart` (or `package:cupertino_ui/cupertino_ui.dart` for a `CupertinoApp`) in your `main.dart`. Pass `GlobalMaterialLocalizations.delegates` (which bundles the Material, Cupertino, and Widgets delegates) or `GlobalCupertinoLocalizations.delegates` into `localizationsDelegates` rather than importing Material or Cupertino delegates from `flutter_localizations`. ```dart -import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; // Adjust path if synthetic-package is false +import 'package:material_ui/material_ui.dart'; // ... inside build method return MaterialApp( localizationsDelegates: const [ AppLocalizations.delegate, - GlobalMaterialLocalizations.delegate, - GlobalWidgetsLocalizations.delegate, - GlobalCupertinoLocalizations.delegate, + ...GlobalMaterialLocalizations.delegates, ], supportedLocales: const [ Locale('en'), // English @@ -182,7 +180,7 @@ use-escaping: true ### Complete Widget Implementation ```dart -import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; class GreetingWidget extends StatelessWidget { diff --git a/skills/flutter-use-http-package/SKILL.md b/skills/flutter-use-http-package/SKILL.md index bb60468d..2b38b64a 100644 --- a/skills/flutter-use-http-package/SKILL.md +++ b/skills/flutter-use-http-package/SKILL.md @@ -80,8 +80,8 @@ 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; +import 'package:material_ui/material_ui.dart'; // 1. Top-level parsing function for Isolate List parsePhotos(String responseBody) {