/
cloze.dart
213 lines (200 loc) · 6.76 KB
/
cloze.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
import 'package:flutter/material.dart';
import '../../generated/l10n.dart';
import '../tasks/task.dart';
import '../util.dart';
class Cloze extends Task {
static const double fontSize = 30.0;
late List<Segment> _segments;
late int _currentSegmentIndex;
late bool _feedbacking;
int? _chosenOptionIndex;
@override
void init(Map<String, dynamic> data) {
List<Map<String, dynamic>> segmentsData =
data['segments'].cast<Map<String, dynamic>>();
_segments = segmentsData.map(Segment.fromJson).toList();
_currentSegmentIndex = 0;
_feedbacking = false;
logger.log('started segment', {'segment': _currentSegmentIndex});
}
@override
double? getProgress() => !_feedbacking
? _currentSegmentIndex / _segments.length
: (_currentSegmentIndex + 1) / _segments.length;
@override
Widget build(BuildContext context) {
var segment = _segments[_currentSegmentIndex];
var _chosenOptionIndex = this._chosenOptionIndex;
var feedback =
_feedbacking ? _chosenOptionIndex == segment.correctOptionIndex : null;
return Column(
children: [
Expanded(
child: ReadingWidth(
child: Center(
child: Text.rich(
TextSpan(children: [
TextSpan(text: segment.pre),
if (segment.options.isNotEmpty)
WidgetSpan(
alignment: PlaceholderAlignment.baseline,
baseline: TextBaseline.alphabetic,
child: ClozeBlank(
_chosenOptionIndex != null
? segment.options[_chosenOptionIndex]
: null,
feedback:
_chosenOptionIndex == segment.correctOptionIndex
? feedback
: null,
),
),
TextSpan(text: segment.post),
]),
style: const TextStyle(
fontSize: fontSize,
),
textAlign: TextAlign.center,
),
),
),
),
Visibility(
visible: _chosenOptionIndex != null || segment.options.isEmpty,
maintainAnimation: true,
maintainSize: true,
maintainState: true,
child: ElevatedButton.icon(
icon: const Icon(Icons.arrow_forward),
label: Text(S.of(context).taskAdvance),
onPressed: !_feedbacking
? () async {
logger.log('finished segment', {
'segment': _currentSegmentIndex,
'finalResponse': _chosenOptionIndex
});
if (segment.correctOptionIndex != null) {
logger.log('started feedback',
{'segment': _currentSegmentIndex});
setState(() {
_feedbacking = true;
});
await Future.delayed(const Duration(milliseconds: 600));
logger.log('finished feedback',
{'segment': _currentSegmentIndex});
_feedbacking = false;
}
this._chosenOptionIndex = null;
if (_currentSegmentIndex < _segments.length - 1) {
setState(() {
_currentSegmentIndex++;
});
logger.log(
'started segment', {'segment': _currentSegmentIndex});
} else {
finish();
}
}
: null,
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: Row(
children: [
Expanded(
child: Wrap(
alignment: WrapAlignment.center,
children: [
for (var i = 0; i < segment.options.length; i++)
ClozeBlank(
segment.options[i],
onTap: () {
logger.log('chose option',
{'segment': _currentSegmentIndex, 'option': i});
setState(() {
this._chosenOptionIndex = i;
});
},
feedback: i != _chosenOptionIndex &&
i == segment.correctOptionIndex
? feedback
: null,
),
],
),
),
],
),
),
],
);
}
}
class ClozeBlank extends StatelessWidget {
final String? text;
final double fontSize;
final VoidCallback? onTap;
final bool? feedback;
const ClozeBlank(this.text,
{this.fontSize = Cloze.fontSize, this.onTap, this.feedback, Key? key})
: super(key: key);
@override
Widget build(BuildContext context) {
var feedback = this.feedback;
var text = this.text;
return Card(
color: feedback != null
? feedback
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.error
: null,
child: InkWell(
onTap: onTap,
child: Padding(
padding: const EdgeInsets.all(5.0),
child: text != null
? Text(
text,
style: TextStyle(
fontSize: fontSize,
color: feedback != null
? Theme.of(context).colorScheme.onPrimary
: null,
),
)
: ConstrainedBox(
constraints: const BoxConstraints(minWidth: 50.0),
child: Text(
'',
style: TextStyle(fontSize: fontSize),
),
),
),
),
);
}
}
@immutable
class Segment {
final String pre, post;
final List<String> options;
final int? correctOptionIndex;
static final RegExp blankPattern = RegExp(r'\{\{(.+?)\}\}');
const Segment(this.pre, this.post, this.options, [this.correctOptionIndex]);
static Segment fromJson(Map<String, dynamic> json) {
var text = json['text'];
var blankPosition = json['blankPosition'];
if (blankPosition == null) {
return Segment(text, '', const []);
}
var pre = text.substring(0, blankPosition);
var post = text.substring(blankPosition);
return Segment(
pre,
post,
json['options'].cast<String>(),
json['correctOptionIndex'],
);
}
}