-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.dart
372 lines (338 loc) · 13.3 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:example/data_handlers.dart';
import 'package:example/nodes.dart';
import 'package:file_picker/file_picker.dart';
import 'package:http/http.dart' as http;
import 'package:os_detect/os_detect.dart' as os_detect;
import 'package:fl_nodes/fl_nodes.dart';
import './widgets/hierarchy.dart';
import './widgets/search.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(const NodeEditorExampleApp());
}
class NodeEditorExampleApp extends StatelessWidget {
const NodeEditorExampleApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Node Editor Example',
theme: ThemeData.dark(),
home: const NodeEditorExampleScreen(),
debugShowCheckedModeBanner: kDebugMode,
);
}
}
class NodeEditorExampleScreen extends StatefulWidget {
const NodeEditorExampleScreen({super.key});
@override
State<NodeEditorExampleScreen> createState() =>
NodeEditorExampleScreenState();
}
class NodeEditorExampleScreenState extends State<NodeEditorExampleScreen> {
late final FlNodeEditorController _nodeEditorController;
bool isHierarchyCollapsed = true;
@override
void initState() {
super.initState();
_nodeEditorController = FlNodeEditorController(
projectSaver: (jsonData) async {
if (kIsWeb) return false;
final String? outputPath = await FilePicker.platform.saveFile(
dialogTitle: 'Save Project',
fileName: 'node_project.json',
type: FileType.custom,
allowedExtensions: ['json'],
);
if (outputPath != null) {
final File file = File(outputPath);
await file.writeAsString(jsonEncode(jsonData));
return true;
} else {
return false;
}
},
projectLoader: (isSaved) async {
if (!isSaved) {
final bool? proceed = await showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Unsaved Changes'),
content: const Text(
'You have unsaved changes. Do you want to proceed without saving?',
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: const Text('Proceed'),
),
],
);
},
);
if (proceed != true) return null;
}
final FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['json'],
);
if (result == null) return null;
late final String fileContent;
if (kIsWeb) {
final byteData = result.files.single.bytes!;
fileContent = utf8.decode(byteData.buffer.asUint8List());
} else {
final File file = File(result.files.single.path!);
fileContent = await file.readAsString();
}
return jsonDecode(fileContent);
},
projectCreator: (isSaved) async {
if (isSaved) return true;
final bool? proceed = await showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Unsaved Changes'),
content: const Text(
'You have unsaved changes. Do you want to proceed without saving?',
),
actions: <Widget>[
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: const Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: const Text('Proceed'),
),
],
);
},
);
return proceed == true;
},
);
registerDataHandlers(_nodeEditorController);
registerNodes(context, _nodeEditorController);
const sampleProjectLink =
'https://raw.githubusercontent.com/WilliamKarolDiCioccio/fl_nodes/refs/heads/main/example/assets/www/node_project.json';
() async {
final response = await http.get(Uri.parse(sampleProjectLink));
if (response.statusCode == 200) {
_nodeEditorController.project.load(
data: jsonDecode(response.body),
);
} else {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'Failed to load sample project. Please check your internet connection.',
),
backgroundColor: Colors.red,
),
);
}
}();
SchedulerBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Welcome to FlNodes live example! Keep in mind that this is a work in progress and some features may not work as expected.",
),
backgroundColor: Colors.blue,
),
);
if (os_detect.isBrowser) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Currently our example code depends on https://pub.dev/packages/file_picker so you won't be able to save files on the web",
),
backgroundColor: Colors.orange,
),
);
}
if (os_detect.isAndroid || os_detect.isIOS) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"This example is not optimized for mobile devices. Please use a desktop browser for the best experience.",
),
backgroundColor: Colors.orange,
),
);
}
});
}
@override
void dispose() {
_nodeEditorController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: [
HierarchyWidget(
controller: _nodeEditorController,
isCollapsed: isHierarchyCollapsed,
),
Expanded(
child: FlNodeEditorWidget(
controller: _nodeEditorController,
expandToParent: true,
overlay: () {
return [
FlOverlayData(
child: Padding(
padding: const EdgeInsets.all(8),
child: Row(
spacing: 8,
children: [
IconButton.filled(
tooltip: 'Toggle Hierarchy Panel',
style: IconButton.styleFrom(
backgroundColor: Colors.blue,
),
onPressed: () => setState(() {
isHierarchyCollapsed = !isHierarchyCollapsed;
}),
icon: Icon(
isHierarchyCollapsed
? Icons.keyboard_arrow_right
: Icons.keyboard_arrow_left,
size: 32,
color: Colors.white,
),
),
SearchWidget(controller: _nodeEditorController),
const Spacer(),
IconButton.filled(
tooltip: 'Toggle Snap to Grid',
style: IconButton.styleFrom(
backgroundColor: Colors.blue,
),
onPressed: () => setState(() {
_nodeEditorController.enableSnapToGrid(
!_nodeEditorController
.config.enableSnapToGrid,
);
}),
icon: Icon(
_nodeEditorController.config.enableSnapToGrid
? Icons.grid_on
: Icons.grid_off,
size: 32,
color: Colors.white,
),
),
IconButton.filled(
tooltip: 'Execute Graph',
style: IconButton.styleFrom(
backgroundColor: Colors.blue,
),
onPressed: () =>
_nodeEditorController.runner.executeGraph(),
icon: const Icon(
Icons.play_arrow,
size: 32,
color: Colors.white,
),
),
],
),
),
),
FlOverlayData(
bottom: 0,
left: 0,
child: Opacity(
opacity: 0.5,
child: Padding(
padding: const EdgeInsets.all(8),
child: os_detect.isAndroid || os_detect.isIOS
? const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Touch Commands:',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
Text(' - Tap: Select Node'),
Text(' - Double Tap: Clear Selection'),
Text(' - Long Press: Open Context Menu'),
Text(
' - Drag: Start Linking / Select Nodes',
),
Text(' - Pinch: Zoom In/Out'),
SizedBox(height: 8),
Text(
'Additional Gestures:',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
Text(' - Two-Finger Drag: Pan'),
],
)
: const Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Mouse Commands:',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
Text(' - Left Click: Select Node'),
Text(' - Right Click: Open Context Menu'),
Text(' - Scroll: Zoom In/Out'),
Text(' - Middle Click: Pan'),
SizedBox(height: 8),
Text(
'Keyboard Commands:',
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
Text(' - Ctrl + S: Save Project'),
Text(' - Ctrl + O: Open Project'),
Text(' - Ctrl + N: New Project'),
Text(' - Ctrl + C: Copy Node'),
Text(' - Ctrl + V: Paste Node'),
Text(' - Ctrl + X: Cut Node'),
Text(' - Delete | Backspace: Remove Node'),
Text(' - Ctrl + Z: Undo'),
Text(' - Ctrl + Y: Redo'),
],
),
),
),
),
];
},
),
),
],
),
),
);
}
}