Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a useTransformationController hook #254

Merged
merged 3 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ A series of hooks with no particular theme.
| [useFocusNode](https://pub.dartlang.org/documentation/flutter_hooks/latest/flutter_hooks/useFocusNode.html) | Create a `FocusNode` |
| [useTabController](https://pub.dartlang.org/documentation/flutter_hooks/latest/flutter_hooks/useTabController.html) | Creates and disposes a `TabController`. |
| [useScrollController](https://pub.dartlang.org/documentation/flutter_hooks/latest/flutter_hooks/useScrollController.html) | Creates and disposes a `ScrollController`. |
| [useTransformationController](https://pub.dartlang.org/documentation/flutter_hooks/latest/flutter_hooks/useTransformationController.html) | Creates and disposes a `TransformationController`. |
| [usePageController](https://pub.dartlang.org/documentation/flutter_hooks/latest/flutter_hooks/usePageController.html) | Creates and disposes a `PageController`. |
| [useIsMounted](https://pub.dartlang.org/documentation/flutter_hooks/latest/flutter_hooks/useIsMounted.html) | An equivalent to `State.mounted` for hooks |

Expand Down
1 change: 1 addition & 0 deletions packages/flutter_hooks/lib/src/hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ part 'text_controller.dart';
part 'focus.dart';
part 'scroll_controller.dart';
part 'page_controller.dart';
part 'transformation_controller.dart';
44 changes: 44 additions & 0 deletions packages/flutter_hooks/lib/src/transformation_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
part of 'hooks.dart';

/// Creates and disposes a [TransformationController].
///
/// See also:
/// - [TransformationController]
TransformationController useTransformationController({
Matrix4? initialValue,
List<Object?>? keys,
}) {
return use(
_TransformationControllerHook(
initialValue: initialValue,
keys: keys,
),
);
}

class _TransformationControllerHook extends Hook<TransformationController> {
const _TransformationControllerHook({
required this.initialValue,
List<Object?>? keys,
}) : super(keys: keys);

final Matrix4? initialValue;

@override
HookState<TransformationController, Hook<TransformationController>> createState() =>
_TransformationControllerHookState();
}

class _TransformationControllerHookState
extends HookState<TransformationController, _TransformationControllerHook> {
late final controller = TransformationController(hook.initialValue);

@override
TransformationController build(BuildContext context) => controller;

@override
void dispose() => controller.dispose();

@override
String get debugLabel => 'useTransformationController';
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ São vários hooks sem um tema particular.
| [useFocusNode](https://pub.dartlang.org/documentation/flutter_hooks/latest/flutter_hooks/useFocusNode.html) | Cria um `FocusNode` |
| [useTabController](https://pub.dartlang.org/documentation/flutter_hooks/latest/flutter_hooks/useTabController.html) | Cria e descarta um `TabController`. |
| [useScrollController](https://pub.dartlang.org/documentation/flutter_hooks/latest/flutter_hooks/useScrollController.html) | Cria e descarta um `ScrollController`. |
| [useTransformationController](https://pub.dartlang.org/documentation/flutter_hooks/latest/flutter_hooks/useTransformationController.html) | Cria e descarta um `TransformationController`. |

## Contribuições

Expand Down
93 changes: 93 additions & 0 deletions packages/flutter_hooks/test/use_transformation_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.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) {
useTransformationController();
return const SizedBox();
}),
);

final element = tester.element(find.byType(HookBuilder));

expect(
element
.toDiagnosticsNode(style: DiagnosticsTreeStyle.offstage)
.toStringDeep(),
equalsIgnoringHashCodes(
'HookBuilder\n'
' │ useTransformationController: TransformationController#00000(no clients)\n'
' └SizedBox(renderObject: RenderConstrainedBox#00000)\n',
),
);
});

group('useTransformationController', () {
testWidgets('initial values matches with real constructor', (tester) async {
late TransformationController controller;
late TransformationController controller2;

await tester.pumpWidget(
HookBuilder(builder: (context) {
controller2 = TransformationController();
controller = useTransformationController();
return Container();
}),
);

expect(controller.value, controller2.value);
});
testWidgets("returns a TransformationController that doesn't change",
(tester) async {
late TransformationController controller;
late TransformationController controller2;

await tester.pumpWidget(
HookBuilder(builder: (context) {
controller = useTransformationController();
return Container();
}),
);

expect(controller, isA<TransformationController>());

await tester.pumpWidget(
HookBuilder(builder: (context) {
controller2 = useTransformationController();
return Container();
}),
);

expect(identical(controller, controller2), isTrue);
});

testWidgets('passes hook parameters to the TransformationController',
(tester) async {
late TransformationController controller;

await tester.pumpWidget(
HookBuilder(
builder: (context) {
controller = useTransformationController(
initialValue: Matrix4.translationValues(1, 2, 3),
);

return Container();
},
),
);

expect(controller.value, Matrix4.translationValues(1, 2, 3));
});
});
}

class TickerProviderMock extends Mock implements TickerProvider {}