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 draggable_scrollable_controller #417

Merged
merged 7 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
part of 'hooks.dart';

/// Creates a [DraggableScrollableController] that will be disposed automatically.
///
/// See also:
/// - [DraggableScrollableController]
DraggableScrollableController useDraggableScrollableController(
{List<Object?>? keys}) {
dickermoshe marked this conversation as resolved.
Show resolved Hide resolved
return use(_DraggableScrollableControllerHook(keys: keys));
}

class _DraggableScrollableControllerHook
extends Hook<DraggableScrollableController> {
const _DraggableScrollableControllerHook({List<Object?>? keys})
: super(keys: keys);
rrousselGit marked this conversation as resolved.
Show resolved Hide resolved

@override
HookState<DraggableScrollableController, Hook<DraggableScrollableController>>
createState() => _DraggableScrollableControllerHookState();
}

class _DraggableScrollableControllerHookState extends HookState<
DraggableScrollableController, _DraggableScrollableControllerHook> {
final controller = DraggableScrollableController();

@override
String get debugLabel => 'useDraggableScrollableController';

@override
DraggableScrollableController build(BuildContext context) => controller;

@override
void dispose() => controller.dispose();
}
2 changes: 2 additions & 0 deletions packages/flutter_hooks/lib/src/hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'
show
Brightness,
DraggableScrollableController,
ExpansionTileController,
MaterialState,
MaterialStatesController,
Expand All @@ -16,6 +17,7 @@ import 'framework.dart';

part 'animation.dart';
part 'async.dart';
part 'draggable_scrollable_controller.dart';
part 'expansion_tile_controller.dart';
part 'focus_node.dart';
part 'focus_scope_node.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
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) {
useDraggableScrollableController();
return const SizedBox();
}),
);

await tester.pump();

final element = tester.element(find.byType(HookBuilder));

expect(
element
.toDiagnosticsNode(style: DiagnosticsTreeStyle.offstage)
.toStringDeep(),
equalsIgnoringHashCodes('HookBuilder\n'
' │ useDraggableScrollableController: Instance of\n'
" │ 'DraggableScrollableController'\n"
' └SizedBox(renderObject: RenderConstrainedBox#00000)\n'),
);
});

group('useDraggableScrollableController', () {
testWidgets(
'controller functions correctly and initial values matches with real constructor',
(tester) async {
late DraggableScrollableController controller;
final controller2 = DraggableScrollableController();

await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: HookBuilder(builder: (context) {
return Column(
children: [
ElevatedButton(
onPressed: () {
showBottomSheet(
context: context,
builder: (context) {
// Using a builder here to ensure that the controller is
// disposed when the sheet is closed.
return HookBuilder(builder: (context) {
controller = useDraggableScrollableController();
return DraggableScrollableSheet(
controller: controller,
builder: (context, scrollController) {
return ListView.builder(
controller: scrollController,
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
title: Text('Item $index on Sheet 1'),
);
},
);
},
);
});
});
},
child: Text("Open Sheet 1")),
ElevatedButton(
onPressed: () {
showBottomSheet(
context: context,
builder: (context) {
return DraggableScrollableSheet(
controller: controller2,
builder: (context, scrollController) {
return ListView.builder(
controller: scrollController,
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
title: Text('Item $index on Sheet 2'),
);
},
);
},
);
});
},
child: Text("Open Sheet 2"))
],
);
}),
),
));

// Open Sheet 1 and get the initial values
await tester.tap(find.text('Open Sheet 1'));
await tester.pumpAndSettle();
final controllerPixels = controller.pixels;
final controllerSize = controller.size;
final controllerIsAttached = controller.isAttached;
// Close Sheet 1 by dragging it down
await tester.fling(
find.byType(DraggableScrollableSheet), const Offset(0, 500), 300);
await tester.pumpAndSettle();

// Open Sheet 2 and get the initial values
await tester.tap(find.text('Open Sheet 2'));
await tester.pumpAndSettle();
final controller2Pixels = controller2.pixels;
final controller2Size = controller2.size;
final controller2IsAttached = controller2.isAttached;
// Close Sheet 2 by dragging it down
await tester.fling(
find.byType(DraggableScrollableSheet), const Offset(0, 500), 300);
await tester.pumpAndSettle();

// Compare the initial values of the two controllers
expect(controllerPixels, controller2Pixels);
expect(controllerSize, controller2Size);
expect(controllerIsAttached, controller2IsAttached);

// Open Sheet 1 again and use the controller to scroll
await tester.tap(find.text('Open Sheet 1'));
await tester.pumpAndSettle();
const targetSize = 1.0;
controller.jumpTo(targetSize);
expect(targetSize, controller.size);
});
});
}
Loading