Repository: rrousselGit/flutter_hooks
Pull request #446 (2024-09-26 21:07:23)

Add animationDuration property to useTabController hook


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

- **New Features**
	- Introduced an optional `animationDuration` parameter for the `useTabController` function, allowing for customizable tab transition animations.
	- Added `kTabScrollDuration` export from the Flutter material library for enhanced functionality.

- **Improvements**
	- Enhanced the `TabController` to accept the new `animationDuration` parameter for improved user experience during tab transitions.

- **Tests**
	- Added a test case to verify that the `animationDuration` property of the `TabController` matches when created using the `useTabController` hook and directly via constructor.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->


Patch:
diff --git a/packages/flutter_hooks/lib/src/hooks.dart b/packages/flutter_hooks/lib/src/hooks.dart
index 8cce956..d1e4664 100644
--- a/packages/flutter_hooks/lib/src/hooks.dart
+++ b/packages/flutter_hooks/lib/src/hooks.dart
@@ -7,11 +7,11 @@ import 'package:flutter/material.dart'
         CarouselController,
         DraggableScrollableController,
         ExpansionTileController,
-        WidgetStatesController,
         SearchController,
         TabController,
-        TreeSliverController,
-        WidgetState;
+        WidgetState,
+        WidgetStatesController,
+        kTabScrollDuration;
 import 'package:flutter/scheduler.dart';
 import 'package:flutter/widgets.dart';
 
diff --git a/packages/flutter_hooks/lib/src/tab_controller.dart b/packages/flutter_hooks/lib/src/tab_controller.dart
index 5aba96c..eef5f0a 100644
--- a/packages/flutter_hooks/lib/src/tab_controller.dart
+++ b/packages/flutter_hooks/lib/src/tab_controller.dart
@@ -6,6 +6,7 @@ part of 'hooks.dart';
 /// - [TabController]
 TabController useTabController({
   required int initialLength,
+  Duration? animationDuration = kTabScrollDuration,
   TickerProvider? vsync,
   int initialIndex = 0,
   List<Object?>? keys,
@@ -17,6 +18,7 @@ TabController useTabController({
       vsync: vsync,
       length: initialLength,
       initialIndex: initialIndex,
+      animationDuration: animationDuration,
       keys: keys,
     ),
   );
@@ -27,12 +29,14 @@ class _TabControllerHook extends Hook<TabController> {
     required this.length,
     required this.vsync,
     required this.initialIndex,
-    List<Object?>? keys,
-  }) : super(keys: keys);
+    required this.animationDuration,
+    super.keys,
+  });
 
   final int length;
   final TickerProvider vsync;
   final int initialIndex;
+  final Duration? animationDuration;
 
   @override
   HookState<TabController, Hook<TabController>> createState() =>
@@ -44,6 +48,7 @@ class _TabControllerHookState
   late final controller = TabController(
     length: hook.length,
     initialIndex: hook.initialIndex,
+    animationDuration: hook.animationDuration,
     vsync: hook.vsync,
   );
 


Test patch:
diff --git a/packages/flutter_hooks/test/use_tab_controller_test.dart b/packages/flutter_hooks/test/use_tab_controller_test.dart
index 80ddaf9..b4dffce 100644
--- a/packages/flutter_hooks/test/use_tab_controller_test.dart
+++ b/packages/flutter_hooks/test/use_tab_controller_test.dart
@@ -138,6 +138,25 @@ void main() {
       verifyNoMoreInteractions(vsync);
       ticker.dispose();
     });
+
+    testWidgets('initial animationDuration matches with real constructor',
+        (tester) async {
+      late TabController controller;
+      late TabController controller2;
+
+      await tester.pumpWidget(
+        HookBuilder(
+          builder: (context) {
+            final vsync = useSingleTickerProvider();
+            controller = useTabController(initialLength: 4);
+            controller2 = TabController(length: 4, vsync: vsync);
+            return Container();
+          },
+        ),
+      );
+
+      expect(controller.animationDuration, controller2.animationDuration);
+    });
   });
 }