Graph Layout: Minor refactor

This commit is contained in:
Vishesh Handa
2020-08-03 01:04:14 +02:00
parent b27f63f829
commit 12ec4c243f

View File

@ -36,17 +36,7 @@ class _MyExampleWidgetState extends State<MyExampleWidget> {
graph.edges = edges; graph.edges = edges;
graph.assignRandomPositions(400, 650); graph.assignRandomPositions(400, 650);
graph.startLayout();
const interval = Duration(milliseconds: 25);
bool shouldStop = false;
var timer = Timer.periodic(interval, (Timer t) {
if (shouldStop) {
return;
}
shouldStop = updateGraphPositions(graph);
});
Timer(const Duration(seconds: 5), () => timer.cancel());
} }
@override @override
@ -230,6 +220,30 @@ class Graph extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
Timer layoutTimer;
void startLayout() {
if (layoutTimer != null) {
return;
}
const interval = Duration(milliseconds: 25);
layoutTimer = Timer.periodic(interval, (Timer t) {
bool shouldStop = updateGraphPositions(this);
if (shouldStop) {
layoutTimer.cancel();
layoutTimer = null;
}
});
Timer(const Duration(seconds: 5), () {
if (layoutTimer != null) {
layoutTimer.cancel();
layoutTimer = null;
}
});
}
} }
void main() => runApp(MyApp()); void main() => runApp(MyApp());