Skip to content

Commit 7189aa2

Browse files
committed
FEAT: 收藏词汇复习新增「正面显示释义」开关
- 设置新增 showWordDefinitionOnFront(默认关闭),随 review.favorites 跨平台同步 - 仅对单词生效:先取基础在线词典(本地缓存 → API),未查到时只读 AI 查词缓存的第一项释义,不发起 AI 请求 - AiDictionarySource 抽取 L1/L2 缓存读取为共用方法,新增只读 readCachedEntry - LocalDictionarySource 增加静态 sourceId - 设置项副标题支持,展示「仅对单词有效」
1 parent e28f53d commit 7189aa2

15 files changed

Lines changed: 590 additions & 36 deletions

‎TASKS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
- [ ] 学习内容字号(只调正文,不缩放整个 UI;颜色设置待产品决定)。
2929
- [x] 云端偏好同步接入字幕文字字号与粗细:本地键 `subtitle_font_size_percent` / `subtitle_font_weight_percent`,云端字段 `appearance.subtitleFontSizePercent` / `appearance.subtitleFontWeightPercent`(后端按域名校验、域内字段自由,无需改后端);云端缺失或非法时保留本地值。**完成时间**: 2026-11-12
30+
- [x] 收藏词汇复习设置新增「正面显示释义」(默认关闭,仅对单词有效,先取基础在线词典(本地缓存 → API),未查到时只读 AI 查词缓存,经偏好同步跨平台)。**完成时间**: 2026-11-13
31+
- 未完成的验证:真机确认正面释义的加载/占位观感,以及两台设备切换后同步生效。
3032
- [ ] 查词归一化 Unicode 化,并迁移已有收藏 key(需产品与数据决策)。
3133
- [ ] 计算并展示每个学习任务的预计/实际耗时。
3234
- [ ] 学习 Tab 点击学习/复习后直接进入学习页面,跳过学习计划页。

‎lib/l10n/app_en.arb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,8 @@
12321232
"favoriteReviewAutoPlayFront": "Auto-play front",
12331233
"favoriteReviewAutoPlayBack": "Auto-play back",
12341234
"favoriteReviewShowVocabularyOnFront": "Show vocabulary on front",
1235+
"favoriteReviewShowWordDefinitionOnFront": "Show definition on front",
1236+
"favoriteReviewShowWordDefinitionOnFrontHint": "Words only",
12351237
"favoriteReviewSentenceSettings": "Saved sentence review",
12361238
"favoriteReviewVocabularySettings": "Saved vocabulary review",
12371239
"bookmarkReviewOrder": "Review order",

‎lib/l10n/app_localizations.dart‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4238,6 +4238,18 @@ abstract class AppLocalizations {
42384238
/// **'Show vocabulary on front'**
42394239
String get favoriteReviewShowVocabularyOnFront;
42404240

4241+
/// No description provided for @favoriteReviewShowWordDefinitionOnFront.
4242+
///
4243+
/// In en, this message translates to:
4244+
/// **'Show definition on front'**
4245+
String get favoriteReviewShowWordDefinitionOnFront;
4246+
4247+
/// No description provided for @favoriteReviewShowWordDefinitionOnFrontHint.
4248+
///
4249+
/// In en, this message translates to:
4250+
/// **'Words only'**
4251+
String get favoriteReviewShowWordDefinitionOnFrontHint;
4252+
42414253
/// No description provided for @favoriteReviewSentenceSettings.
42424254
///
42434255
/// In en, this message translates to:

‎lib/l10n/app_localizations_en.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,13 @@ class AppLocalizationsEn extends AppLocalizations {
24002400
@override
24012401
String get favoriteReviewShowVocabularyOnFront => 'Show vocabulary on front';
24022402

2403+
@override
2404+
String get favoriteReviewShowWordDefinitionOnFront =>
2405+
'Show definition on front';
2406+
2407+
@override
2408+
String get favoriteReviewShowWordDefinitionOnFrontHint => 'Words only';
2409+
24032410
@override
24042411
String get favoriteReviewSentenceSettings => 'Saved sentence review';
24052412

‎lib/l10n/app_localizations_zh.dart‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,12 @@ class AppLocalizationsZh extends AppLocalizations {
22872287
@override
22882288
String get favoriteReviewShowVocabularyOnFront => '正面显示词汇';
22892289

2290+
@override
2291+
String get favoriteReviewShowWordDefinitionOnFront => '正面显示释义';
2292+
2293+
@override
2294+
String get favoriteReviewShowWordDefinitionOnFrontHint => '仅对单词有效';
2295+
22902296
@override
22912297
String get favoriteReviewSentenceSettings => '收藏句子复习';
22922298

‎lib/l10n/app_zh.arb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,8 @@
973973
"favoriteReviewAutoPlayFront": "自动播放正面",
974974
"favoriteReviewAutoPlayBack": "自动播放背面",
975975
"favoriteReviewShowVocabularyOnFront": "正面显示词汇",
976+
"favoriteReviewShowWordDefinitionOnFront": "正面显示释义",
977+
"favoriteReviewShowWordDefinitionOnFrontHint": "仅对单词有效",
976978
"favoriteReviewSentenceSettings": "收藏句子复习",
977979
"favoriteReviewVocabularySettings": "收藏词汇复习",
978980
"bookmarkReviewOrder": "复习顺序",

‎lib/models/favorite_review_settings.dart‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class FavoriteReviewSettings {
1414
this.autoPlayBack = true,
1515
this.autoShowAiLookup = false,
1616
this.showVocabularyOnFront = false,
17+
this.showWordDefinitionOnFront = false,
1718
this.order = FavoriteReviewOrder.smart,
1819
});
1920

@@ -31,6 +32,9 @@ class FavoriteReviewSettings {
3132
/// 收藏词汇复习正面是否显示当前词汇。
3233
final bool showVocabularyOnFront;
3334

35+
/// 收藏单词复习正面是否显示 AI 释义(仅单词生效,词组不受影响)。
36+
final bool showWordDefinitionOnFront;
37+
3438
final FavoriteReviewOrder order;
3539

3640
FavoriteReviewSettings copyWith({
@@ -39,13 +43,16 @@ class FavoriteReviewSettings {
3943
bool? autoPlayBack,
4044
bool? autoShowAiLookup,
4145
bool? showVocabularyOnFront,
46+
bool? showWordDefinitionOnFront,
4247
FavoriteReviewOrder? order,
4348
}) => FavoriteReviewSettings(
4449
showNextReviewTime: showNextReviewTime ?? this.showNextReviewTime,
4550
autoPlayFront: autoPlayFront ?? this.autoPlayFront,
4651
autoPlayBack: autoPlayBack ?? this.autoPlayBack,
4752
autoShowAiLookup: autoShowAiLookup ?? this.autoShowAiLookup,
4853
showVocabularyOnFront: showVocabularyOnFront ?? this.showVocabularyOnFront,
54+
showWordDefinitionOnFront:
55+
showWordDefinitionOnFront ?? this.showWordDefinitionOnFront,
4956
order: order ?? this.order,
5057
);
5158

@@ -55,6 +62,7 @@ class FavoriteReviewSettings {
5562
'autoPlayBack': autoPlayBack,
5663
'autoShowAiLookup': autoShowAiLookup,
5764
'showVocabularyOnFront': showVocabularyOnFront,
65+
'showWordDefinitionOnFront': showWordDefinitionOnFront,
5866
'order': order.name,
5967
};
6068

@@ -80,6 +88,9 @@ class FavoriteReviewSettings {
8088
showVocabularyOnFront: json['showVocabularyOnFront'] is bool
8189
? json['showVocabularyOnFront'] == true
8290
: false,
91+
showWordDefinitionOnFront: json['showWordDefinitionOnFront'] is bool
92+
? json['showWordDefinitionOnFront'] == true
93+
: false,
8394
order: rawOrder is String
8495
? FavoriteReviewOrder.values.firstWhereOrNull(
8596
(item) => item.name == rawOrder,

‎lib/screens/favorite_vocabulary_review_screen.dart‎

Lines changed: 138 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import '../providers/dictionary/lookup_controller.dart';
2222
import '../providers/dictionary/dictionary_registry.dart';
2323
import '../providers/pronunciation/pronunciation_providers.dart';
2424
import '../providers/tts/tts_controller_provider.dart';
25+
import '../models/dictionary/dictionary_entry.dart';
2526
import '../models/dictionary/dictionary_lookup_result.dart';
2627
import '../models/dictionary/dict_speakable_texts.dart';
2728
import '../models/flashcard_item.dart';
@@ -33,6 +34,7 @@ import '../widgets/dictionary/dictionary_panel_host.dart';
3334
import '../services/app_logger.dart';
3435
import '../services/dictionary/ai_dictionary_source.dart';
3536
import '../services/dictionary/dictionary_source.dart';
37+
import '../services/dictionary/local_dictionary_source.dart';
3638
import '../theme/app_theme.dart';
3739
import '../utils/time_format.dart';
3840
import '../utils/wakelock_mixin.dart';
@@ -336,6 +338,14 @@ class _FavoriteVocabularyReviewScreenState
336338
settings.showVocabularyOnFront,
337339
),
338340
),
341+
showWordDefinition:
342+
_isSingleWordCard(card) &&
343+
ref.watch(
344+
favoriteReviewSettingsProvider.select(
345+
(settings) => settings
346+
.showWordDefinitionOnFront,
347+
),
348+
),
339349
playbackState: state.wordPlaybackState,
340350
hasError: state.mediaError != null,
341351
onReplay: () =>
@@ -417,10 +427,16 @@ class _ReviewProgress extends StatelessWidget {
417427
}
418428
}
419429

430+
/// 收藏卡是否为单词(非词组)。词组判定与背面 AI 查词一致:文本含空白即为词组。
431+
bool _isSingleWordCard(FlashcardItem card) =>
432+
card is FlashcardWordItem &&
433+
!card.displayText.trim().contains(RegExp(r'\s'));
434+
420435
class _VocabularyFront extends StatelessWidget {
421436
const _VocabularyFront({
422437
required this.vocabulary,
423438
required this.showVocabulary,
439+
required this.showWordDefinition,
424440
required this.playbackState,
425441
required this.hasError,
426442
required this.onReplay,
@@ -429,6 +445,7 @@ class _VocabularyFront extends StatelessWidget {
429445

430446
final String vocabulary;
431447
final bool showVocabulary;
448+
final bool showWordDefinition;
432449
final FavoriteVocabularyReviewPlaybackState playbackState;
433450
final bool hasError;
434451
final VoidCallback onReplay;
@@ -479,6 +496,9 @@ class _VocabularyFront extends StatelessWidget {
479496
child: SizedBox.expand(
480497
child: _CenteredPrompt(
481498
vocabulary: showVocabulary ? vocabulary : null,
499+
definition: showWordDefinition
500+
? _FrontWordDefinition(word: vocabulary)
501+
: null,
482502
icon: hasError
483503
? Icons.refresh_rounded
484504
: Icons.volume_up_rounded,
@@ -528,15 +548,131 @@ class _VocabularyFront extends StatelessWidget {
528548
}
529549
}
530550

551+
/// 收藏单词正面的释义,按顺序取:基础在线词典(本地缓存 → API),未查到时只读 AI 查词缓存。
552+
/// AI 只读缓存未命中也不发起请求;两者都没有则不占位,释义只是辅助信息,不阻断复习。
553+
class _FrontWordDefinition extends ConsumerWidget {
554+
const _FrontWordDefinition({required this.word});
555+
556+
final String word;
557+
558+
@override
559+
Widget build(BuildContext context, WidgetRef ref) {
560+
final lookup = ref.watch(
561+
dictionaryLookupControllerProvider(
562+
word,
563+
preferredSourceId: LocalDictionarySource.sourceId,
564+
),
565+
);
566+
final current = lookup.current;
567+
if (current is LookupLoaded) {
568+
final translation = _basicTranslation(current.result);
569+
return translation == null
570+
? const SizedBox.shrink()
571+
: _DefinitionText(translation);
572+
}
573+
if (current is LookupNotFound || current is LookupError) {
574+
// 按词区分 State:卡片切换时复用同一 State 会沿用上一张词的缓存结果。
575+
return _AiCachedDefinition(key: ValueKey(word), word: word);
576+
}
577+
return const SizedBox.shrink();
578+
}
579+
}
580+
581+
/// 基础词典的中文释义(每个义项一行,原样展示)。
582+
String? _basicTranslation(DictionaryLookupResult result) {
583+
if (result is! LocalDictResult) return null;
584+
final translation = result.entry.translation?.trim();
585+
return translation == null || translation.isEmpty ? null : translation;
586+
}
587+
588+
/// 读取 AI 查词的本地缓存(只读,绝不请求 AI)并取第一项释义。
589+
class _AiCachedDefinition extends ConsumerStatefulWidget {
590+
const _AiCachedDefinition({super.key, required this.word});
591+
592+
final String word;
593+
594+
@override
595+
ConsumerState<_AiCachedDefinition> createState() =>
596+
_AiCachedDefinitionState();
597+
}
598+
599+
class _AiCachedDefinitionState extends ConsumerState<_AiCachedDefinition> {
600+
late final Future<String?> _definition = _readDefinition();
601+
602+
Future<String?> _readDefinition() async {
603+
final entry = await ref
604+
.read(aiDictionarySourceProvider)
605+
.readCachedEntry(
606+
widget.word,
607+
targetLanguage: ref
608+
.read(dictionaryLookupContextProvider)
609+
.targetLanguage,
610+
);
611+
return _aiFirstMeaningText(entry);
612+
}
613+
614+
@override
615+
Widget build(BuildContext context) {
616+
return FutureBuilder<String?>(
617+
future: _definition,
618+
builder: (context, snapshot) {
619+
final definition = snapshot.data;
620+
return definition == null
621+
? const SizedBox.shrink()
622+
: _DefinitionText(definition);
623+
},
624+
);
625+
}
626+
}
627+
628+
/// AI 单词结果的第一项释义:词性 + 目标语言对译,无对译时回退英文释义。
629+
String? _aiFirstMeaningText(AiDictionaryEntry? entry) {
630+
if (entry is! DictionaryEntry || entry.meanings.isEmpty) return null;
631+
final meaning = entry.meanings.first;
632+
final gloss = meaning.translation.isNotEmpty
633+
? meaning.translation.join(';')
634+
: meaning.definition;
635+
final text = [
636+
meaning.partOfSpeech,
637+
gloss,
638+
].where((part) => part.trim().isNotEmpty).join(' ');
639+
return text.isEmpty ? null : text;
640+
}
641+
642+
/// 正面释义文本,居中展示并与下方内容保持间距。
643+
class _DefinitionText extends StatelessWidget {
644+
const _DefinitionText(this.text);
645+
646+
final String text;
647+
648+
@override
649+
Widget build(BuildContext context) {
650+
return Column(
651+
children: [
652+
Text(
653+
text,
654+
key: const Key('favorite-vocabulary-review-front-definition'),
655+
textAlign: TextAlign.center,
656+
style: Theme.of(context).textTheme.bodyLarge
657+
?.copyWith(color: Theme.of(context).colorScheme.onSurfaceVariant),
658+
),
659+
const SizedBox(height: AppSpacing.m),
660+
],
661+
);
662+
}
663+
}
664+
531665
class _CenteredPrompt extends StatelessWidget {
532666
const _CenteredPrompt({
533667
this.vocabulary,
668+
this.definition,
534669
required this.icon,
535670
required this.title,
536671
required this.body,
537672
required this.accent,
538673
});
539674
final String? vocabulary;
675+
final Widget? definition;
540676
final IconData icon;
541677
final String title;
542678
final String body;
@@ -570,6 +706,7 @@ class _CenteredPrompt extends StatelessWidget {
570706
),
571707
const SizedBox(height: AppSpacing.m),
572708
],
709+
if (definition case final definitionWidget?) definitionWidget,
573710
DecoratedBox(
574711
decoration: BoxDecoration(
575712
color: accent.withValues(alpha: 0.1),
@@ -676,10 +813,6 @@ class _VocabularyBackState extends ConsumerState<_VocabularyBack> {
676813
);
677814
}
678815

679-
bool get _isSingleWord =>
680-
!widget.card.displayText.trim().contains(RegExp(r'\s')) &&
681-
widget.card is FlashcardWordItem;
682-
683816
Future<void> _toggleSourcePlayback() async {
684817
await ref
685818
.read(favoriteVocabularyReviewProvider.notifier)
@@ -820,7 +953,7 @@ class _VocabularyBackState extends ConsumerState<_VocabularyBack> {
820953
),
821954
],
822955
),
823-
if (_isSingleWord) ...[
956+
if (_isSingleWordCard(widget.card)) ...[
824957
const SizedBox(height: AppSpacing.s),
825958
_LocalDictionarySection(word: card.displayText),
826959
],

0 commit comments

Comments
 (0)