vyuh_diagram_core 3.2.0
vyuh_diagram_core: ^3.2.0 copied to clipboard
Headless diagram engine for Dart: create shapes and connectors, edit through transactions with undo and redo, and extend with plugins. No Flutter needed.
example/main.dart
import 'package:vyuh_diagram_codec_json/vyuh_diagram_codec_json.dart';
import 'package:vyuh_diagram_core/vyuh_diagram_core.dart';
/// Run on the Dart VM. The same content commands work in the Flutter kit.
void main() {
final controller = DiagramController();
try {
late final String tasks;
controller.edits.batch('Plan the idea', () {
// Every verb returns a decision; creations carry the new identity.
final idea = controller.edits
.createShape(
Shapes.rectangle,
text: 'An idea',
at: const DiagramPoint(120, 120),
style: const ShapeStyle(fill: DiagramColor(0xffd9f99d)),
)
.id!;
tasks = controller.edits
.createText(
at: const DiagramPoint(400, 120),
text: 'Sketch an idea\nBuild it together',
)
.id!;
controller.edits
.setTextAlignment(tasks, ParagraphAlignment.center)
.requireAllowed();
final connection = controller.edits.connect(idea, tasks).id!;
controller.edits.addConnectorLabel(connection, text: 'Next step');
controller.edits
.setText(connection, 'Next step\nReview together')
.requireAllowed();
controller.edits
.setConnectorStyle(connection, strokeStyle: StrokeStyle.dashed)
.requireAllowed();
}).requireAllowed();
controller.edits
.move(const DiagramPoint(0, 40), ids: [tasks])
.requireAllowed();
controller.edits.undo().requireAllowed();
controller.edits.redo().requireAllowed();
// Installed extensions persist the content and text they own.
final saved = controller.save(DiagramJsonCodec.new);
final reopened = DiagramController.open(
saved,
format: DiagramJsonCodec.new,
);
try {
// Send this JSON to your application's file or persistence layer.
// This command-line example writes it to standard output.
// ignore: avoid_print
print(reopened.save(DiagramJsonCodec.new));
} finally {
reopened.dispose();
}
} finally {
controller.dispose();
}
}