Enable long-press handling

This commit is contained in:
luckyrat 2021-07-09 11:21:26 +01:00
parent 734a06452f
commit 71fb121848
2 changed files with 18 additions and 0 deletions

View file

@ -109,6 +109,14 @@ class _TreeNodeState<T> extends State<TreeNode<T>>
}
}
void _handleLongPress() {
TreeView<T>? _treeView = TreeView.of<T>(context);
assert(_treeView != null, 'TreeView must exist in context');
if (_treeView!.onNodeLongPress != null) {
_treeView.onNodeLongPress!(widget.node.key);
}
}
void _handleDoubleTap() {
TreeView<T>? _treeView = TreeView.of<T>(context);
assert(_treeView != null, 'TreeView must exist in context');
@ -213,17 +221,20 @@ class _TreeNodeState<T> extends State<TreeNode<T>>
Widget _tappable = _treeView.onNodeDoubleTap != null
? InkWell(
onTap: _handleTap,
onLongPress: _handleLongPress,
onDoubleTap: _handleDoubleTap,
child: labelContainer,
)
: InkWell(
onTap: _handleTap,
onLongPress: _handleLongPress,
child: labelContainer,
);
if (widget.node.isParent) {
if (_treeView.supportParentDoubleTap && canSelectParent) {
_tappable = InkWell(
onTap: canSelectParent ? _handleTap : _handleExpand,
onLongPress: _handleLongPress,
onDoubleTap: () {
_handleExpand();
_handleDoubleTap();
@ -233,12 +244,14 @@ class _TreeNodeState<T> extends State<TreeNode<T>>
} else if (_treeView.supportParentDoubleTap) {
_tappable = InkWell(
onTap: _handleExpand,
onLongPress: _handleLongPress,
onDoubleTap: _handleDoubleTap,
child: labelContainer,
);
} else {
_tappable = InkWell(
onTap: canSelectParent ? _handleTap : _handleExpand,
onLongPress: _handleLongPress,
child: labelContainer,
);
}

View file

@ -38,6 +38,9 @@ class TreeView<T> extends InheritedWidget {
/// The double tap handler for a node. Passes the node key.
final Function(String)? onNodeDoubleTap;
/// The long press handler for a node. Passes the node key.
final Function(String)? onNodeLongPress;
/// The expand/collapse handler for a node. Passes the node key and the
/// expansion state.
final Function(String, bool)? onExpansionChanged;
@ -81,6 +84,7 @@ class TreeView<T> extends InheritedWidget {
required this.controller,
this.onNodeTap,
this.onNodeDoubleTap,
this.onNodeLongPress,
this.physics,
this.onExpansionChanged,
this.allowParentSelect: false,
@ -107,6 +111,7 @@ class TreeView<T> extends InheritedWidget {
bool updateShouldNotify(TreeView oldWidget) {
return oldWidget.controller.children != this.controller.children ||
oldWidget.onNodeTap != this.onNodeTap ||
oldWidget.onNodeLongPress != this.onNodeLongPress ||
oldWidget.onExpansionChanged != this.onExpansionChanged ||
oldWidget.theme != this.theme ||
oldWidget.supportParentDoubleTap != this.supportParentDoubleTap ||