Repository: rrousselGit/flutter_hooks
Pull request #423 (2024-03-22 10:56:06)

Fix depreciation

changeset-bot[bot]: ###  ⚠️  No Changeset found

Latest commit: 219990041cb7e2129a1c0a437f829de683c5b363

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. **If these changes should result in a version bump, you need to add a changeset.**



[Click here to learn what changesets are, and how to add one](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add a changeset to this PR](https://github.com/rrousselGit/flutter_hooks/new/update-deprecated?filename=.changeset/perfect-eels-raise.md&value=---%0A%22%40fake-scope%2Ffake-pkg%22%3A%20patch%0A---%0A%0AFix%20depreciation%0A)



Patch:
diff --git a/README.md b/README.md
index 6fb701f..424d118 100644
--- a/README.md
+++ b/README.md
@@ -354,7 +354,7 @@ A series of hooks with no particular theme.
 | [useAutomaticKeepAlive](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useAutomaticKeepAlive.html)                 | An equivalent to the `AutomaticKeepAlive` widget for hooks.                |
 | [useOnPlatformBrightnessChange](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useOnPlatformBrightnessChange.html) | Listens to platform `Brightness` changes and triggers a callback on change.|
 | [useSearchController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useSearchController.html)                     | Creates and disposes a `SearchController`.                                 |
-| [useMaterialStatesController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useMaterialStatesController.html)     | Creates and disposes a `MaterialStatesController`.                         |
+| [useWidgetStatesController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useWidgetStatesController.html)     | Creates and disposes a `WidgetStatesController`.                         |
 | [useExpansionTileController](https://api.flutter.dev/flutter/material/ExpansionTileController-class.html)                            | Creates a `ExpansionTileController`.                                       |
 | [useDebounced](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useDebounced.html)                             | Returns a debounced version of the provided value, triggering widget updates accordingly after a specified timeout duration  |
 
diff --git a/packages/flutter_hooks/CHANGELOG.md b/packages/flutter_hooks/CHANGELOG.md
index 3f29f42..571233f 100644
--- a/packages/flutter_hooks/CHANGELOG.md
+++ b/packages/flutter_hooks/CHANGELOG.md
@@ -1,3 +1,7 @@
+## Unreleased minor
+
+- Renamed `useMaterialStatesController` to `useWidgetStatesController` to follow the rename in Flutter.
+
 ## 0.20.5 - 2024-02-05
 
 - Deprecate the `useIsMounted` hook as you should use `BuildContext.mounted` instead if you're on Flutter 3.7.0 or greater
@@ -10,7 +14,7 @@
 ## 0.20.3 - 2023-10-10
 
 - Added `useExpansionTileController` (thanks to @droidbg)
-- Added `useMaterialStateController` (thanks to @AdamHavlicek)
+- Added `useMaterialStatesController` (thanks to @AdamHavlicek)
 
 ## 0.20.2 - 2023-10-02
 
diff --git a/packages/flutter_hooks/example/lib/star_wars/planet_screen.dart b/packages/flutter_hooks/example/lib/star_wars/planet_screen.dart
index 90852d6..0640b58 100644
--- a/packages/flutter_hooks/example/lib/star_wars/planet_screen.dart
+++ b/packages/flutter_hooks/example/lib/star_wars/planet_screen.dart
@@ -107,7 +107,7 @@ class _Error extends StatelessWidget {
         if (errorMsg != null) Text(errorMsg!),
         ElevatedButton(
           style: ButtonStyle(
-            backgroundColor: MaterialStateProperty.all(Colors.redAccent),
+            backgroundColor: WidgetStateProperty.all(Colors.redAccent),
           ),
           onPressed: () async {
             await Provider.of<_PlanetHandler>(
diff --git a/packages/flutter_hooks/lib/src/focus_node.dart b/packages/flutter_hooks/lib/src/focus_node.dart
index 5b22582..5dfb3e0 100644
--- a/packages/flutter_hooks/lib/src/focus_node.dart
+++ b/packages/flutter_hooks/lib/src/focus_node.dart
@@ -6,7 +6,6 @@ part of 'hooks.dart';
 /// - [FocusNode]
 FocusNode useFocusNode({
   String? debugLabel,
-  FocusOnKeyCallback? onKey,
   FocusOnKeyEventCallback? onKeyEvent,
   bool skipTraversal = false,
   bool canRequestFocus = true,
@@ -15,7 +14,6 @@ FocusNode useFocusNode({
   return use(
     _FocusNodeHook(
       debugLabel: debugLabel,
-      onKey: onKey,
       onKeyEvent: onKeyEvent,
       skipTraversal: skipTraversal,
       canRequestFocus: canRequestFocus,
@@ -27,7 +25,6 @@ FocusNode useFocusNode({
 class _FocusNodeHook extends Hook<FocusNode> {
   const _FocusNodeHook({
     this.debugLabel,
-    this.onKey,
     this.onKeyEvent,
     required this.skipTraversal,
     required this.canRequestFocus,
@@ -35,7 +32,6 @@ class _FocusNodeHook extends Hook<FocusNode> {
   });
 
   final String? debugLabel;
-  final FocusOnKeyCallback? onKey;
   final FocusOnKeyEventCallback? onKeyEvent;
   final bool skipTraversal;
   final bool canRequestFocus;
@@ -50,7 +46,6 @@ class _FocusNodeHook extends Hook<FocusNode> {
 class _FocusNodeHookState extends HookState<FocusNode, _FocusNodeHook> {
   late final FocusNode _focusNode = FocusNode(
     debugLabel: hook.debugLabel,
-    onKey: hook.onKey,
     onKeyEvent: hook.onKeyEvent,
     skipTraversal: hook.skipTraversal,
     canRequestFocus: hook.canRequestFocus,
@@ -64,7 +59,6 @@ class _FocusNodeHookState extends HookState<FocusNode, _FocusNodeHook> {
       ..skipTraversal = hook.skipTraversal
       ..canRequestFocus = hook.canRequestFocus
       ..descendantsAreFocusable = hook.descendantsAreFocusable
-      ..onKey = hook.onKey
       ..onKeyEvent = hook.onKeyEvent;
   }
 
diff --git a/packages/flutter_hooks/lib/src/focus_scope_node.dart b/packages/flutter_hooks/lib/src/focus_scope_node.dart
index 297a1bb..8e1f84c 100644
--- a/packages/flutter_hooks/lib/src/focus_scope_node.dart
+++ b/packages/flutter_hooks/lib/src/focus_scope_node.dart
@@ -6,7 +6,6 @@ part of 'hooks.dart';
 /// - [FocusScopeNode]
 FocusScopeNode useFocusScopeNode({
   String? debugLabel,
-  FocusOnKeyCallback? onKey,
   FocusOnKeyEventCallback? onKeyEvent,
   bool skipTraversal = false,
   bool canRequestFocus = true,
@@ -14,7 +13,6 @@ FocusScopeNode useFocusScopeNode({
   return use(
     _FocusScopeNodeHook(
       debugLabel: debugLabel,
-      onKey: onKey,
       onKeyEvent: onKeyEvent,
       skipTraversal: skipTraversal,
       canRequestFocus: canRequestFocus,
@@ -25,14 +23,12 @@ FocusScopeNode useFocusScopeNode({
 class _FocusScopeNodeHook extends Hook<FocusScopeNode> {
   const _FocusScopeNodeHook({
     this.debugLabel,
-    this.onKey,
     this.onKeyEvent,
     required this.skipTraversal,
     required this.canRequestFocus,
   });
 
   final String? debugLabel;
-  final FocusOnKeyCallback? onKey;
   final FocusOnKeyEventCallback? onKeyEvent;
   final bool skipTraversal;
   final bool canRequestFocus;
@@ -47,7 +43,6 @@ class _FocusScopeNodeHookState
     extends HookState<FocusScopeNode, _FocusScopeNodeHook> {
   late final FocusScopeNode _focusScopeNode = FocusScopeNode(
     debugLabel: hook.debugLabel,
-    onKey: hook.onKey,
     onKeyEvent: hook.onKeyEvent,
     skipTraversal: hook.skipTraversal,
     canRequestFocus: hook.canRequestFocus,
@@ -59,7 +54,6 @@ class _FocusScopeNodeHookState
       ..debugLabel = hook.debugLabel
       ..skipTraversal = hook.skipTraversal
       ..canRequestFocus = hook.canRequestFocus
-      ..onKey = hook.onKey
       ..onKeyEvent = hook.onKeyEvent;
   }
 
diff --git a/packages/flutter_hooks/lib/src/hooks.dart b/packages/flutter_hooks/lib/src/hooks.dart
index 7b6cfbf..859df56 100644
--- a/packages/flutter_hooks/lib/src/hooks.dart
+++ b/packages/flutter_hooks/lib/src/hooks.dart
@@ -5,10 +5,10 @@ import 'package:flutter/material.dart'
     show
         Brightness,
         ExpansionTileController,
-        MaterialState,
-        MaterialStatesController,
+        WidgetStatesController,
         SearchController,
-        TabController;
+        TabController,
+        WidgetState;
 import 'package:flutter/scheduler.dart';
 import 'package:flutter/widgets.dart';
 
@@ -32,5 +32,5 @@ part 'tab_controller.dart';
 part 'text_controller.dart';
 part 'transformation_controller.dart';
 part 'widgets_binding_observer.dart';
-part 'material_states_controller.dart';
+part 'widget_states_controller.dart';
 part 'debounced.dart';
diff --git a/packages/flutter_hooks/lib/src/material_states_controller.dart b/packages/flutter_hooks/lib/src/material_states_controller.dart
deleted file mode 100644
index 45aee2e..0000000
--- a/packages/flutter_hooks/lib/src/material_states_controller.dart
+++ /dev/null
@@ -1,44 +0,0 @@
-part of 'hooks.dart';
-
-/// Creates a [MaterialStatesController] that will be disposed automatically.
-///
-/// See also:
-/// - [MaterialStatesController]
-MaterialStatesController useMaterialStatesController({
-  Set<MaterialState>? values,
-  List<Object?>? keys,
-}) {
-  return use(
-    _MaterialStatesControllerHook(
-      values: values,
-      keys: keys,
-    ),
-  );
-}
-
-class _MaterialStatesControllerHook extends Hook<MaterialStatesController> {
-  const _MaterialStatesControllerHook({
-    required this.values,
-    super.keys,
-  });
-
-  final Set<MaterialState>? values;
-
-  @override
-  HookState<MaterialStatesController, Hook<MaterialStatesController>>
-      createState() => _MaterialStateControllerHookState();
-}
-
-class _MaterialStateControllerHookState
-    extends HookState<MaterialStatesController, _MaterialStatesControllerHook> {
-  late final controller = MaterialStatesController(hook.values);
-
-  @override
-  MaterialStatesController build(BuildContext context) => controller;
-
-  @override
-  void dispose() => controller.dispose();
-
-  @override
-  String get debugLabel => 'useMaterialStatesController';
-}
diff --git a/packages/flutter_hooks/lib/src/widget_states_controller.dart b/packages/flutter_hooks/lib/src/widget_states_controller.dart
new file mode 100644
index 0000000..eab0ea2
--- /dev/null
+++ b/packages/flutter_hooks/lib/src/widget_states_controller.dart
@@ -0,0 +1,44 @@
+part of 'hooks.dart';
+
+/// Creates a [WidgetStatesController] that will be disposed automatically.
+///
+/// See also:
+/// - [WidgetStatesController]
+WidgetStatesController useWidgetStatesController({
+  Set<WidgetState>? values,
+  List<Object?>? keys,
+}) {
+  return use(
+    _WidgetStatesControllerHook(
+      values: values,
+      keys: keys,
+    ),
+  );
+}
+
+class _WidgetStatesControllerHook extends Hook<WidgetStatesController> {
+  const _WidgetStatesControllerHook({
+    required this.values,
+    super.keys,
+  });
+
+  final Set<WidgetState>? values;
+
+  @override
+  HookState<WidgetStatesController, Hook<WidgetStatesController>>
+      createState() => _WidgetStateControllerHookState();
+}
+
+class _WidgetStateControllerHookState
+    extends HookState<WidgetStatesController, _WidgetStatesControllerHook> {
+  late final controller = WidgetStatesController(hook.values);
+
+  @override
+  WidgetStatesController build(BuildContext context) => controller;
+
+  @override
+  void dispose() => controller.dispose();
+
+  @override
+  String get debugLabel => 'useWidgetStatesController';
+}
diff --git a/packages/flutter_hooks/pubspec.yaml b/packages/flutter_hooks/pubspec.yaml
index 380ca76..f5faba9 100644
--- a/packages/flutter_hooks/pubspec.yaml
+++ b/packages/flutter_hooks/pubspec.yaml
@@ -7,7 +7,7 @@ version: 0.20.5
 
 environment:
   sdk: ">=2.17.0 <3.0.0"
-  flutter: ">=3.0.0"
+  flutter: ">=3.19.0-0.3.pre"
 
 dependencies:
   flutter:
diff --git a/packages/flutter_hooks/resources/translations/zh_cn/README.md b/packages/flutter_hooks/resources/translations/zh_cn/README.md
index b656baa..ebd3caf 100644
--- a/packages/flutter_hooks/resources/translations/zh_cn/README.md
+++ b/packages/flutter_hooks/resources/translations/zh_cn/README.md
@@ -345,7 +345,7 @@ Flutter_Hooks 已经包含一些不同类别的可复用的钩子：
 | [useIsMounted](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useIsMounted.html)                                   | 对钩子而言和 `State.mounted` 一样                  |
 | [useAutomaticKeepAlive](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useAutomaticKeepAlive.html)                 | 对钩子而言和 `AutomaticKeepAlive` 一样             |
 | [useOnPlatformBrightnessChange](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useOnPlatformBrightnessChange.html) | 监听平台 `Brightness` 并在其改变时触发回调         |
-| [useMaterialStatesController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useMaterialStatesController.html)     | 创建并自动 dispose 一个 `MaterialStatesController` |
+| [useWidgetStatesController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useWidgetStatesController.html)     | 创建并自动 dispose 一个 `WidgetStatesController` |
 | [useExpansionTileController](https://api.flutter.dev/flutter/material/ExpansionTileController-class.html)                            | 创建一个 `ExpansionTileController`                 |
 
 ## 贡献


Test patch:
diff --git a/packages/flutter_hooks/test/use_focus_node_test.dart b/packages/flutter_hooks/test/use_focus_node_test.dart
index 91642c6..055893e 100644
--- a/packages/flutter_hooks/test/use_focus_node_test.dart
+++ b/packages/flutter_hooks/test/use_focus_node_test.dart
@@ -73,16 +73,12 @@ void main() {
     );
 
     expect(focusNode.debugLabel, official.debugLabel);
-    expect(focusNode.onKey, official.onKey);
     expect(focusNode.skipTraversal, official.skipTraversal);
     expect(focusNode.canRequestFocus, official.canRequestFocus);
     expect(focusNode.descendantsAreFocusable, official.descendantsAreFocusable);
   });
 
   testWidgets('has all the FocusNode parameters', (tester) async {
-    KeyEventResult onKey(FocusNode node, RawKeyEvent event) =>
-        KeyEventResult.ignored;
-
     KeyEventResult onKeyEvent(FocusNode node, KeyEvent event) =>
         KeyEventResult.ignored;
 
@@ -91,7 +87,6 @@ void main() {
       HookBuilder(builder: (_) {
         focusNode = useFocusNode(
           debugLabel: 'Foo',
-          onKey: onKey,
           onKeyEvent: onKeyEvent,
           skipTraversal: true,
           canRequestFocus: false,
@@ -102,7 +97,6 @@ void main() {
     );
 
     expect(focusNode.debugLabel, 'Foo');
-    expect(focusNode.onKey, onKey);
     expect(focusNode.onKeyEvent, onKeyEvent);
     expect(focusNode.skipTraversal, true);
     expect(focusNode.canRequestFocus, false);
@@ -110,11 +104,6 @@ void main() {
   });
 
   testWidgets('handles parameter change', (tester) async {
-    KeyEventResult onKey(FocusNode node, RawKeyEvent event) =>
-        KeyEventResult.ignored;
-    KeyEventResult onKey2(FocusNode node, RawKeyEvent event) =>
-        KeyEventResult.ignored;
-
     KeyEventResult onKeyEvent(FocusNode node, KeyEvent event) =>
         KeyEventResult.ignored;
     KeyEventResult onKeyEvent2(FocusNode node, KeyEvent event) =>
@@ -125,7 +114,6 @@ void main() {
       HookBuilder(builder: (_) {
         focusNode = useFocusNode(
           debugLabel: 'Foo',
-          onKey: onKey,
           onKeyEvent: onKeyEvent,
           skipTraversal: true,
           canRequestFocus: false,
@@ -140,7 +128,6 @@ void main() {
       HookBuilder(builder: (_) {
         focusNode = useFocusNode(
           debugLabel: 'Bar',
-          onKey: onKey2,
           onKeyEvent: onKeyEvent2,
         );
 
@@ -148,7 +135,6 @@ void main() {
       }),
     );
 
-    expect(focusNode.onKey, onKey2);
     expect(focusNode.onKeyEvent, onKeyEvent2);
     expect(focusNode.debugLabel, 'Bar');
     expect(focusNode.skipTraversal, false);
diff --git a/packages/flutter_hooks/test/use_focus_scope_node_test.dart b/packages/flutter_hooks/test/use_focus_scope_node_test.dart
index 587b235..fcadb77 100644
--- a/packages/flutter_hooks/test/use_focus_scope_node_test.dart
+++ b/packages/flutter_hooks/test/use_focus_scope_node_test.dart
@@ -73,15 +73,11 @@ void main() {
     );
 
     expect(focusScopeNode.debugLabel, official.debugLabel);
-    expect(focusScopeNode.onKey, official.onKey);
     expect(focusScopeNode.skipTraversal, official.skipTraversal);
     expect(focusScopeNode.canRequestFocus, official.canRequestFocus);
   });
 
   testWidgets('has all the FocusScopeNode parameters', (tester) async {
-    KeyEventResult onKey(FocusNode node, RawKeyEvent event) =>
-        KeyEventResult.ignored;
-
     KeyEventResult onKeyEvent(FocusNode node, KeyEvent event) =>
         KeyEventResult.ignored;
 
@@ -90,7 +86,6 @@ void main() {
       HookBuilder(builder: (_) {
         focusScopeNode = useFocusScopeNode(
           debugLabel: 'Foo',
-          onKey: onKey,
           onKeyEvent: onKeyEvent,
           skipTraversal: true,
           canRequestFocus: false,
@@ -100,18 +95,12 @@ void main() {
     );
 
     expect(focusScopeNode.debugLabel, 'Foo');
-    expect(focusScopeNode.onKey, onKey);
     expect(focusScopeNode.onKeyEvent, onKeyEvent);
     expect(focusScopeNode.skipTraversal, true);
     expect(focusScopeNode.canRequestFocus, false);
   });
 
   testWidgets('handles parameter change', (tester) async {
-    KeyEventResult onKey(FocusNode node, RawKeyEvent event) =>
-        KeyEventResult.ignored;
-    KeyEventResult onKey2(FocusNode node, RawKeyEvent event) =>
-        KeyEventResult.ignored;
-
     KeyEventResult onKeyEvent(FocusNode node, KeyEvent event) =>
         KeyEventResult.ignored;
     KeyEventResult onKeyEvent2(FocusNode node, KeyEvent event) =>
@@ -122,7 +111,6 @@ void main() {
       HookBuilder(builder: (_) {
         focusScopeNode = useFocusScopeNode(
           debugLabel: 'Foo',
-          onKey: onKey,
           onKeyEvent: onKeyEvent,
           skipTraversal: true,
           canRequestFocus: false,
@@ -136,7 +124,6 @@ void main() {
       HookBuilder(builder: (_) {
         focusScopeNode = useFocusScopeNode(
           debugLabel: 'Bar',
-          onKey: onKey2,
           onKeyEvent: onKeyEvent2,
         );
 
@@ -144,7 +131,6 @@ void main() {
       }),
     );
 
-    expect(focusScopeNode.onKey, onKey2);
     expect(focusScopeNode.onKeyEvent, onKeyEvent2);
     expect(focusScopeNode.debugLabel, 'Bar');
     expect(focusScopeNode.skipTraversal, false);
diff --git a/packages/flutter_hooks/test/use_material_states_controller_test.dart b/packages/flutter_hooks/test/use_material_states_controller_test.dart
index d30e627..8592ef9 100644
--- a/packages/flutter_hooks/test/use_material_states_controller_test.dart
+++ b/packages/flutter_hooks/test/use_material_states_controller_test.dart
@@ -10,7 +10,7 @@ void main() {
   testWidgets('debugFillProperties', (tester) async {
     await tester.pumpWidget(
       HookBuilder(builder: (context) {
-        useMaterialStatesController();
+        useWidgetStatesController();
         return const SizedBox();
       }),
     );
@@ -23,44 +23,44 @@ void main() {
           .toStringDeep(),
       equalsIgnoringHashCodes(
         'HookBuilder\n'
-        ' │ useMaterialStatesController: MaterialStatesController#00000({})\n'
+        ' │ useWidgetStatesController: WidgetStatesController#00000({})\n'
         ' └SizedBox(renderObject: RenderConstrainedBox#00000)\n',
       ),
     );
   });
 
-  group('useMaterialStatesController', () {
+  group('useWidgetStatesController', () {
     testWidgets('initial values matches with real constructor', (tester) async {
-      late MaterialStatesController controller;
-      late MaterialStatesController controller2;
+      late WidgetStatesController controller;
+      late WidgetStatesController controller2;
 
       await tester.pumpWidget(
         HookBuilder(builder: (context) {
-          controller2 = MaterialStatesController();
-          controller = useMaterialStatesController();
+          controller2 = WidgetStatesController();
+          controller = useWidgetStatesController();
           return Container();
         }),
       );
 
       expect(controller.value, controller2.value);
     });
-    testWidgets("returns a MaterialStatesController that doesn't change",
+    testWidgets("returns a WidgetStatesController that doesn't change",
         (tester) async {
-      late MaterialStatesController controller;
-      late MaterialStatesController controller2;
+      late WidgetStatesController controller;
+      late WidgetStatesController controller2;
 
       await tester.pumpWidget(
         HookBuilder(builder: (context) {
-          controller = useMaterialStatesController();
+          controller = useWidgetStatesController();
           return Container();
         }),
       );
 
-      expect(controller, isA<MaterialStatesController>());
+      expect(controller, isA<WidgetStatesController>());
 
       await tester.pumpWidget(
         HookBuilder(builder: (context) {
-          controller2 = useMaterialStatesController();
+          controller2 = useWidgetStatesController();
           return Container();
         }),
       );
@@ -68,15 +68,15 @@ void main() {
       expect(identical(controller, controller2), isTrue);
     });
 
-    testWidgets('passes hook parameters to the MaterialStatesController',
+    testWidgets('passes hook parameters to the WidgetStatesController',
         (tester) async {
-      late MaterialStatesController controller;
+      late WidgetStatesController controller;
 
       await tester.pumpWidget(
         HookBuilder(
           builder: (context) {
-            controller = useMaterialStatesController(
-              values: {MaterialState.selected},
+            controller = useWidgetStatesController(
+              values: {WidgetState.selected},
             );
 
             return Container();
@@ -84,17 +84,17 @@ void main() {
         ),
       );
 
-      expect(controller.value, {MaterialState.selected});
+      expect(controller.value, {WidgetState.selected});
     });
 
-    testWidgets('disposes the MaterialStatesController on unmount',
+    testWidgets('disposes the WidgetStatesController on unmount',
         (tester) async {
-      late MaterialStatesController controller;
+      late WidgetStatesController controller;
 
       await tester.pumpWidget(
         HookBuilder(
           builder: (context) {
-            controller = useMaterialStatesController();
+            controller = useWidgetStatesController();
             return Container();
           },
         ),