129 lines
3.7 KiB
Dart
129 lines
3.7 KiB
Dart
import 'package:graphs/src/graph.dart';
|
|
import 'dart:math';
|
|
import 'package:flutter/material.dart';
|
|
|
|
class CurvePainter extends CustomPainter {
|
|
double rad = 7;
|
|
double width = 4;
|
|
Color col = Colors.black;
|
|
double aboveHeight = 3;
|
|
double circleRad = 100;
|
|
TextStyle textStyle = const TextStyle(
|
|
color: Colors.black,
|
|
fontSize: 25,
|
|
);
|
|
|
|
void drawLine(Canvas canvas, Offset p1, Offset p2) {
|
|
Paint p = Paint();
|
|
p.color = col;
|
|
p.strokeWidth = width;
|
|
canvas.drawLine(p1, p2, p);
|
|
}
|
|
|
|
void drawDot(Canvas canvas, Offset p1) {
|
|
var p = Paint();
|
|
p.color = col;
|
|
p.strokeWidth = width + 1;
|
|
canvas.drawCircle(p1, rad, p);
|
|
}
|
|
|
|
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);
|
|
}
|
|
print(off);
|
|
return off;
|
|
}
|
|
|
|
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) {
|
|
drawLine(canvas, beg!, off[d]!);
|
|
print(d);
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
void paint(Canvas canvas, Size size) {
|
|
if (size.width > size.height) {
|
|
circleRad = size.height / 5;
|
|
} else {
|
|
circleRad = size.width / 5;
|
|
}
|
|
var paint = Paint();
|
|
//drawLine(canvas, Offset(0, size.height / 2),
|
|
//Offset(size.width, size.height / 2));
|
|
|
|
var gr = getGraph();
|
|
var off = getDotPos(gr.getDotAmount(), size);
|
|
for (int i = 0; i < off.length; i++) {
|
|
drawDot(canvas, off[i + 1]!);
|
|
drawAboveText(canvas, off[i + 1]!, gr.getDots()[i].getName());
|
|
}
|
|
drawConnections(canvas, gr.getDots(), off);
|
|
|
|
paint.color = Colors.blue;
|
|
paint.style = PaintingStyle.stroke;
|
|
//drawDot(canvas, Offset(size.width / 2, size.height / 2));
|
|
//drawAboveText(canvas, size, "sssssssssssss");
|
|
//_drawTextAt("Assss", Offset(size.width / 2, size.height / 2), canvas);
|
|
paint.color = Colors.green;
|
|
|
|
var path = Path();
|
|
path.moveTo(size.width / 3, size.height * 3 / 4);
|
|
path.lineTo(size.width / 2, size.height * 5 / 6);
|
|
path.lineTo(size.width * 3 / 4, size.height * 4 / 6);
|
|
path.close();
|
|
|
|
paint.style = PaintingStyle.fill;
|
|
|
|
//canvas.drawPath(path, paint);
|
|
}
|
|
|
|
@override
|
|
bool shouldRepaint(CustomPainter oldDelegate) {
|
|
return true;
|
|
}
|
|
|
|
/*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);
|
|
}*/
|
|
}
|