-
-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hook): implemented hook for MaterialStateController (#383)
- Loading branch information
1 parent
6227c0f
commit 74f356a
Showing
4 changed files
with
183 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/flutter_hooks/lib/src/material_states_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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'; | ||
} |
113 changes: 113 additions & 0 deletions
113
packages/flutter_hooks/test/use_material_states_controller_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_hooks/src/framework.dart'; | ||
import 'package:flutter_hooks/src/hooks.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import 'mock.dart'; | ||
|
||
void main() { | ||
testWidgets('debugFillProperties', (tester) async { | ||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
useMaterialStatesController(); | ||
return const SizedBox(); | ||
}), | ||
); | ||
|
||
final element = tester.element(find.byType(HookBuilder)); | ||
|
||
expect( | ||
element | ||
.toDiagnosticsNode(style: DiagnosticsTreeStyle.offstage) | ||
.toStringDeep(), | ||
equalsIgnoringHashCodes( | ||
'HookBuilder\n' | ||
' │ useMaterialStatesController: MaterialStatesController#00000({})\n' | ||
' └SizedBox(renderObject: RenderConstrainedBox#00000)\n', | ||
), | ||
); | ||
}); | ||
|
||
group('useMaterialStatesController', () { | ||
testWidgets('initial values matches with real constructor', (tester) async { | ||
late MaterialStatesController controller; | ||
late MaterialStatesController controller2; | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
controller2 = MaterialStatesController(); | ||
controller = useMaterialStatesController(); | ||
return Container(); | ||
}), | ||
); | ||
|
||
expect(controller.value, controller2.value); | ||
}); | ||
testWidgets("returns a MaterialStatesController that doesn't change", | ||
(tester) async { | ||
late MaterialStatesController controller; | ||
late MaterialStatesController controller2; | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
controller = useMaterialStatesController(); | ||
return Container(); | ||
}), | ||
); | ||
|
||
expect(controller, isA<MaterialStatesController>()); | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
controller2 = useMaterialStatesController(); | ||
return Container(); | ||
}), | ||
); | ||
|
||
expect(identical(controller, controller2), isTrue); | ||
}); | ||
|
||
testWidgets('passes hook parameters to the MaterialStatesController', | ||
(tester) async { | ||
late MaterialStatesController controller; | ||
|
||
await tester.pumpWidget( | ||
HookBuilder( | ||
builder: (context) { | ||
controller = useMaterialStatesController( | ||
values: {MaterialState.selected}, | ||
); | ||
|
||
return Container(); | ||
}, | ||
), | ||
); | ||
|
||
expect(controller.value, {MaterialState.selected}); | ||
}); | ||
|
||
testWidgets('disposes the MaterialStatesController on unmount', | ||
(tester) async { | ||
late MaterialStatesController controller; | ||
|
||
await tester.pumpWidget( | ||
HookBuilder( | ||
builder: (context) { | ||
controller = useMaterialStatesController(); | ||
return Container(); | ||
}, | ||
), | ||
); | ||
|
||
// pump another widget so that the old one gets disposed | ||
await tester.pumpWidget(Container()); | ||
|
||
expect( | ||
() => controller.addListener(() {}), | ||
throwsA(isFlutterError.having( | ||
(e) => e.message, 'message', contains('disposed'))), | ||
); | ||
}); | ||
}); | ||
} |