@@ -22,6 +22,7 @@ import '../providers/dictionary/lookup_controller.dart';
2222import '../providers/dictionary/dictionary_registry.dart' ;
2323import '../providers/pronunciation/pronunciation_providers.dart' ;
2424import '../providers/tts/tts_controller_provider.dart' ;
25+ import '../models/dictionary/dictionary_entry.dart' ;
2526import '../models/dictionary/dictionary_lookup_result.dart' ;
2627import '../models/dictionary/dict_speakable_texts.dart' ;
2728import '../models/flashcard_item.dart' ;
@@ -33,6 +34,7 @@ import '../widgets/dictionary/dictionary_panel_host.dart';
3334import '../services/app_logger.dart' ;
3435import '../services/dictionary/ai_dictionary_source.dart' ;
3536import '../services/dictionary/dictionary_source.dart' ;
37+ import '../services/dictionary/local_dictionary_source.dart' ;
3638import '../theme/app_theme.dart' ;
3739import '../utils/time_format.dart' ;
3840import '../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+
420435class _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+
531665class _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