-
-
Notifications
You must be signed in to change notification settings - Fork 186
Add useCupertinoTabController hook for automatic disposal of CupertinoTabController #475
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
Open
Erengun
wants to merge
4
commits into
rrousselGit:master
Choose a base branch
from
Erengun:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5d37deb
Add useCupertinoTabController hook for automatic disposal of Cupertin…
Erengun 01cf58a
Add import for CupertinoTabController and include it in part directives
Erengun 97a3bef
Add tests for useCupertinoTabController functionality
Erengun 470968f
Fix typo in test group name for useCupertinoTabController
Erengun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
packages/flutter_hooks/lib/src/cupertino_tab_controller.dart
This file contains hidden or 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,47 @@ | ||
part of 'hooks.dart'; | ||
|
||
|
||
/// Creates a [CupertinoTabController] that will be disposed automatically. | ||
/// | ||
/// See also: | ||
/// - [CupertinoTabController] | ||
CupertinoTabController useCupertinoTabController({ | ||
int initialIndex = 0, | ||
List<Object?>? keys, | ||
}) { | ||
return use( | ||
_CupertinoTabControllerHook( | ||
initialIndex: initialIndex, | ||
keys: keys, | ||
), | ||
); | ||
} | ||
|
||
class _CupertinoTabControllerHook extends Hook<CupertinoTabController> { | ||
const _CupertinoTabControllerHook({ | ||
required this.initialIndex, | ||
super.keys, | ||
}); | ||
|
||
final int initialIndex; | ||
|
||
@override | ||
HookState<CupertinoTabController, Hook<CupertinoTabController>> createState() => | ||
_CupertinoTabControllerHookState(); | ||
} | ||
|
||
class _CupertinoTabControllerHookState | ||
extends HookState<CupertinoTabController, _CupertinoTabControllerHook> { | ||
late final controller = CupertinoTabController( | ||
initialIndex: hook.initialIndex, | ||
); | ||
|
||
@override | ||
CupertinoTabController build(BuildContext context) => controller; | ||
|
||
@override | ||
void dispose() => controller.dispose(); | ||
|
||
@override | ||
String get debugLabel => 'useCupertinoTabController'; | ||
} |
This file contains hidden or 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
89 changes: 89 additions & 0 deletions
89
packages/flutter_hooks/test/use_cupertino_tab_controller_test.dart
This file contains hidden or 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,89 @@ | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/foundation.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) { | ||
useCupertinoTabController(); | ||
return const SizedBox(); | ||
}), | ||
); | ||
|
||
await tester.pump(); | ||
|
||
final element = tester.element(find.byType(HookBuilder)); | ||
|
||
expect( | ||
element | ||
.toDiagnosticsNode(style: DiagnosticsTreeStyle.offstage) | ||
.toStringDeep(), | ||
equalsIgnoringHashCodes( | ||
'HookBuilder\n' | ||
" │ useCupertinoTabController: Instance of 'CupertinoTabController'\n" | ||
' └SizedBox(renderObject: RenderConstrainedBox#00000)\n', | ||
), | ||
); | ||
}); | ||
|
||
group('useCupertinoTabController', () { | ||
testWidgets('initial values matches with real constructor', (tester) async { | ||
late CupertinoTabController controller; | ||
late CupertinoTabController controller2; | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
controller2 = CupertinoTabController(); | ||
controller = useCupertinoTabController(); | ||
return Container(); | ||
}), | ||
); | ||
|
||
expect(controller.index, controller2.index); | ||
}); | ||
testWidgets("returns a CupertinoTabController that doesn't change", | ||
(tester) async { | ||
late CupertinoTabController controller; | ||
late CupertinoTabController controller2; | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
controller = useCupertinoTabController(initialIndex: 1); | ||
return Container(); | ||
}), | ||
); | ||
|
||
expect(controller, isA<CupertinoTabController>()); | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
controller2 = useCupertinoTabController(initialIndex: 1); | ||
return Container(); | ||
}), | ||
); | ||
|
||
expect(identical(controller, controller2), isTrue); | ||
}); | ||
|
||
testWidgets('passes hook parameters to the CupertinoTabController', | ||
(tester) async { | ||
late CupertinoTabController controller; | ||
|
||
await tester.pumpWidget( | ||
HookBuilder( | ||
builder: (context) { | ||
controller = useCupertinoTabController(initialIndex: 2); | ||
|
||
return Container(); | ||
}, | ||
), | ||
); | ||
|
||
expect(controller.index, 2); | ||
}); | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests fail to compile – missing
flutter_test
import & unusedmock.dart
testWidgets
,equalsIgnoringHashCodes
, and other testing utilities live inpackage:flutter_test/flutter_test.dart
, which is not imported.mock.dart
is imported but never referenced, causing an “unused import” lint.(Remove
mock.dart
unless it is actually used.)This change lets the suite compile and removes needless internal-API exposure.
📝 Committable suggestion
🤖 Prompt for AI Agents