整个课程和数据UI重新设计版本

This commit is contained in:
shenlei
2026-09-18 17:55:44 +09:00
parent febfd30f49
commit 0a4f97d105
154 changed files with 12366 additions and 4784 deletions
@@ -3,6 +3,7 @@ part of '../lesson_flow.dart';
class _IndependentStep extends StatefulWidget {
const _IndependentStep({
required this.state,
required this.lessonId,
required this.segmentId,
required this.keepRecording,
required this.activity,
@@ -16,6 +17,7 @@ class _IndependentStep extends StatefulWidget {
required this.onLater,
});
final AppState state;
final String lessonId;
final LessonActivity activity;
final String segmentId;
final bool keepRecording;
@@ -25,7 +27,8 @@ class _IndependentStep extends StatefulWidget {
final VoidCallback onChanged;
final VoidCallback onNeedHelp;
final VoidCallback onLookup;
final void Function(bool spoken, String? recordingPath) onContinue;
final void Function(bool spoken, String? recordingPath, bool aiCorrected)
onContinue;
final VoidCallback onLater;
@override
@@ -38,6 +41,12 @@ class _IndependentStepState extends State<_IndependentStep>
bool transcriptEdited = false;
String lastTranscript = '';
String? validationError;
bool checkingWithAi = false;
WritingAiFeedback? aiCheck;
String? aiCheckError;
/// A shown AI correction is help, so finishing afterwards is assisted.
bool aiCorrected = false;
@override
AppState get voiceState => widget.state;
@@ -49,16 +58,74 @@ class _IndependentStepState extends State<_IndependentStep>
}
void _submit() {
if (!matchesSegmentIndependent(widget.segmentId, widget.controller.text)) {
setState(() => validationError = '这次还没有用上本段要练的内容。查看帮助后补充一次。');
final shortfall = independentShortfall(
widget.segmentId,
widget.controller.text,
);
if (shortfall != null) {
setState(() => validationError = shortfall);
return;
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
aiCheck?.verdict == 'accepted'
? '通过:用上了本段要练的表达,AI 也没发现拼写或语法问题。'
: '通过:用上了本段要练的表达。',
),
),
);
widget.onContinue(
usedVoice && !transcriptEdited,
widget.keepRecording ? recordingPath : null,
aiCorrected,
);
}
void _clearChecks() {
validationError = null;
aiCheck = null;
aiCheckError = null;
}
Future<void> _checkWithAi() async {
final answer = widget.controller.text.trim();
if (answer.isEmpty || checkingWithAi) return;
if (widget.state.aiProvider == AiProviderType.mock) {
setState(() => aiCheckError = '请先在“我的”配置 AI 服务;本地检查仍可继续学习。');
return;
}
setState(() {
checkingWithAi = true;
aiCheckError = null;
});
final feedback = await AiService.instance.answerFeedback(
provider: widget.state.aiProvider,
endpoint: widget.state.aiEndpoint,
model: widget.state.aiModel,
answerId: widget.segmentId,
target: widget.activity.independentHelp,
taskPrompt: widget.activity.independentPrompt,
answer: answer,
level: lessonLevel(widget.lessonId),
);
// The learner may have kept typing while the request ran.
if (!mounted || widget.controller.text.trim() != answer) {
if (mounted) setState(() => checkingWithAi = false);
return;
}
setState(() {
checkingWithAi = false;
aiCheck = feedback;
aiCheckError = feedback == null
? '暂时无法获得 AI 检查结果。你的答案保留在这里,可稍后重试或直接提交。'
: null;
if (feedback != null && feedback.verdict != 'accepted') {
aiCorrected = true;
}
});
}
Future<void> _toggleMic() async {
if (aiVoiceRecording) {
await finishVoiceInput(
@@ -67,6 +134,7 @@ class _IndependentStepState extends State<_IndependentStep>
usedVoice = true;
lastTranscript = text;
transcriptEdited = false;
_clearChecks();
},
afterTranscribe: widget.onChanged,
);
@@ -87,6 +155,7 @@ class _IndependentStepState extends State<_IndependentStep>
usedVoice = true;
lastTranscript = text;
transcriptEdited = false;
_clearChecks();
});
widget.onChanged();
},
@@ -133,7 +202,7 @@ class _IndependentStepState extends State<_IndependentStep>
controller: widget.controller,
onChanged: (value) {
if (usedVoice && value != lastTranscript) transcriptEdited = true;
setState(() {});
setState(_clearChecks);
widget.onChanged();
},
minLines: 2,
@@ -185,6 +254,48 @@ class _IndependentStepState extends State<_IndependentStep>
),
),
),
OutlinedButton.icon(
onPressed: widget.canContinue && !checkingWithAi
? _checkWithAi
: null,
icon: checkingWithAi
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.spellcheck),
label: Text(checkingWithAi ? '正在检查…' : '提交前 AI 检查语法和拼写(可选)'),
),
if (aiCheck != null)
SectionCard(
tint: aiCheck!.verdict == 'accepted'
? AppColors.softGreen
: AppColors.warm,
child: SpacedColumn(
spacing: 6,
children: [
Text('AI 检查:${aiCheck!.feedback}'),
for (final note in aiCheck!.missing) Text('· $note'),
if (aiCheck!.suggestion != null)
Text('参考改正:${aiCheck!.suggestion}'),
Text(
aiCheck!.verdict == 'accepted'
? 'AI 仅作参考;是否完成仍由本地检查判断。'
: '看过改正后再提交,本次会记为带提示完成。',
style: TextStyle(fontSize: 12, color: AppColors.muted),
),
],
),
),
if (aiCheckError != null)
SectionCard(
tint: AppColors.warm,
child: Text(
aiCheckError!,
style: TextStyle(color: AppColors.warmInk),
),
),
if (validationError != null)
SectionCard(
tint: AppColors.warm,
@@ -210,7 +321,7 @@ class _IndependentStepState extends State<_IndependentStep>
else
TextButton(onPressed: widget.onLookup, child: const Text('查词或短语')),
PrimaryButton(
label: widget.showHelp ? '带帮助完成' : '独立完成',
label: widget.showHelp || aiCorrected ? '带帮助完成' : '独立完成',
onPressed: widget.canContinue ? _submit : null,
),
TextButton(onPressed: widget.onLater, child: const Text('稍后继续')),