-
Notifications
You must be signed in to change notification settings - Fork 3
/
blockly_editor_widget.dart
131 lines (111 loc) · 3.02 KB
/
blockly_editor_widget.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
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'blockly_editor.dart';
import 'types/types.dart';
/// The Flutter Blockly widget visual programming editor
class BlocklyEditorWidget extends StatefulWidget {
/// ## Example
/// ```dart
/// @override
/// Widget build(BuildContext context) {
/// return Scaffold(
/// body: SafeArea(
/// child: BlocklyEditorWidget(
/// workspaceConfiguration: workspaceConfiguration,
/// initial: initial,
/// onInject: onInject,
/// onChange: onChange,
/// onDispose: onDispose,
/// onError: onError,
/// ),
/// ),
/// );
/// }
/// ```
const BlocklyEditorWidget({
super.key,
this.workspaceConfiguration,
this.initial,
this.onError,
this.onInject,
this.onChange,
this.onDispose,
this.style,
this.script,
this.editor,
this.packages,
});
/// [BlocklyOptions interface](https://developers.google.com/blockly/reference/js/blockly.blocklyoptions_interface)
final BlocklyOptions? workspaceConfiguration;
/// Blockly initial state (xml string or json)
final dynamic initial;
/// It is called on any error
final Function? onError;
/// It is called on inject editor
final Function? onInject;
/// It is called on change editor sate
final Function? onChange;
/// It is called on dispose editor
final Function? onDispose;
/// html render style
final String? style;
/// html render script
final String? script;
/// html render editor
final String? editor;
/// html render packages
final String? packages;
@override
State<BlocklyEditorWidget> createState() => _BlocklyEditorWidgetState();
}
class _BlocklyEditorWidgetState extends State<BlocklyEditorWidget> {
late final BlocklyEditor editor;
@override
void initState() {
super.initState();
/// Create new BlocklyEditor
editor = BlocklyEditor(
workspaceConfiguration: widget.workspaceConfiguration,
initial: widget.initial,
onError: widget.onError,
onInject: widget.onInject,
onChange: widget.onChange,
onDispose: widget.onDispose,
);
/// Configuration the WebViewController
editor.blocklyController
..setNavigationDelegate(
NavigationDelegate(
onPageFinished: (url) {
editor.init();
},
),
)
..setJavaScriptMode(JavaScriptMode.unrestricted)
..addJavaScriptChannel(
'FlutterWebView',
onMessageReceived: editor.onMessage,
);
editor
.htmlRender(
style: widget.style,
script: widget.script,
editor: widget.editor,
packages: widget.packages,
)
.then((htmlString) {
editor.blocklyController.loadHtmlString(htmlString);
});
}
@override
void dispose() {
super.dispose();
editor.dispose();
}
@override
Widget build(BuildContext context) {
return WebViewWidget(
controller: editor.blocklyController,
);
}
}