Browse Source

节点可拖动

master
hejl 4 weeks ago
parent
commit
4e377a67e9
  1. 57
      win_text_editor/lib/shared/components/tree_view.dart

57
win_text_editor/lib/shared/components/tree_view.dart

@ -23,6 +23,8 @@ class TreeViewConfig {
final Map<String, IconData> icons; final Map<String, IconData> icons;
final bool showRefreshButton; final bool showRefreshButton;
final IconData refreshIcon; final IconData refreshIcon;
final bool draggable; //
final bool droppable; //
const TreeViewConfig({ const TreeViewConfig({
this.lazyLoad = false, this.lazyLoad = false,
@ -34,6 +36,8 @@ class TreeViewConfig {
this.icons = const {}, this.icons = const {},
this.showRefreshButton = false, this.showRefreshButton = false,
this.refreshIcon = Icons.refresh, this.refreshIcon = Icons.refresh,
this.draggable = false,
this.droppable = false,
}); });
} }
@ -98,7 +102,20 @@ class _TreeViewState extends State<TreeView> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Stack( return Stack(
children: [ children: [
ListView.builder( DragTarget<TreeNode>(
onWillAcceptWithDetails: (data) {
//
if (!widget.config.droppable) return false;
return true;
},
onAcceptWithDetails: (draggedNode) {
//
//
debugPrint('Dropped ${draggedNode.data.name}');
},
builder: (context, candidateData, rejectedData) {
return ListView.builder(
controller: _effectiveController, controller: _effectiveController,
physics: const ClampingScrollPhysics(), physics: const ClampingScrollPhysics(),
itemCount: _countVisibleNodes(widget.nodes), itemCount: _countVisibleNodes(widget.nodes),
@ -108,7 +125,40 @@ class _TreeViewState extends State<TreeView> {
return widget.nodeBuilder != null return widget.nodeBuilder != null
? widget.nodeBuilder!(context, node, isSelected, () => _handleNodeTap(node)) ? widget.nodeBuilder!(context, node, isSelected, () => _handleNodeTap(node))
: TreeNodeWidget( : Draggable<TreeNode>(
data: node,
feedback: Material(
child: Container(
width: 200,
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Theme.of(context).cardColor,
borderRadius: BorderRadius.circular(4),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.2),
blurRadius: 4,
offset: const Offset(0, 2),
),
],
),
child: Text(node.title),
),
),
childWhenDragging: Opacity(
opacity: 0.5,
child: TreeNodeWidget(
key: ValueKey(node.id),
node: node,
config: widget.config,
isSelected: isSelected,
isChecked: _checkedIds.contains(node.id),
onTap: () => _handleNodeTap(node),
onDoubleTap: () => widget.onNodeDoubleTap?.call(node),
onCheckChanged: (value) => _handleNodeCheckChanged(node, value),
),
),
child: TreeNodeWidget(
key: ValueKey(node.id), key: ValueKey(node.id),
node: node, node: node,
config: widget.config, config: widget.config,
@ -117,6 +167,9 @@ class _TreeViewState extends State<TreeView> {
onTap: () => _handleNodeTap(node), onTap: () => _handleNodeTap(node),
onDoubleTap: () => widget.onNodeDoubleTap?.call(node), onDoubleTap: () => widget.onNodeDoubleTap?.call(node),
onCheckChanged: (value) => _handleNodeCheckChanged(node, value), onCheckChanged: (value) => _handleNodeCheckChanged(node, value),
),
);
},
); );
}, },
), ),

Loading…
Cancel
Save