-
-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
useFixedExtentScrollController
(#437)
- Loading branch information
1 parent
a10ec1e
commit 835aea0
Showing
7 changed files
with
170 additions
and
33 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
57 changes: 57 additions & 0 deletions
57
packages/flutter_hooks/lib/src/fixed_extent_scroll_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,57 @@ | ||
part of 'hooks.dart'; | ||
|
||
/// Creates [FixedExtentScrollController] that will be disposed automatically. | ||
/// | ||
/// See also: | ||
/// - [FixedExtentScrollController] | ||
FixedExtentScrollController useFixedExtentScrollController({ | ||
int initialItem = 0, | ||
ScrollControllerCallback? onAttach, | ||
ScrollControllerCallback? onDetach, | ||
List<Object?>? keys, | ||
}) { | ||
return use( | ||
_FixedExtentScrollControllerHook( | ||
initialItem: initialItem, | ||
onAttach: onAttach, | ||
onDetach: onDetach, | ||
keys: keys, | ||
), | ||
); | ||
} | ||
|
||
class _FixedExtentScrollControllerHook | ||
extends Hook<FixedExtentScrollController> { | ||
const _FixedExtentScrollControllerHook({ | ||
required this.initialItem, | ||
this.onAttach, | ||
this.onDetach, | ||
super.keys, | ||
}); | ||
|
||
final int initialItem; | ||
final ScrollControllerCallback? onAttach; | ||
final ScrollControllerCallback? onDetach; | ||
|
||
@override | ||
HookState<FixedExtentScrollController, Hook<FixedExtentScrollController>> | ||
createState() => _FixedExtentScrollControllerHookState(); | ||
} | ||
|
||
class _FixedExtentScrollControllerHookState extends HookState< | ||
FixedExtentScrollController, _FixedExtentScrollControllerHook> { | ||
late final controller = FixedExtentScrollController( | ||
initialItem: hook.initialItem, | ||
onAttach: hook.onAttach, | ||
onDetach: hook.onDetach, | ||
); | ||
|
||
@override | ||
FixedExtentScrollController build(BuildContext context) => controller; | ||
|
||
@override | ||
void dispose() => controller.dispose(); | ||
|
||
@override | ||
String get debugLabel => 'useFixedExtentScrollController'; | ||
} |
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
102 changes: 102 additions & 0 deletions
102
packages/flutter_hooks/test/use_fixed_extent_scroll_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,102 @@ | ||
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 'mock.dart'; | ||
|
||
void main() { | ||
testWidgets('debugFillProperties', (tester) async { | ||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
useFixedExtentScrollController(); | ||
return const SizedBox(); | ||
}), | ||
); | ||
|
||
final element = tester.element(find.byType(HookBuilder)); | ||
|
||
expect( | ||
element | ||
.toDiagnosticsNode(style: DiagnosticsTreeStyle.offstage) | ||
.toStringDeep(), | ||
equalsIgnoringHashCodes( | ||
'HookBuilder\n' | ||
' │ useFixedExtentScrollController:\n' | ||
' │ FixedExtentScrollController#00000(no clients)\n' | ||
' └SizedBox(renderObject: RenderConstrainedBox#00000)\n', | ||
), | ||
); | ||
}); | ||
|
||
group('useFixedExtentScrollController', () { | ||
testWidgets('initial values matches with real constructor', (tester) async { | ||
late FixedExtentScrollController controller; | ||
late FixedExtentScrollController controller2; | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
controller2 = FixedExtentScrollController(); | ||
controller = useFixedExtentScrollController(); | ||
return Container(); | ||
}), | ||
); | ||
|
||
expect(controller.debugLabel, controller2.debugLabel); | ||
expect(controller.initialItem, controller2.initialItem); | ||
expect(controller.onAttach, controller2.onAttach); | ||
expect(controller.onDetach, controller2.onDetach); | ||
}); | ||
testWidgets("returns a FixedExtentScrollController that doesn't change", | ||
(tester) async { | ||
late FixedExtentScrollController controller; | ||
late FixedExtentScrollController controller2; | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
controller2 = FixedExtentScrollController(); | ||
controller = useFixedExtentScrollController(); | ||
return Container(); | ||
}), | ||
); | ||
expect(controller, isA<FixedExtentScrollController>()); | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
controller2 = useFixedExtentScrollController(); | ||
return Container(); | ||
}), | ||
); | ||
|
||
expect(identical(controller, controller2), isTrue); | ||
}); | ||
|
||
testWidgets('passes hook parameters to the FixedExtentScrollController', | ||
(tester) async { | ||
late FixedExtentScrollController controller; | ||
|
||
void onAttach(ScrollPosition position) {} | ||
void onDetach(ScrollPosition position) {} | ||
|
||
await tester.pumpWidget( | ||
HookBuilder( | ||
builder: (context) { | ||
controller = useFixedExtentScrollController( | ||
initialItem: 42, | ||
onAttach: onAttach, | ||
onDetach: onDetach, | ||
); | ||
|
||
return Container(); | ||
}, | ||
), | ||
); | ||
|
||
expect(controller.initialItem, 42); | ||
expect(controller.onAttach, onAttach); | ||
expect(controller.onDetach, onDetach); | ||
}); | ||
}); | ||
} | ||
|
||
class TickerProviderMock extends Mock implements TickerProvider {} |
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