Skip to content

Commit

Permalink
确保更新actions的时候手势监听器能正常更新 (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckysmg authored May 29, 2023
1 parent 23bb5a8 commit 908c0db
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 27 deletions.
40 changes: 13 additions & 27 deletions lib/core/cell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ class SwipeActionCellState extends State<SwipeActionCell>

bool get hasLeadingAction => leadingActionsCount > 0;

final _CellStateInfo cellStateInfo = _CellStateInfo();

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -778,17 +776,16 @@ class SwipeActionCellState extends State<SwipeActionCell>
}),
if (widget.isDraggable)
_DirectionDependentDragGestureRecognizer:
GestureRecognizerFactoryWithHandlers<
_DirectionDependentDragGestureRecognizer>(
() => _DirectionDependentDragGestureRecognizer(
cellStateInfo: cellStateInfo,
canDragToLeft: hasTrailingAction,
canDragToRight: hasLeadingAction), (instance) {
GestureRecognizerFactoryWithHandlers<_DirectionDependentDragGestureRecognizer>(
() => _DirectionDependentDragGestureRecognizer(), (instance) {
instance
..onStart = _onHorizontalDragStart
..onUpdate = _onHorizontalDragUpdate
..onEnd = _onHorizontalDragEnd
..gestureSettings = gestureSettings;
..gestureSettings = gestureSettings
..isActionShowing = whenTrailingActionShowing || whenLeadingActionShowing
..canDragToLeft = hasTrailingAction
..canDragToRight = hasLeadingAction;
}),
};
}
Expand All @@ -805,8 +802,6 @@ class SwipeActionCellState extends State<SwipeActionCell>

whenTrailingActionShowing = currentOffset.dx < 0;
whenLeadingActionShowing = currentOffset.dx > 0;
cellStateInfo.isActionShowing =
whenTrailingActionShowing || whenLeadingActionShowing;

final Widget selectedButton = widget.controller != null &&
(widget.controller!.isEditing.value || editController.isAnimating)
Expand Down Expand Up @@ -887,7 +882,7 @@ class SwipeActionCellState extends State<SwipeActionCell>
}

Widget _buildLeadingActionButtons() {
if (currentOffset.dx < 0) {
if (currentOffset.dx < 0 || !hasLeadingAction) {
return const SizedBox();
}
final List<Widget> actionButtons =
Expand Down Expand Up @@ -923,7 +918,7 @@ class SwipeActionCellState extends State<SwipeActionCell>
}

Widget _buildTrailingActionButtons() {
if (currentOffset.dx > 0) {
if (currentOffset.dx > 0 || !hasTrailingAction) {
return const SizedBox();
}
final List<Widget> actionButtons =
Expand Down Expand Up @@ -1109,27 +1104,18 @@ class SwipeNestedAction {

class _DirectionDependentDragGestureRecognizer
extends HorizontalDragGestureRecognizer {
final bool canDragToLeft;
final bool canDragToRight;
final _CellStateInfo cellStateInfo;

_DirectionDependentDragGestureRecognizer(
{required this.cellStateInfo,
required this.canDragToLeft,
required this.canDragToRight});
late bool canDragToLeft;
late bool canDragToRight;
late bool isActionShowing;

@override
void handleEvent(PointerEvent event) {
final delta = event.delta.dx;
if (cellStateInfo.isActionShowing ||
final double delta = event.delta.dx;
if (isActionShowing ||
canDragToLeft && delta < 0 ||
canDragToRight && delta > 0 ||
delta == 0) {
super.handleEvent(event);
}
}
}

class _CellStateInfo {
bool isActionShowing = false;
}
113 changes: 113 additions & 0 deletions test/tests.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_swipe_action_cell/flutter_swipe_action_cell.dart';

void main() {
testWidgets('Actions buttons can show and hide correctly when actions update.', (tester) async {
final GlobalKey key = GlobalKey();
final List<SwipeAction> trailingActions = [
SwipeAction(onTap: (handler) {}, title: "trailingAction 1"),
SwipeAction(onTap: (handler) {}, title: "trailingAction 2"),
];
final List<SwipeAction> leadingActions = [
SwipeAction(onTap: (handler) {}, title: "leadingActions 1"),
];

final SwipeActionController controller = SwipeActionController();

// No actions. we expect to find nothing.
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: ListView(
children: [
SwipeActionCell(
controller: controller,
key: key,
child: Container(
color: Colors.red,
height: 200,
),
),
],
),
),
));

await tester.timedDrag(
find.byKey(key),
const Offset(-100, 0),
const Duration(milliseconds: 100),
);
expect(find.text('trailingAction 1'), findsNothing);
expect(find.text('trailingAction 2'), findsNothing);

controller.closeAllOpenCell();
await tester.pumpAndSettle();
await tester.timedDrag(
find.byKey(key),
const Offset(100, 0),
const Duration(milliseconds: 100),
);
expect(find.text('leadingActions 1'), findsNothing);


controller.closeAllOpenCell();
await tester.pumpAndSettle();

// Now update the trailing and leading actions, we expect to see buttons when dragging.
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: ListView(
children: [
SwipeActionCell(
trailingActions: trailingActions,
leadingActions: leadingActions,
key: key,
child: Container(
color: Colors.red,
height: 200,
),
),
],
),
),
));
await tester.timedDrag(
find.byKey(key), const Offset(-100, 0), const Duration(milliseconds: 100));
expect(find.text('trailingAction 1'), findsOneWidget);
expect(find.text('trailingAction 2'), findsOneWidget);

controller.closeAllOpenCell();
await tester.pumpAndSettle();
await tester.timedDrag(
find.byKey(key), const Offset(100, 0), const Duration(milliseconds: 100));
expect(find.text('leadingActions 1'), findsOneWidget);

// No update the actions to null again, and we expect to see buttons.
await tester.pumpWidget(MaterialApp(
home: Scaffold(
body: ListView(
children: [
SwipeActionCell(
key: key,
child: Container(
color: Colors.red,
height: 200,
),
),
],
),
),
));
await tester.timedDrag(
find.byKey(key), const Offset(-100, 0), const Duration(milliseconds: 100));
expect(find.text('trailingAction 1'), findsNothing);
expect(find.text('trailingAction 2'), findsNothing);

controller.closeAllOpenCell();
await tester.pumpAndSettle();
await tester.timedDrag(
find.byKey(key), const Offset(100, 0), const Duration(milliseconds: 100));
expect(find.text('leadingActions 1'), findsNothing);
});
}

0 comments on commit 908c0db

Please sign in to comment.