mono_flow 0.1.0
mono_flow: ^0.1.0 copied to clipboard
Premium node-based workflow editor for Flutter. Pan/zoom canvas, typed ports, connection rules, undo/redo, auto layout, minimap, inspector, JSON and a run simulator.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:mono_flow/mono_flow.dart';
void main() => runApp(const MonoFlowExampleApp());
/// A complete workflow editor: toolbar, node library, canvas, inspector and
/// a run log — everything below comes from `package:mono_flow`.
class MonoFlowExampleApp extends StatefulWidget {
const MonoFlowExampleApp({super.key});
@override
State<MonoFlowExampleApp> createState() => _MonoFlowExampleAppState();
}
class _MonoFlowExampleAppState extends State<MonoFlowExampleApp> {
ThemeMode _mode = ThemeMode.system;
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'mono_flow',
debugShowCheckedModeBanner: false,
themeMode: _mode,
theme: ThemeData(
brightness: Brightness.light,
useMaterial3: true,
colorSchemeSeed: const Color(0xFF4B3FF5),
),
darkTheme: ThemeData(
brightness: Brightness.dark,
useMaterial3: true,
colorSchemeSeed: const Color(0xFF7D73FF),
),
home: Builder(
builder: (context) {
final dark = Theme.of(context).brightness == Brightness.dark;
return MonoFlowTheme(
data: dark
? const MonoFlowThemeData.dark()
: const MonoFlowThemeData.light(),
child: EditorPage(
onToggleTheme: () => setState(
() => _mode = dark ? ThemeMode.light : ThemeMode.dark,
),
),
);
},
),
);
}
}
class EditorPage extends StatefulWidget {
const EditorPage({super.key, required this.onToggleTheme});
final VoidCallback onToggleTheme;
@override
State<EditorPage> createState() => _EditorPageState();
}
class _EditorPageState extends State<EditorPage> {
late final MonoFlowController controller = MonoFlowController(
registry: MonoStandardNodes.registry,
)..load(MonoStandardNodes.sampleGraph());
late final MonoFlowRunner runner = MonoFlowRunner(
controller: controller,
executor: MonoStandardNodes.simulate,
);
late final TextEditingController _name = TextEditingController(
text: controller.name,
);
final _scaffold = GlobalKey<ScaffoldState>();
bool _logOpen = false;
bool _everSaved = false;
String? _savedJson;
@override
void dispose() {
runner.dispose();
controller.dispose();
_name.dispose();
super.dispose();
}
void _toast(String message) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Text(message),
behavior: SnackBarBehavior.floating,
width: 360,
duration: const Duration(seconds: 2),
),
);
}
void _save() {
_savedJson = jsonEncode(controller.toJson());
controller.markSaved();
setState(() => _everSaved = true);
_toast(
'Workflow saved (${(_savedJson!.length / 1024).toStringAsFixed(1)} KB JSON)',
);
}
Future<void> _run() async {
if (runner.isRunning) {
runner.stop();
return;
}
setState(() => _logOpen = true);
final result = await runner.run();
if (!mounted || result == null) return;
_toast(
result.succeeded
? 'Run succeeded in ${formatMonoDuration(result.duration)}'
: 'Run failed — see the run log',
);
}
Future<void> _exportJson() async {
final json = const JsonEncoder.withIndent(
' ',
).convert(controller.toJson());
await showDialog<void>(
context: context,
builder: (ctx) => _JsonDialog(
title: 'Export JSON',
initial: json,
readOnly: true,
action: 'Copy',
onAction: (text) async {
await Clipboard.setData(ClipboardData(text: text));
if (ctx.mounted) Navigator.pop(ctx);
_toast('JSON copied to clipboard');
},
),
);
}
Future<void> _importJson() async {
await showDialog<void>(
context: context,
builder: (ctx) => _JsonDialog(
title: 'Import JSON',
initial: '',
action: 'Import',
onAction: (text) async {
try {
controller.loadJson(jsonDecode(text) as Map<String, Object?>);
_name.text = controller.name;
if (ctx.mounted) Navigator.pop(ctx);
_toast('Imported “${controller.name}”');
} catch (e) {
_toast('Import failed: $e');
}
},
),
);
}
@override
Widget build(BuildContext context) {
final t = MonoFlowTheme.of(context);
return CallbackShortcuts(
bindings: {
const SingleActivator(LogicalKeyboardKey.keyS, meta: true): _save,
const SingleActivator(LogicalKeyboardKey.keyS, control: true): _save,
const SingleActivator(LogicalKeyboardKey.enter, meta: true): _run,
const SingleActivator(LogicalKeyboardKey.enter, control: true): _run,
},
child: LayoutBuilder(
builder: (context, box) {
final wide = box.maxWidth >= 1180;
final medium = box.maxWidth >= 860;
final palette = Material(
color: t.panel,
child: MonoFlowPalette(
controller: controller,
footer: Container(
padding: const EdgeInsets.fromLTRB(14, 10, 14, 12),
decoration: BoxDecoration(
border: Border(top: BorderSide(color: t.border)),
),
child: Text(
'Drag onto the canvas, or tap to append',
style: t.textStyle(size: 11.5, color: t.textSubtle),
),
),
),
);
final inspector = Material(
color: t.panel,
child: MonoFlowInspector(controller: controller, runner: runner),
);
return Scaffold(
key: _scaffold,
backgroundColor: t.canvas,
drawer: medium
? null
: Drawer(width: 300, child: SafeArea(child: palette)),
endDrawer: wide
? null
: Drawer(width: 340, child: SafeArea(child: inspector)),
body: SafeArea(
child: Column(
children: [
_Toolbar(
controller: controller,
runner: runner,
name: _name,
everSaved: _everSaved,
compact: box.maxWidth < 1100,
onSave: _save,
onRun: _run,
onToggleTheme: widget.onToggleTheme,
onExport: _exportJson,
onImport: _importJson,
onLoadSample: () {
controller.load(MonoStandardNodes.sampleGraph());
_name.text = controller.name;
},
onClear: () {
runner.clear();
controller.clear();
},
onOpenPalette: medium
? null
: () => _scaffold.currentState?.openDrawer(),
onOpenInspector: wide
? null
: () => _scaffold.currentState?.openEndDrawer(),
),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (medium) ...[
SizedBox(width: 268, child: palette),
VerticalDivider(width: 1, color: t.border),
],
Expanded(
child: Column(
children: [
Expanded(
child: MonoFlowCanvas(
controller: controller,
onNodeDoubleTap: (_) {
if (!wide) {
_scaffold.currentState?.openEndDrawer();
}
},
),
),
_RunPanel(
runner: runner,
controller: controller,
open: _logOpen,
onToggle: () =>
setState(() => _logOpen = !_logOpen),
),
],
),
),
if (wide) ...[
VerticalDivider(width: 1, color: t.border),
SizedBox(width: 324, child: inspector),
],
],
),
),
],
),
),
);
},
),
);
}
}
class _Toolbar extends StatelessWidget {
const _Toolbar({
required this.controller,
required this.runner,
required this.name,
required this.everSaved,
required this.compact,
required this.onSave,
required this.onRun,
required this.onToggleTheme,
required this.onExport,
required this.onImport,
required this.onLoadSample,
required this.onClear,
this.onOpenPalette,
this.onOpenInspector,
});
final MonoFlowController controller;
final MonoFlowRunner runner;
final TextEditingController name;
final bool everSaved;
final bool compact;
final VoidCallback onSave;
final VoidCallback onRun;
final VoidCallback onToggleTheme;
final VoidCallback onExport;
final VoidCallback onImport;
final VoidCallback onLoadSample;
final VoidCallback onClear;
final VoidCallback? onOpenPalette;
final VoidCallback? onOpenInspector;
@override
Widget build(BuildContext context) {
final t = MonoFlowTheme.of(context);
final c = controller;
Widget sep() => Container(
width: 1,
height: 20,
margin: const EdgeInsets.symmetric(horizontal: 6),
color: t.border,
);
return Container(
height: 52,
padding: const EdgeInsets.symmetric(horizontal: 10),
decoration: BoxDecoration(
color: t.panel,
border: Border(bottom: BorderSide(color: t.border)),
),
child: ListenableBuilder(
listenable: Listenable.merge([c, c.viewport, runner]),
builder: (context, _) {
final (statusColor, statusText) = runner.isRunning
? (t.accent, 'Running · ${runner.startedCount}/${c.nodes.length}')
: c.hasUnsavedChanges
? (t.warning, 'Unsaved changes')
: everSaved
? (t.success, 'Saved')
: (t.textSubtle, 'Draft');
return Row(
children: [
if (onOpenPalette != null)
MonoIconButton(
icon: Icons.view_sidebar_outlined,
tooltip: 'Node library',
onPressed: onOpenPalette,
),
const _Brand(),
SizedBox(
width: compact ? 150 : 230,
child: TextField(
controller: name,
onChanged: (v) =>
c.name = v.isEmpty ? 'Untitled workflow' : v,
style: t.textStyle(size: 13.5, weight: FontWeight.w600),
cursorColor: t.accent,
decoration: const InputDecoration(
isDense: true,
border: InputBorder.none,
contentPadding: EdgeInsets.symmetric(
horizontal: 8,
vertical: 8,
),
),
),
),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 10,
vertical: 4,
),
decoration: BoxDecoration(
color: t.panelAlt,
borderRadius: BorderRadius.circular(99),
border: Border.all(color: t.border),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 7,
height: 7,
decoration: BoxDecoration(
color: statusColor,
shape: BoxShape.circle,
),
),
if (!compact) ...[
const SizedBox(width: 7),
Text(
statusText,
style: t.textStyle(
size: 11.5,
weight: FontWeight.w500,
color: t.textMuted,
),
),
],
],
),
),
const Spacer(),
MonoIconButton(
icon: Icons.undo_rounded,
tooltip: 'Undo (⌘Z)',
onPressed: c.canUndo && !c.readOnly ? c.undo : null,
),
MonoIconButton(
icon: Icons.redo_rounded,
tooltip: 'Redo (⇧⌘Z)',
onPressed: c.canRedo && !c.readOnly ? c.redo : null,
),
if (!compact) ...[
sep(),
MonoIconButton(
icon: Icons.remove_rounded,
tooltip: 'Zoom out',
onPressed: () => c.zoomBy(0.8),
),
SizedBox(
width: 52,
child: TextButton(
onPressed: () => c.zoomTo(1),
style: TextButton.styleFrom(
foregroundColor: t.textMuted,
padding: EdgeInsets.zero,
),
child: Text(
'${(c.viewport.value.scale * 100).round()}%',
style: t.monoStyle(size: 12, weight: FontWeight.w500),
),
),
),
MonoIconButton(
icon: Icons.add_rounded,
tooltip: 'Zoom in',
onPressed: () => c.zoomBy(1.25),
),
MonoIconButton(
icon: Icons.fit_screen_outlined,
tooltip: 'Fit to view (F)',
onPressed: () => c.fitView(),
),
],
MonoIconButton(
icon: Icons.account_tree_outlined,
tooltip: 'Auto layout (L)',
onPressed: c.readOnly ? null : () => c.autoLayout(),
),
sep(),
PopupMenuButton<VoidCallback>(
tooltip: 'Import / export JSON',
color: t.panel,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: BorderSide(color: t.border),
),
onSelected: (fn) => fn(),
itemBuilder: (_) => [
_menuItem(t, Icons.download_rounded, 'Export JSON', onExport),
_menuItem(t, Icons.upload_rounded, 'Import JSON…', onImport),
const PopupMenuDivider(),
_menuItem(
t,
Icons.description_outlined,
'Load sample workflow',
onLoadSample,
),
_menuItem(
t,
Icons.layers_clear_outlined,
'Clear canvas',
onClear,
danger: true,
),
],
child: SizedBox(
width: 34,
height: 34,
child: Icon(
Icons.data_object_rounded,
size: 17,
color: t.textMuted,
),
),
),
MonoIconButton(
icon: t.isDark
? Icons.light_mode_outlined
: Icons.dark_mode_outlined,
tooltip: t.isDark ? 'Light mode' : 'Dark mode',
onPressed: onToggleTheme,
),
const SizedBox(width: 4),
OutlinedButton.icon(
onPressed: onSave,
icon: const Icon(Icons.save_outlined, size: 16),
label: Text(compact ? '' : 'Save'),
style: OutlinedButton.styleFrom(
foregroundColor: t.text,
side: BorderSide(color: t.border),
minimumSize: const Size(0, 34),
padding: EdgeInsets.symmetric(horizontal: compact ? 8 : 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
const SizedBox(width: 6),
FilledButton.icon(
onPressed: onRun,
icon: Icon(
runner.isRunning
? Icons.stop_rounded
: Icons.play_arrow_rounded,
size: 18,
),
label: Text(runner.isRunning ? 'Stop' : 'Run'),
style: FilledButton.styleFrom(
backgroundColor: runner.isRunning ? t.text : t.accent,
foregroundColor: runner.isRunning ? t.canvas : t.onAccent,
minimumSize: const Size(0, 34),
padding: const EdgeInsets.symmetric(horizontal: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
),
if (onOpenInspector != null) ...[
const SizedBox(width: 4),
MonoIconButton(
icon: Icons.tune_rounded,
tooltip: 'Inspector',
active:
c.selectedNodeIds.isNotEmpty || c.selectedEdgeId != null,
onPressed: onOpenInspector,
),
],
],
);
},
),
);
}
PopupMenuItem<VoidCallback> _menuItem(
MonoFlowThemeData t,
IconData icon,
String label,
VoidCallback fn, {
bool danger = false,
}) => PopupMenuItem(
value: fn,
height: 36,
child: Row(
children: [
Icon(icon, size: 16, color: danger ? t.danger : t.textMuted),
const SizedBox(width: 10),
Text(
label,
style: t.textStyle(size: 12.5, color: danger ? t.danger : t.text),
),
],
),
);
}
class _Brand extends StatelessWidget {
const _Brand();
@override
Widget build(BuildContext context) {
final t = MonoFlowTheme.of(context);
return Container(
padding: const EdgeInsets.only(left: 4, right: 10),
margin: const EdgeInsets.only(right: 4),
decoration: BoxDecoration(
border: Border(right: BorderSide(color: t.border)),
),
child: Row(
children: [
Icon(Icons.hub_outlined, size: 20, color: t.text),
const SizedBox(width: 8),
Text.rich(
TextSpan(
children: [
const TextSpan(text: 'mono'),
TextSpan(
text: '_',
style: TextStyle(color: t.textSubtle),
),
const TextSpan(text: 'flow'),
],
),
style: t.monoStyle(
size: 13,
weight: FontWeight.w600,
color: t.text,
),
),
],
),
);
}
}
class _RunPanel extends StatelessWidget {
const _RunPanel({
required this.runner,
required this.controller,
required this.open,
required this.onToggle,
});
final MonoFlowRunner runner;
final MonoFlowController controller;
final bool open;
final VoidCallback onToggle;
@override
Widget build(BuildContext context) {
final t = MonoFlowTheme.of(context);
return AnimatedContainer(
duration: const Duration(milliseconds: 200),
curve: Curves.easeOut,
height: open ? 230 : 38,
decoration: BoxDecoration(
color: t.panel,
border: Border(top: BorderSide(color: t.border)),
),
child: Column(
children: [
SizedBox(
height: 37,
child: ListenableBuilder(
listenable: runner,
builder: (context, _) {
final last = runner.lastRun;
final summary = runner.isRunning
? 'Running…'
: last == null
? 'No runs yet'
: last.succeeded
? 'Succeeded · ${last.executed} nodes · ${formatMonoDuration(last.duration)}'
: last.stopped
? 'Stopped'
: 'Failed · ${formatMonoDuration(last.duration)}';
return InkWell(
onTap: onToggle,
child: Padding(
padding: const EdgeInsets.only(left: 14, right: 6),
child: Row(
children: [
Icon(
Icons.terminal_rounded,
size: 15,
color: t.textMuted,
),
const SizedBox(width: 8),
Text(
'Run log',
style: t.textStyle(size: 12, weight: FontWeight.w600),
),
const SizedBox(width: 8),
Expanded(
child: Text(
summary,
overflow: TextOverflow.ellipsis,
style: t.textStyle(size: 12, color: t.textSubtle),
),
),
TextButton(
onPressed:
runner.log.isEmpty && !controller.hasRunState
? null
: runner.clear,
style: TextButton.styleFrom(
foregroundColor: t.textMuted,
),
child: const Text('Clear'),
),
AnimatedRotation(
turns: open ? 0 : 0.5,
duration: const Duration(milliseconds: 200),
child: Icon(
Icons.expand_more_rounded,
size: 18,
color: t.textMuted,
),
),
],
),
),
);
},
),
),
if (open)
Expanded(
child: MonoRunLog(
runner: runner,
onEntryTap: (e) {
controller.select([e.nodeId!]);
controller.centerOn(e.nodeId!);
},
),
),
],
),
);
}
}
class _JsonDialog extends StatefulWidget {
const _JsonDialog({
required this.title,
required this.initial,
required this.action,
required this.onAction,
this.readOnly = false,
});
final String title;
final String initial;
final String action;
final bool readOnly;
final Future<void> Function(String text) onAction;
@override
State<_JsonDialog> createState() => _JsonDialogState();
}
class _JsonDialogState extends State<_JsonDialog> {
late final _text = TextEditingController(text: widget.initial);
@override
void dispose() {
_text.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final t = MonoFlowTheme.of(context);
return Dialog(
backgroundColor: t.panel,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
side: BorderSide(color: t.border),
),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 640, maxHeight: 620),
child: Padding(
padding: const EdgeInsets.all(18),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
widget.title,
style: t.textStyle(size: 15, weight: FontWeight.w600),
),
const SizedBox(height: 12),
Expanded(
child: TextField(
controller: _text,
readOnly: widget.readOnly,
maxLines: null,
expands: true,
textAlignVertical: TextAlignVertical.top,
style: t.monoStyle(size: 12, color: t.text, height: 1.5),
decoration: monoInputDecoration(t).copyWith(
hintText: widget.readOnly
? null
: 'Paste a mono_flow workflow JSON here',
),
),
),
const SizedBox(height: 12),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => Navigator.pop(context),
style: TextButton.styleFrom(foregroundColor: t.textMuted),
child: const Text('Cancel'),
),
const SizedBox(width: 8),
FilledButton(
onPressed: () => widget.onAction(_text.text),
style: FilledButton.styleFrom(
backgroundColor: t.accent,
foregroundColor: t.onAccent,
),
child: Text(widget.action),
),
],
),
],
),
),
),
);
}
}