diff --git a/README.md b/README.md index bf62334f..8d7255e1 100644 --- a/README.md +++ b/README.md @@ -355,10 +355,10 @@ A series of hooks with no particular theme. | [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`. | | [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`. | +| [useExpansibleController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useExpansibleController.html) | Creates a `ExpansibleController`. | | [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 | | [useDraggableScrollableController](https://api.flutter.dev/flutter/widgets/DraggableScrollableController-class.html) | Creates a `DraggableScrollableController`. | -| [useCarouselController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useCarouselController.html) | Creates and disposes a **`CarouselController`**. | +| [useCarouselController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useCarouselController.html) | Creates and disposes a **`CarouselController`**. | | [useTreeSliverController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTreeSliverController.html) | Creates a `TreeSliverController`. | | [useOverlayPortalController](https://api.flutter.dev/flutter/widgets/OverlayPortalController-class.html) | Creates and manages an `OverlayPortalController` for controlling the visibility of overlay content. The controller will be automatically disposed when no longer needed. | diff --git a/packages/flutter_hooks/CHANGELOG.md b/packages/flutter_hooks/CHANGELOG.md index 7748f78c..ed4a35d1 100644 --- a/packages/flutter_hooks/CHANGELOG.md +++ b/packages/flutter_hooks/CHANGELOG.md @@ -1,3 +1,7 @@ +## Unreleased patch + +Deprecated `useExpansionTileController` in favor of `useExpansibleController`. + ## 0.21.2 - 2025-02-23 - Add `useCarouselController` (thanks to @riscait) diff --git a/packages/flutter_hooks/lib/src/expansion_tile_controller.dart b/packages/flutter_hooks/lib/src/expansion_tile_controller.dart index f698f54b..665aef49 100644 --- a/packages/flutter_hooks/lib/src/expansion_tile_controller.dart +++ b/packages/flutter_hooks/lib/src/expansion_tile_controller.dart @@ -1,28 +1,37 @@ part of 'hooks.dart'; +/// Creates a [ExpansibleController] that will be disposed automatically. +/// +/// See also: +/// - [ExpansibleController] +ExpansibleController useExpansibleController({List? keys}) { + return use(_ExpansibleControllerHook(keys: keys)); +} + /// Creates a [ExpansionTileController] that will be disposed automatically. /// /// See also: /// - [ExpansionTileController] +@Deprecated('Use `useExpansibleController` instead.') ExpansionTileController useExpansionTileController({List? keys}) { - return use(_ExpansionTileControllerHook(keys: keys)); + return use(_ExpansibleControllerHook(keys: keys)); } -class _ExpansionTileControllerHook extends Hook { - const _ExpansionTileControllerHook({List? keys}) : super(keys: keys); +class _ExpansibleControllerHook extends Hook { + const _ExpansibleControllerHook({List? keys}) : super(keys: keys); @override - HookState> - createState() => _ExpansionTileControllerHookState(); + HookState> createState() => + _ExpansibleControllerHookState(); } -class _ExpansionTileControllerHookState - extends HookState { - final controller = ExpansionTileController(); +class _ExpansibleControllerHookState + extends HookState { + final controller = ExpansibleController(); @override - String get debugLabel => 'useExpansionTileController'; + String get debugLabel => 'useExpansibleController'; @override - ExpansionTileController build(BuildContext context) => controller; + ExpansibleController build(BuildContext context) => controller; } diff --git a/packages/flutter_hooks/lib/src/hooks.dart b/packages/flutter_hooks/lib/src/hooks.dart index d1e4664a..7fb94cd6 100644 --- a/packages/flutter_hooks/lib/src/hooks.dart +++ b/packages/flutter_hooks/lib/src/hooks.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart' Brightness, CarouselController, DraggableScrollableController, + // ignore: deprecated_member_use ExpansionTileController, SearchController, TabController, diff --git a/packages/flutter_hooks/lib/src/misc.dart b/packages/flutter_hooks/lib/src/misc.dart index 57510ad3..b461ce3d 100644 --- a/packages/flutter_hooks/lib/src/misc.dart +++ b/packages/flutter_hooks/lib/src/misc.dart @@ -1,17 +1,17 @@ part of 'hooks.dart'; /// A store of mutable state that allows mutations by dispatching actions. -abstract class Store { +abstract class Store { /// The current state. /// /// This value may change after a call to [dispatch]. - State get state; + StateT get state; /// Dispatches an action. /// /// Actions are dispatched synchronously. /// It is impossible to try to dispatch actions during `build`. - void dispatch(Action action); + void dispatch(ActionT action); } /// Composes an [Action] and a [State] to create a new [State]. @@ -33,10 +33,10 @@ typedef Reducer = State Function(State state, Action action); /// See also: /// * [Reducer] /// * [Store] -Store useReducer( - Reducer reducer, { - required State initialState, - required Action initialAction, +Store useReducer( + Reducer reducer, { + required StateT initialState, + required ActionT initialAction, }) { return use( _ReducerHook( @@ -47,27 +47,27 @@ Store useReducer( ); } -class _ReducerHook extends Hook> { +class _ReducerHook extends Hook> { const _ReducerHook( this.reducer, { required this.initialState, required this.initialAction, }); - final Reducer reducer; - final State initialState; - final Action initialAction; + final Reducer reducer; + final StateT initialState; + final ActionT initialAction; @override - _ReducerHookState createState() => - _ReducerHookState(); + _ReducerHookState createState() => + _ReducerHookState(); } -class _ReducerHookState - extends HookState, _ReducerHook> - implements Store { +class _ReducerHookState + extends HookState, _ReducerHook> + implements Store { @override - late State state = hook.reducer(hook.initialState, hook.initialAction); + late StateT state = hook.reducer(hook.initialState, hook.initialAction); @override void initHook() { @@ -77,7 +77,7 @@ class _ReducerHookState } @override - void dispatch(Action action) { + void dispatch(ActionT action) { final newState = hook.reducer(state, action); if (state != newState) { @@ -86,7 +86,7 @@ class _ReducerHookState } @override - Store build(BuildContext context) { + Store build(BuildContext context) { return this; } diff --git a/packages/flutter_hooks/pubspec.yaml b/packages/flutter_hooks/pubspec.yaml index a1ab28c2..ae237b0d 100644 --- a/packages/flutter_hooks/pubspec.yaml +++ b/packages/flutter_hooks/pubspec.yaml @@ -7,7 +7,7 @@ version: 0.21.2 environment: sdk: ">=2.17.0 <3.0.0" - flutter: ">=3.21.0-13.0.pre.4" + flutter: ">=3.32.0" dependencies: flutter: diff --git a/packages/flutter_hooks/test/use_animation_controller_test.dart b/packages/flutter_hooks/test/use_animation_controller_test.dart index bf766aae..2c71bf32 100644 --- a/packages/flutter_hooks/test/use_animation_controller_test.dart +++ b/packages/flutter_hooks/test/use_animation_controller_test.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/foundation.dart'; import 'package:flutter/scheduler.dart'; import 'package:flutter/widgets.dart'; @@ -26,9 +28,10 @@ void main() { controller ..duration = const Duration(seconds: 1) - ..reverseDuration = const Duration(seconds: 1) - // check has a ticker - ..forward(); + ..reverseDuration = const Duration(seconds: 1); + + // check has a ticker + unawaited(controller.forward()); // dispose await tester.pumpWidget(const SizedBox()); diff --git a/packages/flutter_hooks/test/use_expansion_tile_controller_test.dart b/packages/flutter_hooks/test/use_expansible_controller_test.dart similarity index 80% rename from packages/flutter_hooks/test/use_expansion_tile_controller_test.dart rename to packages/flutter_hooks/test/use_expansible_controller_test.dart index 84dd57d1..76d2207d 100644 --- a/packages/flutter_hooks/test/use_expansion_tile_controller_test.dart +++ b/packages/flutter_hooks/test/use_expansible_controller_test.dart @@ -9,7 +9,7 @@ void main() { testWidgets('debugFillProperties', (tester) async { await tester.pumpWidget( HookBuilder(builder: (context) { - useExpansionTileController(); + useExpansibleController(); return const SizedBox(); }), ); @@ -24,21 +24,21 @@ void main() { .toStringDeep(), equalsIgnoringHashCodes( 'HookBuilder\n' - " │ useExpansionTileController: Instance of 'ExpansionTileController'\n" + " │ useExpansibleController: Instance of 'ExpansibleController'\n" ' └SizedBox(renderObject: RenderConstrainedBox#00000)\n', ), ); }); - group('useExpansionTileController', () { + group('useExpansibleController', () { testWidgets('initial values matches with real constructor', (tester) async { - late ExpansionTileController controller; - final controller2 = ExpansionTileController(); + late ExpansibleController controller; + final controller2 = ExpansibleController(); await tester.pumpWidget(MaterialApp( home: Scaffold( body: HookBuilder(builder: (context) { - controller = useExpansionTileController(); + controller = useExpansibleController(); return Column( children: [ ExpansionTile( @@ -54,16 +54,16 @@ void main() { }), ), )); - expect(controller, isA()); + expect(controller, isA()); expect(controller.isExpanded, controller2.isExpanded); }); testWidgets('check expansion/collapse of tile', (tester) async { - late ExpansionTileController controller; + late ExpansibleController controller; await tester.pumpWidget(MaterialApp( home: Scaffold( body: HookBuilder(builder: (context) { - controller = useExpansionTileController(); + controller = useExpansibleController(); return ExpansionTile( controller: controller, title: const Text('Expansion Tile'), diff --git a/packages/flutter_hooks/test/use_ticker_provider_test.dart b/packages/flutter_hooks/test/use_ticker_provider_test.dart index 329fea41..801a8b7b 100644 --- a/packages/flutter_hooks/test/use_ticker_provider_test.dart +++ b/packages/flutter_hooks/test/use_ticker_provider_test.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_hooks/flutter_hooks.dart'; @@ -41,8 +43,10 @@ void main() { )); final animationController = AnimationController( - vsync: provider, duration: const Duration(seconds: 1)) - ..forward(); + vsync: provider, + duration: const Duration(seconds: 1), + ); + unawaited(animationController.forward()); expect(() => AnimationController(vsync: provider), throwsFlutterError); diff --git a/packages/flutter_hooks/test/use_transformation_controller_test.dart b/packages/flutter_hooks/test/use_transformation_controller_test.dart index 4df11c08..c67a0368 100644 --- a/packages/flutter_hooks/test/use_transformation_controller_test.dart +++ b/packages/flutter_hooks/test/use_transformation_controller_test.dart @@ -20,15 +20,27 @@ void main() { element .toDiagnosticsNode(style: DiagnosticsTreeStyle.offstage) .toStringDeep(), - equalsIgnoringHashCodes( - 'HookBuilder\n' - ' │ useTransformationController:\n' - ' │ TransformationController#00000([0] 1.0,0.0,0.0,0.0\n' - ' │ [1] 0.0,1.0,0.0,0.0\n' - ' │ [2] 0.0,0.0,1.0,0.0\n' - ' │ [3] 0.0,0.0,0.0,1.0\n' - ' │ )\n' - ' └SizedBox(renderObject: RenderConstrainedBox#00000)\n', + anyOf( + equalsIgnoringHashCodes( + 'HookBuilder\n' + ' │ useTransformationController:\n' + ' │ TransformationController#00000([0] 1.0,0.0,0.0,0.0\n' + ' │ [1] 0.0,1.0,0.0,0.0\n' + ' │ [2] 0.0,0.0,1.0,0.0\n' + ' │ [3] 0.0,0.0,0.0,1.0\n' + ' │ )\n' + ' └SizedBox(renderObject: RenderConstrainedBox#00000)\n', + ), + equalsIgnoringHashCodes( + 'HookBuilder\n' + ' │ useTransformationController:\n' + ' │ TransformationController#00000([0] [1.0,0.0,0.0,0.0]\n' + ' │ [1] [0.0,1.0,0.0,0.0]\n' + ' │ [2] [0.0,0.0,1.0,0.0]\n' + ' │ [3] [0.0,0.0,0.0,1.0]\n' + ' │ )\n' + ' └SizedBox(renderObject: RenderConstrainedBox#00000)\n', + ), ), ); });