拖动另一个窗口小部件时窗口小部件更改

时间:2020-08-29 12:11:12

标签: flutter dart draggable

我有三节课: 顶点,边,图 它们都是小部件。 Graph哈希有两个hasSet,一个是顶点,另一个是Edge。 顶点类包括(一个子代)一个Draggable小部件。 有没有一种方法,当我拖动某个顶点(onDrag)时,边缘将跟随拖动的顶点,一侧保持const,而与拖动的顶点相连的另一侧则发生变化?

Widget vertex = Container(
      height: widget.size,
      width: widget.size,
      child: Container(
        // color: Colors.red,
        decoration: BoxDecoration(
          border: Border.all(
            color: widget.borderColor,
            width: widget.borderWidth,
          ),
          borderRadius: BorderRadius.all(
            Radius.circular(widget.radius),
          ),
          color: widget.backgroundColor,
        ),
        child: Center(
          child: Text(
            widget.text,
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        ),
      ),
    );

边缘:

 @override
  Widget build(BuildContext context) {
    /* inilize Offsets*/
    start = new Offset(widget.v1.posX + widget.v1.size / 2,
        widget.v1.posY + widget.v1.size / 2);
    end = new Offset(widget.v2.posX + widget.v2.size / 2,
        widget.v2.posY + widget.v2.size / 2);
    return CustomPaint(
      size: Size(500, 500),
      painter: LinesPainter(start, end),
    );
  }
}

class LinesPainter extends CustomPainter {
  final Offset start, end;

  LinesPainter(this.start, this.end);

  @override
  void paint(Canvas canvas, Size size) {
    if (start == null || end == null) return;
    canvas.drawLine(
        start,
        end,
        Paint()
          ..strokeWidth = 4
          ..color = GraphDesign().getEdgeColor());
  }

  @override
  bool shouldRepaint(LinesPainter oldDelegate) {
    return oldDelegate.start != start || oldDelegate.end != end;
  }

0 个答案:

没有答案