Skip to content

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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions packages/flutter_hooks/lib/src/cupertino_tab_controller.dart
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';
}
3 changes: 3 additions & 0 deletions packages/flutter_hooks/lib/src/hooks.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:async';

import 'package:flutter/cupertino.dart'
show CupertinoTabController;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'
show
Expand All @@ -21,6 +23,7 @@ import 'framework.dart';
part 'animation.dart';
part 'async.dart';
part 'carousel_controller.dart';
part 'cupertino_tab_controller.dart';
part 'debounced.dart';
part 'draggable_scrollable_controller.dart';
part 'expansion_tile_controller.dart';
Expand Down
89 changes: 89 additions & 0 deletions packages/flutter_hooks/test/use_cupertino_tab_controller_test.dart
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';

Comment on lines +1 to +7
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Tests fail to compile – missing flutter_test import & unused mock.dart

testWidgets, equalsIgnoringHashCodes, and other testing utilities live in package:flutter_test/flutter_test.dart, which is not imported.
mock.dart is imported but never referenced, causing an “unused import” lint.

-import 'package:flutter/foundation.dart';
-import 'package:flutter_hooks/src/framework.dart';
-import 'package:flutter_hooks/src/hooks.dart';
-import 'mock.dart';
+import 'package:flutter_test/flutter_test.dart';
+import 'package:flutter_hooks/flutter_hooks.dart';

(Remove mock.dart unless it is actually used.)

This change lets the suite compile and removes needless internal-API exposure.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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';
import 'package:flutter/cupertino.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
🤖 Prompt for AI Agents
In packages/flutter_hooks/test/use_cupertino_tab_controller_test.dart at lines 1
to 7, the test file is missing the import for
package:flutter_test/flutter_test.dart, which provides essential testing
utilities like testWidgets and equalsIgnoringHashCodes. Add this import to
enable the tests to compile. Also, remove the import of mock.dart since it is
not used anywhere in the file to avoid unused import lint warnings and reduce
unnecessary internal API exposure.

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);
});
});
}