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 animationDuration property to useTabController hook #446

Merged
merged 9 commits into from
Feb 23, 2025
6 changes: 3 additions & 3 deletions packages/flutter_hooks/lib/src/hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
9 changes: 7 additions & 2 deletions packages/flutter_hooks/lib/src/tab_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -17,6 +18,7 @@ TabController useTabController({
vsync: vsync,
length: initialLength,
initialIndex: initialIndex,
animationDuration: animationDuration,
keys: keys,
),
);
Expand All @@ -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() =>
Expand All @@ -44,6 +48,7 @@ class _TabControllerHookState
late final controller = TabController(
length: hook.length,
initialIndex: hook.initialIndex,
animationDuration: hook.animationDuration,
vsync: hook.vsync,
);

Expand Down
19 changes: 19 additions & 0 deletions packages/flutter_hooks/test/use_tab_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}

Expand Down