2021-11-05 17:43:27 +03:00
|
|
|
import 'package:arrow_path/arrow_path.dart';
|
2021-11-05 00:35:51 +03:00
|
|
|
import 'package:graphs/src/graph.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2021-11-05 01:40:16 +03:00
|
|
|
import 'dart:math';
|
2021-11-05 00:35:51 +03:00
|
|
|
|
|
|
|
class CurvePainter extends CustomPainter {
|
2021-11-05 02:50:05 +03:00
|
|
|
CurvePainter({
|
|
|
|
Key? key,
|
|
|
|
required this.gr,
|
|
|
|
});
|
2021-11-05 01:40:16 +03:00
|
|
|
|
2021-11-05 02:50:05 +03:00
|
|
|
Graphs gr;
|
|
|
|
final double rad = 7;
|
|
|
|
final double width = 4;
|
|
|
|
final Color col = Colors.black;
|
|
|
|
final double aboveHeight = 5;
|
2021-11-05 00:35:51 +03:00
|
|
|
double circleRad = 100;
|
2021-11-05 02:50:05 +03:00
|
|
|
final TextStyle textStyle = const TextStyle(
|
2021-11-05 00:35:51 +03:00
|
|
|
color: Colors.black,
|
2021-11-05 01:40:16 +03:00
|
|
|
fontSize: 21,
|
2021-11-05 00:35:51 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
void drawLine(Canvas canvas, Offset p1, Offset p2) {
|
|
|
|
Paint p = Paint();
|
|
|
|
p.color = col;
|
2021-11-05 17:43:27 +03:00
|
|
|
p.strokeWidth = width - 2;
|
2021-11-05 00:35:51 +03:00
|
|
|
canvas.drawLine(p1, p2, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawDot(Canvas canvas, Offset p1) {
|
|
|
|
var p = Paint();
|
|
|
|
p.color = col;
|
2021-11-05 17:43:27 +03:00
|
|
|
p.strokeWidth = width;
|
2021-11-05 00:35:51 +03:00
|
|
|
canvas.drawCircle(p1, rad, p);
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:43:27 +03:00
|
|
|
void drawSelfConnect(Canvas canvas, Offset p1) {
|
|
|
|
var p = Paint();
|
|
|
|
p.color = col;
|
|
|
|
p.strokeWidth = width - 2;
|
|
|
|
p.style = PaintingStyle.stroke;
|
|
|
|
canvas.drawCircle(Offset(p1.dx + rad + 20, p1.dy), rad + 20, p);
|
|
|
|
}
|
|
|
|
|
2021-11-05 00:35:51 +03:00
|
|
|
TextSpan _getTextSpan(String s) => TextSpan(text: s, style: textStyle);
|
|
|
|
TextPainter _getTextPainter(String s) => TextPainter(
|
|
|
|
text: _getTextSpan(s),
|
|
|
|
textDirection: TextDirection.ltr,
|
|
|
|
textAlign: TextAlign.center);
|
|
|
|
|
|
|
|
void drawAboveText(Canvas canvas, Offset size, String s) {
|
|
|
|
var textPainter = _getTextPainter(s);
|
|
|
|
textPainter.layout();
|
|
|
|
textPainter.paint(
|
|
|
|
canvas,
|
|
|
|
Offset((size.dx - textPainter.width),
|
|
|
|
(size.dy - textPainter.height) - aboveHeight));
|
|
|
|
}
|
|
|
|
|
|
|
|
Graphs getGraph() {
|
|
|
|
List<Dot> d = <Dot>[];
|
|
|
|
d.add(Dot.fromTwoLists("1", [2, 3], [1, 1]));
|
|
|
|
d.add(Dot.fromTwoLists("2", [1, 3], [1, 1]));
|
|
|
|
d.add(Dot.fromTwoLists("3", [1, 2], [1, 1]));
|
|
|
|
d.add(Dot.fromTwoLists("Name1", [], []));
|
|
|
|
d.add(Dot.fromTwoLists("Name2", [], []));
|
|
|
|
return Graphs.fromList("1", d, false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<int, Offset> getDotPos(int dotsAm, Size size) {
|
|
|
|
Map<int, Offset> off = <int, Offset>{};
|
|
|
|
var width = size.width / 2;
|
|
|
|
var height = size.height / 2;
|
|
|
|
for (int i = 0; i < dotsAm; i++) {
|
|
|
|
double x = cos(2 * pi * i / dotsAm) * circleRad + width;
|
|
|
|
double y = sin(2 * pi * i / dotsAm) * circleRad + height;
|
|
|
|
off[i + 1] = Offset(x, y);
|
|
|
|
}
|
2021-11-05 01:40:16 +03:00
|
|
|
//print(off);
|
2021-11-05 00:35:51 +03:00
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:43:27 +03:00
|
|
|
void drawArrow(Canvas canvas, Offset from, Offset to,
|
|
|
|
[bool doubleSided = false]) {
|
|
|
|
Path path;
|
|
|
|
|
|
|
|
// The arrows usually looks better with rounded caps.
|
|
|
|
Paint paint = Paint()
|
|
|
|
..color = Colors.black
|
|
|
|
..style = PaintingStyle.stroke
|
|
|
|
..strokeCap = StrokeCap.round
|
|
|
|
..strokeJoin = StrokeJoin.round
|
|
|
|
..strokeWidth = width - 1;
|
|
|
|
|
|
|
|
/// Draw a single arrow.
|
|
|
|
path = Path();
|
|
|
|
path.moveTo(from.dx, from.dy);
|
|
|
|
path.relativeCubicTo(0, 0, 0, 0, to.dx - from.dx, to.dy - from.dy);
|
|
|
|
path =
|
|
|
|
ArrowPath.make(path: path, isDoubleSided: doubleSided, tipLength: 13);
|
|
|
|
canvas.drawPath(path, paint);
|
|
|
|
}
|
|
|
|
|
2021-11-05 00:35:51 +03:00
|
|
|
void drawConnections(Canvas canvas, List<Dot> dots, Map<int, Offset> off) {
|
|
|
|
for (var i in dots) {
|
|
|
|
var list = i.getL();
|
|
|
|
var beg = off[i.num];
|
|
|
|
for (var d in list.keys) {
|
2021-11-05 17:43:27 +03:00
|
|
|
if (d == i.num) {
|
|
|
|
drawSelfConnect(canvas, off[d]!);
|
|
|
|
} else {
|
|
|
|
drawArrow(canvas, beg!, off[d]!, !gr.getDoubleSidedBool());
|
|
|
|
}
|
2021-11-05 00:35:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void paint(Canvas canvas, Size size) {
|
|
|
|
if (size.width > size.height) {
|
2021-11-05 01:40:16 +03:00
|
|
|
circleRad = size.height / 3;
|
2021-11-05 00:35:51 +03:00
|
|
|
} else {
|
2021-11-05 01:40:16 +03:00
|
|
|
circleRad = size.width / 3;
|
2021-11-05 00:35:51 +03:00
|
|
|
}
|
2021-11-05 17:43:27 +03:00
|
|
|
//var paint = Paint();
|
2021-11-05 00:35:51 +03:00
|
|
|
//drawLine(canvas, Offset(0, size.height / 2),
|
|
|
|
//Offset(size.width, size.height / 2));
|
|
|
|
|
2021-11-05 02:50:05 +03:00
|
|
|
//gr = getGraph();
|
|
|
|
var off = getDotPos(gr.getDotAmount(), size);
|
2021-11-05 00:35:51 +03:00
|
|
|
for (int i = 0; i < off.length; i++) {
|
|
|
|
drawDot(canvas, off[i + 1]!);
|
2021-11-05 02:50:05 +03:00
|
|
|
drawAboveText(
|
|
|
|
canvas, off[i + 1]!, "${gr.getDots()[i].getName()}:[${i + 1}]");
|
2021-11-05 00:35:51 +03:00
|
|
|
}
|
2021-11-05 02:50:05 +03:00
|
|
|
drawConnections(canvas, gr.getDots(), off);
|
2021-11-05 17:43:27 +03:00
|
|
|
//drawArrow(canvas, Offset(size.width / 2, size.height / 2),
|
|
|
|
// Offset(size.width / 2 + 50, size.height / 2 + 200));
|
2021-11-05 00:35:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool shouldRepaint(CustomPainter oldDelegate) {
|
2021-11-05 02:50:05 +03:00
|
|
|
return true;
|
2021-11-05 00:35:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*void _drawTextAt(String txt, Offset position, Canvas canvas) {
|
|
|
|
final textPainter = getTextPainter(txt);
|
|
|
|
textPainter.layout(minWidth: 0, maxWidth: 0);
|
|
|
|
Offset drawPosition =
|
|
|
|
Offset(position.dx, position.dy - (textPainter.height / 2));
|
|
|
|
textPainter.paint(canvas, drawPosition);
|
|
|
|
}*/
|
|
|
|
}
|