-
Notifications
You must be signed in to change notification settings - Fork 1
/
drawing_board.dart
222 lines (198 loc) · 6.41 KB
/
drawing_board.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
import 'package:flutter/material.dart';
import 'package:signature/signature.dart';
import '../../bloc/theme/theme_bloc.dart';
import '../../services/handwriting.dart';
class DrawingBoard extends StatefulWidget {
final Function(String)? onSuggestionChosen;
final bool onlyOneCharacterSuggestions;
final bool allowKanji;
final bool allowHiragana;
final bool allowKatakana;
final bool allowOther;
const DrawingBoard({
this.onSuggestionChosen,
this.onlyOneCharacterSuggestions = false,
this.allowKanji = true,
this.allowHiragana = false,
this.allowKatakana = false,
this.allowOther = false,
Key? key,
}) : super(key: key);
@override
_DrawingBoardState createState() => _DrawingBoardState();
}
class _DrawingBoardState extends State<DrawingBoard> {
List<String> suggestions = [];
final List<List<TimedPoint>> strokes = [];
final List<List<TimedPoint>> undoQueue = [];
GlobalKey signatureW = GlobalKey();
GlobalKey suggestionBarW = GlobalKey();
static const double fontSize = 30;
static const double suggestionCirclePadding = 13;
late ColorSet panelColor =
BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyLight;
late ColorSet barColor =
BlocProvider.of<ThemeBloc>(context).state.theme.menuGreyNormal;
late final SignatureController controller = SignatureController(
penColor: panelColor.foreground,
onDrawStart: () {
strokes.add([]);
undoQueue.clear();
},
onDrawMove: () => strokes.last
.add(TimedPoint(time: DateTime.now(), point: controller.points.last)),
onDrawEnd: () => updateSuggestions(),
);
Future<void> updateSuggestions() async {
if (strokes.isEmpty) return setState(() => suggestions.clear());
final newSuggestions = await HandwritingRequest(
writingAreaHeight: signatureW.currentContext!.size!.width.toInt(),
writingAreaWidth: signatureW.currentContext!.size!.width.toInt(),
ink: strokes,
).fetch();
setState(() {
suggestions = newSuggestions;
});
}
List<String> get filteredSuggestions {
const kanjiR = r'\p{Script=Hani}';
const hiraganaR = r'\p{Script=Hiragana}';
const katakanaR = r'\p{Script=Katakana}';
const otherR = '[^$kanjiR$hiraganaR$katakanaR]';
final x = widget.allowKanji ? kanjiR : '';
final y = widget.allowHiragana ? hiraganaR : '';
final z = widget.allowKatakana ? katakanaR : '';
late final RegExp combinedRegex;
if ((widget.allowKanji || widget.allowHiragana || widget.allowKatakana) &&
widget.allowOther) {
combinedRegex = RegExp('^(?:[$x$y$z]|$otherR)+\$', unicode: true);
} else if (widget.allowOther) {
combinedRegex = RegExp('^$otherR+\$', unicode: true);
} else {
combinedRegex = RegExp('^[$x$y$z]+\$', unicode: true);
}
return suggestions
.where((s) => combinedRegex.hasMatch(s))
.where((s) => !widget.onlyOneCharacterSuggestions || s.length == 1)
.toList();
}
Widget kanjiChip(String kanji) => InkWell(
onTap: () => widget.onSuggestionChosen?.call(kanji),
child: Container(
height: fontSize + 2 * suggestionCirclePadding,
width: fontSize + 2 * suggestionCirclePadding,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: BlocProvider.of<ThemeBloc>(context)
.state
.theme
.menuGreyLight
.background,
),
child: Center(
child: Text(
kanji,
style: const TextStyle(fontSize: fontSize),
),
),
),
);
Widget suggestionBar() {
const padding = EdgeInsets.symmetric(horizontal: 10, vertical: 5);
return Container(
key: suggestionBarW,
color: barColor.background,
alignment: Alignment.center,
padding: padding,
// TODO: calculate dynamically
constraints: BoxConstraints(
minHeight: 8 +
suggestionCirclePadding * 2 +
fontSize +
(2 * 4) +
padding.vertical,
),
child: Wrap(
spacing: 20,
runSpacing: 5,
children: filteredSuggestions.map((s) => kanjiChip(s)).toList(),
),
);
}
Widget buttonRow() => Container(
color: panelColor.background,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () => setState(() {
controller.clear();
strokes.clear();
suggestions.clear();
}),
icon: const Icon(Icons.delete),
),
IconButton(
onPressed: () {
if (strokes.isNotEmpty) {
undoQueue.add(strokes.removeLast());
controller.undo();
updateSuggestions();
}
},
icon: const Icon(Icons.undo),
),
IconButton(
onPressed: () {
if (undoQueue.isNotEmpty) {
strokes.add(undoQueue.removeLast());
controller.redo();
updateSuggestions();
}
},
icon: const Icon(Icons.redo),
),
if (!widget.onlyOneCharacterSuggestions)
IconButton(
onPressed: () => ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('TODO: implement scrolling page feature!'),
),
),
icon: const Icon(Icons.arrow_forward),
),
],
),
);
Widget drawingPanel() => AspectRatio(
aspectRatio: 1.2,
child: Stack(
alignment: Alignment.bottomRight,
children: [
ClipRect(
child: Signature(
key: signatureW,
controller: controller,
backgroundColor: panelColor.background,
),
),
buttonRow(),
],
),
);
@override
Widget build(BuildContext context) {
return BlocListener<ThemeBloc, ThemeState>(
listener: (context, state) => setState(() {
panelColor = state.theme.menuGreyLight;
barColor = state.theme.menuGreyDark;
}),
child: Column(
children: [
suggestionBar(),
drawingPanel(),
],
),
);
}
}