Skip to content

Commit b283a05

Browse files
committed
Merge branch 'master' of https://github.com/rrousselGit/flutter_hooks into pr/d-polikhranidi/446
2 parents a92e2fe + a203adf commit b283a05

File tree

12 files changed

+426
-28
lines changed

12 files changed

+426
-28
lines changed

README.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[English](https://github.com/rrousselGit/flutter_hooks/blob/master/README.md) | [Português](https://github.com/rrousselGit/flutter_hooks/blob/master/packages/flutter_hooks/resources/translations/pt_br/README.md) | [한국어](https://github.com/rrousselGit/flutter_hooks/blob/master/packages/flutter_hooks/resources/translations/ko_kr/README.md) | [简体中文](https://github.com/rrousselGit/flutter_hooks/blob/master/packages/flutter_hooks/resources/translations/zh_cn/README.md)
22

33
[![Build](https://github.com/rrousselGit/flutter_hooks/workflows/Build/badge.svg)](https://github.com/rrousselGit/flutter_hooks/actions?query=workflow%3ABuild) [![codecov](https://codecov.io/gh/rrousselGit/flutter_hooks/branch/master/graph/badge.svg)](https://codecov.io/gh/rrousselGit/flutter_hooks) [![pub package](https://img.shields.io/pub/v/flutter_hooks.svg)](https://pub.dev/packages/flutter_hooks) [![pub package](https://img.shields.io/badge/Awesome-Flutter-blue.svg?longCache=true&style=flat-square)](https://github.com/Solido/awesome-flutter)
4-
<a href="https://discord.gg/Bbumvej"><img src="https://img.shields.io/discord/765557403865186374.svg?logo=discord&color=blue" alt="Discord"></a>
4+
<a href="https://discord.gg/6G6ZWkk3fQ"><img src="https://img.shields.io/discord/765557403865186374.svg?logo=discord&color=blue" alt="Discord"></a>
55

66
<img src="https://raw.githubusercontent.com/rrousselGit/flutter_hooks/master/packages/flutter_hooks/flutter-hook.svg?sanitize=true" width="200">
77

@@ -337,27 +337,30 @@ They will take care of creating/updating/disposing an object.
337337

338338
A series of hooks with no particular theme.
339339

340-
| Name | Description |
341-
| ------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------- |
342-
| [useReducer](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useReducer.html) | An alternative to `useState` for more complex states. |
343-
| [usePrevious](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/usePrevious.html) | Returns the previous argument called to [usePrevious]. |
344-
| [useTextEditingController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTextEditingController-constant.html) | Creates a `TextEditingController`. |
345-
| [useFocusNode](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useFocusNode.html) | Creates a `FocusNode`. |
346-
| [useTabController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTabController.html) | Creates and disposes a `TabController`. |
347-
| [useScrollController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useScrollController.html) | Creates and disposes a `ScrollController`. |
348-
| [usePageController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/usePageController.html) | Creates and disposes a `PageController`. |
349-
| [useFixedExtentScrollController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useFixedExtentScrollController.html) | Creates and disposes a `FixedExtentScrollController`. |
350-
| [useAppLifecycleState](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useAppLifecycleState.html) | Returns the current `AppLifecycleState` and rebuilds the widget on change. |
351-
| [useOnAppLifecycleStateChange](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useOnAppLifecycleStateChange.html) | Listens to `AppLifecycleState` changes and triggers a callback on change. |
352-
| [useTransformationController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTransformationController.html) | Creates and disposes a `TransformationController`. |
353-
| [useIsMounted](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useIsMounted.html) | An equivalent to `State.mounted` for hooks. |
354-
| [useAutomaticKeepAlive](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useAutomaticKeepAlive.html) | An equivalent to the `AutomaticKeepAlive` widget for hooks. |
355-
| [useOnPlatformBrightnessChange](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useOnPlatformBrightnessChange.html) | Listens to platform `Brightness` changes and triggers a callback on change. |
356-
| [useSearchController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useSearchController.html) | Creates and disposes a `SearchController`. |
357-
| [useWidgetStatesController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useWidgetStatesController.html) | Creates and disposes a `WidgetStatesController`. |
358-
| [useExpansionTileController](https://api.flutter.dev/flutter/material/ExpansionTileController-class.html) | Creates a `ExpansionTileController`. |
359-
| [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 |
360-
| [useDraggableScrollableController](https://api.flutter.dev/flutter/widgets/DraggableScrollableController-class.html) | Creates a `DraggableScrollableController`. |
340+
| Name | Description |
341+
| -------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
342+
| [useReducer](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useReducer.html) | An alternative to `useState` for more complex states. |
343+
| [usePrevious](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/usePrevious.html) | Returns the previous argument called to [usePrevious]. |
344+
| [useTextEditingController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTextEditingController-constant.html) | Creates a `TextEditingController`. |
345+
| [useFocusNode](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useFocusNode.html) | Creates a `FocusNode`. |
346+
| [useTabController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTabController.html) | Creates and disposes a `TabController`. |
347+
| [useScrollController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useScrollController.html) | Creates and disposes a `ScrollController`. |
348+
| [usePageController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/usePageController.html) | Creates and disposes a `PageController`. |
349+
| [useFixedExtentScrollController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useFixedExtentScrollController.html) | Creates and disposes a `FixedExtentScrollController`. |
350+
| [useAppLifecycleState](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useAppLifecycleState.html) | Returns the current `AppLifecycleState` and rebuilds the widget on change. |
351+
| [useOnAppLifecycleStateChange](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useOnAppLifecycleStateChange.html) | Listens to `AppLifecycleState` changes and triggers a callback on change. |
352+
| [useTransformationController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTransformationController.html) | Creates and disposes a `TransformationController`. |
353+
| [useIsMounted](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useIsMounted.html) | An equivalent to `State.mounted` for hooks. |
354+
| [useAutomaticKeepAlive](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useAutomaticKeepAlive.html) | An equivalent to the `AutomaticKeepAlive` widget for hooks. |
355+
| [useOnPlatformBrightnessChange](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useOnPlatformBrightnessChange.html) | Listens to platform `Brightness` changes and triggers a callback on change. |
356+
| [useSearchController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useSearchController.html) | Creates and disposes a `SearchController`. |
357+
| [useWidgetStatesController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useWidgetStatesController.html) | Creates and disposes a `WidgetStatesController`. |
358+
| [useExpansionTileController](https://api.flutter.dev/flutter/material/ExpansionTileController-class.html) | Creates a `ExpansionTileController`. |
359+
| [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 |
360+
| [useDraggableScrollableController](https://api.flutter.dev/flutter/widgets/DraggableScrollableController-class.html) | Creates a `DraggableScrollableController`. |
361+
| [useCarouselController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useCarouselController.html) | Creates and disposes a **`CarouselController`**. |
362+
| [useTreeSliverController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTreeSliverController.html) | Creates a `TreeSliverController`. |
363+
| [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. |
361364

362365
## Contributions
363366

packages/flutter_hooks/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.21.2 - 2025-02-23
2+
3+
- Add `useCarouselController` (thanks to @riscait)
4+
- Add `useTreeSliverController` (thanks to @benthillerkus)
5+
- Added `useOverlayPortalController` (thanks to @offich)
6+
17
## 0.21.1-pre.4 - 2024-08-01
28

39
- Added `useFixedExtentScrollController` (thanks to @whynotmake-it)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
part of 'hooks.dart';
2+
3+
/// Creates a [CarouselController] that will be disposed automatically.
4+
///
5+
/// See also:
6+
/// - [CarouselController]
7+
CarouselController useCarouselController({
8+
int initialItem = 0,
9+
List<Object?>? keys,
10+
}) {
11+
return use(
12+
_CarouselControllerHook(
13+
initialItem: initialItem,
14+
keys: keys,
15+
),
16+
);
17+
}
18+
19+
class _CarouselControllerHook extends Hook<CarouselController> {
20+
const _CarouselControllerHook({
21+
required this.initialItem,
22+
super.keys,
23+
});
24+
25+
final int initialItem;
26+
27+
@override
28+
HookState<CarouselController, Hook<CarouselController>> createState() =>
29+
_CarouselControllerHookState();
30+
}
31+
32+
class _CarouselControllerHookState
33+
extends HookState<CarouselController, _CarouselControllerHook> {
34+
late final controller = CarouselController(
35+
initialItem: hook.initialItem,
36+
);
37+
38+
@override
39+
CarouselController build(BuildContext context) => controller;
40+
41+
@override
42+
void dispose() => controller.dispose();
43+
44+
@override
45+
String get debugLabel => 'useCarouselController';
46+
}

packages/flutter_hooks/lib/src/hooks.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,24 @@ import 'dart:async';
22

33
import 'package:flutter/foundation.dart';
44
import 'package:flutter/material.dart'
5-
show Brightness, DraggableScrollableController, ExpansionTileController, SearchController, TabController, WidgetState, WidgetStatesController, kTabScrollDuration;
5+
show
6+
Brightness,
7+
DraggableScrollableController,
8+
ExpansionTileController,
9+
SearchController,
10+
TabController,
11+
WidgetState,
12+
WidgetStatesController,
13+
kTabScrollDuration;
614
import 'package:flutter/scheduler.dart';
715
import 'package:flutter/widgets.dart';
816

917
import 'framework.dart';
1018

1119
part 'animation.dart';
1220
part 'async.dart';
21+
part 'carousel_controller.dart';
22+
part 'debounced.dart';
1323
part 'draggable_scrollable_controller.dart';
1424
part 'expansion_tile_controller.dart';
1525
part 'fixed_extent_scroll_controller.dart';
@@ -27,6 +37,6 @@ part 'search_controller.dart';
2737
part 'tab_controller.dart';
2838
part 'text_controller.dart';
2939
part 'transformation_controller.dart';
30-
part 'widgets_binding_observer.dart';
40+
part 'tree_sliver_controller.dart';
3141
part 'widget_states_controller.dart';
32-
part 'debounced.dart';
42+
part 'widgets_binding_observer.dart';

packages/flutter_hooks/lib/src/misc.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,32 @@ class _IsMountedHookState extends HookState<IsMounted, _IsMountedHook> {
229229
"Use BuildContext.mounted instead if you're on Flutter 3.7.0 or greater",
230230
)
231231
typedef IsMounted = bool Function();
232+
233+
/// Creates a [OverlayPortalController] that will be disposed automatically.
234+
///
235+
/// See also:
236+
/// - [OverlayPortalController]
237+
OverlayPortalController useOverlayPortalController({
238+
List<Object?>? keys,
239+
}) {
240+
return use(_OverlayPortalControllerHook(keys: keys));
241+
}
242+
243+
class _OverlayPortalControllerHook extends Hook<OverlayPortalController> {
244+
const _OverlayPortalControllerHook({List<Object?>? keys}) : super(keys: keys);
245+
246+
@override
247+
HookState<OverlayPortalController, Hook<OverlayPortalController>>
248+
createState() => _OverlayPortalControllerHookState();
249+
}
250+
251+
class _OverlayPortalControllerHookState
252+
extends HookState<OverlayPortalController, _OverlayPortalControllerHook> {
253+
final controller = OverlayPortalController();
254+
255+
@override
256+
OverlayPortalController build(BuildContext context) => controller;
257+
258+
@override
259+
String get debugLabel => 'useOverlayPortalController';
260+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
part of 'hooks.dart';
2+
3+
/// Creates a [TreeSliverController] that will be disposed automatically.
4+
///
5+
/// See also:
6+
/// - [TreeSliverController]
7+
TreeSliverController useTreeSliverController() {
8+
return use(const _TreeSliverControllerHook());
9+
}
10+
11+
class _TreeSliverControllerHook extends Hook<TreeSliverController> {
12+
const _TreeSliverControllerHook();
13+
14+
@override
15+
HookState<TreeSliverController, Hook<TreeSliverController>> createState() =>
16+
_TreeSliverControllerHookState();
17+
}
18+
19+
class _TreeSliverControllerHookState
20+
extends HookState<TreeSliverController, _TreeSliverControllerHook> {
21+
final controller = TreeSliverController();
22+
23+
@override
24+
String get debugLabel => 'useTreeSliverController';
25+
26+
@override
27+
TreeSliverController build(BuildContext context) => controller;
28+
}

packages/flutter_hooks/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: A flutter implementation of React hooks. It adds a new kind of widg
33
homepage: https://github.com/rrousselGit/flutter_hooks
44
repository: https://github.com/rrousselGit/flutter_hooks/tree/master/packages/flutter_hooks
55
issue_tracker: https://github.com/rrousselGit/flutter_hooks/issues
6-
version: 0.21.1-pre.4
6+
version: 0.21.2
77

88
environment:
99
sdk: ">=2.17.0 <3.0.0"

0 commit comments

Comments
 (0)