Skip to content
Merged
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
11 changes: 7 additions & 4 deletions lib/src/act/act.dart
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,13 @@ class Act {
final dragStartSnapshot = dragStart.snapshot()..existsOnce();
_detectSizeZero(snapshot: dragStartSnapshot);

// Take the closest Scrollable above the dragStart widget. This is the
// widget which makes a widget scrollable. It must always exist.
final WidgetSelector<Scrollable> scrollable =
spot<Scrollable>().withChild(dragStart).last();
final WidgetSelector<Scrollable> scrollable = () {
final element = dragStartSnapshot.discoveredElement!;
if (element.widget is Scrollable) {
return spotElement<Scrollable>(element);
}
return spot<Scrollable>().withChild(dragStart).last();
}();

// Save the 'Element' of the currently targeted Scrollable.
// This ensures that—even if multiple scrollables exist or the
Expand Down
51 changes: 50 additions & 1 deletion test/act/act_drag_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:spot/spot.dart';
Expand Down Expand Up @@ -120,6 +119,56 @@ void dragTests() {
await act.tap(secondItem);
},
);

testWidgets(
'Finds widget in vertical ListView after dragging when dragStart is a Scrollable',
(tester) async {
await tester.pumpWidget(
const DragUntilVisibleSingleDirectionTestWidget(
axis: Axis.vertical,
ignorePointerAtIndices: [0, 1, 2, 3, 4, 5, 6, 7, 8],
),
);

final firstItem = spot<DragUntilVisibleSingleDirectionTestWidget>()
.spot<Scrollable>()
.last()
..existsOnce();
final secondItem = spotText('Item at index: 27', exact: true)
..doesNotExist();
await act.dragUntilVisible(
dragStart: firstItem,
dragTarget: secondItem,
maxIteration: 30,
);
secondItem.existsOnce();
},
);

testWidgets(
'Finds widget after dragging when a keyed dragStart resolves to a Scrollable',
(tester) async {
const scrollableKey = ValueKey('direct-scrollable');
await tester.pumpWidget(
const DragUntilVisibleSingleDirectionTestWidget(
axis: Axis.vertical,
ignorePointerAtIndices: [0, 1, 2, 3, 4, 5, 6, 7, 8],
scrollableKey: scrollableKey,
),
);

final firstItem = spotKey(scrollableKey)..existsOnce();
expect(firstItem.snapshotWidget(), isA<Scrollable>());
final secondItem = spotText('Item at index: 27', exact: true)
..doesNotExist();
await act.dragUntilVisible(
dragStart: firstItem,
dragTarget: secondItem,
maxIteration: 30,
);
secondItem.existsOnce();
},
);
});

group('Horizontal Drag', () {
Expand Down
26 changes: 26 additions & 0 deletions test/timeline/drag/drag_until_visible_test_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ class DragUntilVisibleSingleDirectionTestWidget extends StatelessWidget {
required this.axis,
this.ignorePointerAtIndices = const [0, 0],
this.useColumnOrRow = false,
this.scrollableKey,
});

final Axis axis;
final List<int> ignorePointerAtIndices;
final bool useColumnOrRow;
final Key? scrollableKey;

@override
Widget build(BuildContext context) {
Expand All @@ -31,6 +33,23 @@ class DragUntilVisibleSingleDirectionTestWidget extends StatelessWidget {
);

final child = () {
if (scrollableKey != null) {
return Scrollable(
key: scrollableKey,
viewportBuilder: (context, position) {
return Viewport(
axisDirection: _axisDirection(axis),
offset: position,
slivers: [
SliverFixedExtentList(
itemExtent: 100,
delegate: SliverChildListDelegate(items),
),
],
);
},
);
}
if (useColumnOrRow) {
return SingleChildScrollView(
scrollDirection: axis,
Expand Down Expand Up @@ -78,6 +97,13 @@ class DragUntilVisibleSingleDirectionTestWidget extends StatelessWidget {
}
}

AxisDirection _axisDirection(Axis axis) {
if (axis == Axis.vertical) {
return AxisDirection.down;
}
return AxisDirection.right;
}

class NestedScrollDragUntilVisibleTestWidget extends StatelessWidget {
const NestedScrollDragUntilVisibleTestWidget({
super.key,
Expand Down
Loading