GraphView: Fix the node width

The text below the nodes was causing it to not be rendered at its center
point as often the text would occupy more space.
This commit is contained in:
Vishesh Handa
2020-09-26 00:08:21 +02:00
parent 156feb824c
commit 445beb850d

View File

@ -40,6 +40,8 @@ class GraphView extends StatefulWidget {
} }
class _GraphViewState extends State<GraphView> { class _GraphViewState extends State<GraphView> {
final nodeSize = 50.0;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -58,7 +60,7 @@ class _GraphViewState extends State<GraphView> {
for (var node in widget.graph.nodes) { for (var node in widget.graph.nodes) {
var w = Positioned( var w = Positioned(
child: GestureDetector( child: GestureDetector(
child: NodeWidget(node), child: NodeWidget(node, nodeSize),
onPanStart: (details) { onPanStart: (details) {
node.x = details.globalPosition.dx; node.x = details.globalPosition.dx;
node.y = details.globalPosition.dy; node.y = details.globalPosition.dy;
@ -80,8 +82,9 @@ class _GraphViewState extends State<GraphView> {
print("Pan update ${node.label} ${details.globalPosition}"); print("Pan update ${node.label} ${details.globalPosition}");
}, },
), ),
left: node.x - 25, left: node.x - (nodeSize / 2),
top: node.y - 25, top: node.y - (nodeSize / 2),
width: nodeSize,
); );
children.add(w); children.add(w);
} }
@ -139,8 +142,9 @@ class GraphEdgePainter extends CustomPainter {
class NodeWidget extends StatelessWidget { class NodeWidget extends StatelessWidget {
final Node node; final Node node;
final double size;
NodeWidget(this.node); NodeWidget(this.node, this.size);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -154,8 +158,8 @@ class NodeWidget extends StatelessWidget {
return Column( return Column(
children: [ children: [
Container( Container(
width: 50, width: size,
height: 50, height: size,
decoration: const BoxDecoration( decoration: const BoxDecoration(
color: Colors.orange, color: Colors.orange,
shape: BoxShape.circle, shape: BoxShape.circle,