Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5558b3b

Browse filesBrowse files
committed
2 parents 9982bd3 + 74f356a commit 5558b3b
Copy full SHA for 5558b3b

File tree

Expand file treeCollapse file tree

4 files changed

+183
-18
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+183
-18
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+18-17Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -338,23 +338,24 @@ They will take care of creating/updating/disposing an object.
338338

339339
A series of hooks with no particular theme.
340340

341-
| Name | Description |
342-
|--------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|
343-
| [useReducer](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useReducer.html) | An alternative to `useState` for more complex states. |
344-
| [usePrevious](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/usePrevious.html) | Returns the previous argument called to [usePrevious]. |
345-
| [useTextEditingController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTextEditingController-constant.html) | Creates a `TextEditingController`. |
346-
| [useFocusNode](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useFocusNode.html) | Creates a `FocusNode`. |
347-
| [useTabController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTabController.html) | Creates and disposes a `TabController`. |
348-
| [useScrollController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useScrollController.html) | Creates and disposes a `ScrollController`. |
349-
| [usePageController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/usePageController.html) | Creates and disposes a `PageController`. |
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-
| [useExpansionTileController](https://api.flutter.dev/flutter/material/ExpansionTileController-class.html) | Creates a `ExpansionTileController`. |
341+
| Name | Description |
342+
| ------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------- |
343+
| [useReducer](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useReducer.html) | An alternative to `useState` for more complex states. |
344+
| [usePrevious](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/usePrevious.html) | Returns the previous argument called to [usePrevious]. |
345+
| [useTextEditingController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTextEditingController-constant.html) | Creates a `TextEditingController`. |
346+
| [useFocusNode](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useFocusNode.html) | Creates a `FocusNode`. |
347+
| [useTabController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useTabController.html) | Creates and disposes a `TabController`. |
348+
| [useScrollController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useScrollController.html) | Creates and disposes a `ScrollController`. |
349+
| [usePageController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/usePageController.html) | Creates and disposes a `PageController`. |
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+
| [useMaterialStatesController](https://pub.dev/documentation/flutter_hooks/latest/flutter_hooks/useMaterialStatesController.html) | Creates and disposes a `MaterialStatesController`. |
358+
| [useExpansionTileController](https://api.flutter.dev/flutter/material/ExpansionTileController-class.html) | Creates a `ExpansionTileController`. |
358359

359360
## Contributions
360361

‎packages/flutter_hooks/lib/src/hooks.dart

Copy file name to clipboardExpand all lines: packages/flutter_hooks/lib/src/hooks.dart
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import 'dart:async';
22

33
import 'package:flutter/foundation.dart';
44
import 'package:flutter/material.dart'
5-
show Brightness, ExpansionTileController, SearchController, TabController;
5+
show
6+
Brightness,
7+
ExpansionTileController,
8+
MaterialState,
9+
MaterialStatesController,
10+
SearchController,
11+
TabController;
612
import 'package:flutter/scheduler.dart';
713
import 'package:flutter/widgets.dart';
814

@@ -26,3 +32,4 @@ part 'tab_controller.dart';
2632
part 'text_controller.dart';
2733
part 'transformation_controller.dart';
2834
part 'widgets_binding_observer.dart';
35+
part 'material_states_controller.dart';
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
part of 'hooks.dart';
2+
3+
/// Creates a [MaterialStatesController] that will be disposed automatically.
4+
///
5+
/// See also:
6+
/// - [MaterialStatesController]
7+
MaterialStatesController useMaterialStatesController({
8+
Set<MaterialState>? values,
9+
List<Object?>? keys,
10+
}) {
11+
return use(
12+
_MaterialStatesControllerHook(
13+
values: values,
14+
keys: keys,
15+
),
16+
);
17+
}
18+
19+
class _MaterialStatesControllerHook extends Hook<MaterialStatesController> {
20+
const _MaterialStatesControllerHook({
21+
required this.values,
22+
super.keys,
23+
});
24+
25+
final Set<MaterialState>? values;
26+
27+
@override
28+
HookState<MaterialStatesController, Hook<MaterialStatesController>>
29+
createState() => _MaterialStateControllerHookState();
30+
}
31+
32+
class _MaterialStateControllerHookState
33+
extends HookState<MaterialStatesController, _MaterialStatesControllerHook> {
34+
late final controller = MaterialStatesController(hook.values);
35+
36+
@override
37+
MaterialStatesController build(BuildContext context) => controller;
38+
39+
@override
40+
void dispose() => controller.dispose();
41+
42+
@override
43+
String get debugLabel => 'useMaterialStatesController';
44+
}
+113Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter_hooks/src/framework.dart';
4+
import 'package:flutter_hooks/src/hooks.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
7+
import 'mock.dart';
8+
9+
void main() {
10+
testWidgets('debugFillProperties', (tester) async {
11+
await tester.pumpWidget(
12+
HookBuilder(builder: (context) {
13+
useMaterialStatesController();
14+
return const SizedBox();
15+
}),
16+
);
17+
18+
final element = tester.element(find.byType(HookBuilder));
19+
20+
expect(
21+
element
22+
.toDiagnosticsNode(style: DiagnosticsTreeStyle.offstage)
23+
.toStringDeep(),
24+
equalsIgnoringHashCodes(
25+
'HookBuilder\n'
26+
' │ useMaterialStatesController: MaterialStatesController#00000({})\n'
27+
' └SizedBox(renderObject: RenderConstrainedBox#00000)\n',
28+
),
29+
);
30+
});
31+
32+
group('useMaterialStatesController', () {
33+
testWidgets('initial values matches with real constructor', (tester) async {
34+
late MaterialStatesController controller;
35+
late MaterialStatesController controller2;
36+
37+
await tester.pumpWidget(
38+
HookBuilder(builder: (context) {
39+
controller2 = MaterialStatesController();
40+
controller = useMaterialStatesController();
41+
return Container();
42+
}),
43+
);
44+
45+
expect(controller.value, controller2.value);
46+
});
47+
testWidgets("returns a MaterialStatesController that doesn't change",
48+
(tester) async {
49+
late MaterialStatesController controller;
50+
late MaterialStatesController controller2;
51+
52+
await tester.pumpWidget(
53+
HookBuilder(builder: (context) {
54+
controller = useMaterialStatesController();
55+
return Container();
56+
}),
57+
);
58+
59+
expect(controller, isA<MaterialStatesController>());
60+
61+
await tester.pumpWidget(
62+
HookBuilder(builder: (context) {
63+
controller2 = useMaterialStatesController();
64+
return Container();
65+
}),
66+
);
67+
68+
expect(identical(controller, controller2), isTrue);
69+
});
70+
71+
testWidgets('passes hook parameters to the MaterialStatesController',
72+
(tester) async {
73+
late MaterialStatesController controller;
74+
75+
await tester.pumpWidget(
76+
HookBuilder(
77+
builder: (context) {
78+
controller = useMaterialStatesController(
79+
values: {MaterialState.selected},
80+
);
81+
82+
return Container();
83+
},
84+
),
85+
);
86+
87+
expect(controller.value, {MaterialState.selected});
88+
});
89+
90+
testWidgets('disposes the MaterialStatesController on unmount',
91+
(tester) async {
92+
late MaterialStatesController controller;
93+
94+
await tester.pumpWidget(
95+
HookBuilder(
96+
builder: (context) {
97+
controller = useMaterialStatesController();
98+
return Container();
99+
},
100+
),
101+
);
102+
103+
// pump another widget so that the old one gets disposed
104+
await tester.pumpWidget(Container());
105+
106+
expect(
107+
() => controller.addListener(() {}),
108+
throwsA(isFlutterError.having(
109+
(e) => e.message, 'message', contains('disposed'))),
110+
);
111+
});
112+
});
113+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.