From 4d3573da9888e5d243c730f20b412be9a98bbcca Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sat, 26 Sep 2020 05:35:43 +0200 Subject: [PATCH] GraphView: Tap gestures - get the node position properly Earlier this was only working as the global and local coordinates were the same. Now that I've added a SafeArea that is no longer the case. --- lib/screens/graph_view.dart | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/screens/graph_view.dart b/lib/screens/graph_view.dart index b30d42c7..2e5bf7d2 100644 --- a/lib/screens/graph_view.dart +++ b/lib/screens/graph_view.dart @@ -26,7 +26,7 @@ class _GraphViewScreenState extends State { return Container(width: 2500, height: 2500); } - return GraphView(graph); + return SafeArea(child: GraphView(graph)); } } @@ -51,6 +51,13 @@ class _GraphViewState extends State { }); } + Offset _getLocationPosition(Offset globalPos) { + RenderBox graphViewRenderBox = context.findRenderObject(); + assert(graphViewRenderBox != null); + + return graphViewRenderBox.globalToLocal(globalPos); + } + @override Widget build(BuildContext context) { var children = []; @@ -62,12 +69,20 @@ class _GraphViewState extends State { child: GestureDetector( child: NodeWidget(node, nodeSize), onPanStart: (details) { - node.x = details.globalPosition.dx; - node.y = details.globalPosition.dy; + var pos = _getLocationPosition(details.globalPosition); + node.x = pos.dx; + node.y = pos.dy; node.pressed = true; + if (node.y <= nodeSize / 2) { + node.y = nodeSize / 2; + } + if (node.x <= nodeSize / 2) { + node.x = nodeSize / 2; + } + widget.graph.notify(); - print("Pan start ${node.label} $details"); + print("Pan start ${node.label} $pos"); }, onPanEnd: (DragEndDetails details) { print("Pan end ${node.label} $details"); @@ -75,11 +90,19 @@ class _GraphViewState extends State { widget.graph.notify(); }, onPanUpdate: (details) { - node.x = details.globalPosition.dx; - node.y = details.globalPosition.dy; + var pos = _getLocationPosition(details.globalPosition); + node.x = pos.dx; + node.y = pos.dy; + + if (node.y <= nodeSize / 2) { + node.y = nodeSize / 2; + } + if (node.x <= nodeSize / 2) { + node.x = nodeSize / 2; + } widget.graph.notify(); - print("Pan update ${node.label} ${details.globalPosition}"); + print("Pan update ${node.label} $pos"); }, ), left: node.x - (nodeSize / 2),