重整多余的UI组件和未定功能的逻辑链路的修正
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||||
import 'package:maibu_satabot_v2/core/localization/locale_cubit.dart';
|
||||
|
||||
/// 可选语言项:tag 对应 assets/languages/{tag}.json,nativeName 用该语言本地写法展示。
|
||||
@@ -22,8 +25,8 @@ class AppLanguages {
|
||||
static final AppLanguages instance = AppLanguages._();
|
||||
|
||||
final List<LanguageItem> items = const [
|
||||
LanguageItem(Locale('zh', 'CN'), '简体中文'),
|
||||
LanguageItem(Locale('en', 'US'), 'English'),
|
||||
LanguageItem(AppLocalizations.chinese, '简体中文'),
|
||||
LanguageItem(AppLocalizations.english, 'English'),
|
||||
];
|
||||
|
||||
String nameOf(Locale locale) {
|
||||
@@ -44,56 +47,154 @@ class LanguageSelectionDialog extends StatelessWidget {
|
||||
final bool mandatory;
|
||||
const LanguageSelectionDialog({super.key, this.mandatory = true});
|
||||
|
||||
Future<void> _onSelect(BuildContext context, Locale locale) async {
|
||||
await context.read<LocaleCubit>().setLocale(locale);
|
||||
if (context.mounted) Navigator.of(context).pop();
|
||||
void _onSelect(BuildContext context, Locale locale) {
|
||||
if (ModalRoute.of(context)?.isCurrent != true) return;
|
||||
unawaited(context.read<LocaleCubit>().setLocale(locale));
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
/// “继续”:不改变当前(默认)语言,仅确认已选并关闭,后续启动不再弹。
|
||||
Future<void> _onContinue(BuildContext context, Locale current) async {
|
||||
await context.read<LocaleCubit>().setLocale(current);
|
||||
if (context.mounted) Navigator.of(context).pop();
|
||||
}
|
||||
static const _navy = Color(0xFF1A2B4A);
|
||||
static const _blue = Color(0xFF1C96F6);
|
||||
static const _gray = Color(0xFF63758A);
|
||||
static const _border = Color(0xFFE5EDF7);
|
||||
static const _softBlue = Color(0xFFEAF3FE);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final current = context.select<LocaleCubit, Locale>(
|
||||
(c) => c.state,
|
||||
final current = context.select<LocaleCubit, Locale>((c) => c.state);
|
||||
final isChinese = current.languageCode == 'zh';
|
||||
final buttonShape = RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
);
|
||||
|
||||
return PopScope(
|
||||
// 强制弹窗时拦截返回键,必须选一个才能继续使用
|
||||
canPop: !mandatory,
|
||||
child: AlertDialog(
|
||||
title: const Text('选择语言 / Select Language'),
|
||||
content: SizedBox(
|
||||
width: 300,
|
||||
child: Dialog(
|
||||
backgroundColor: Colors.white,
|
||||
surfaceTintColor: Colors.transparent,
|
||||
insetPadding: const EdgeInsets.symmetric(horizontal: 24, vertical: 20),
|
||||
constraints: const BoxConstraints(maxWidth: 360),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(24)),
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(20),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
for (final item in AppLanguages.instance.items)
|
||||
RadioListTile<String>(
|
||||
value: item.tag,
|
||||
// 高亮当前语言:按 languageCode 匹配,兼容国家码为空的历史值
|
||||
groupValue: current.countryCode == null ||
|
||||
current.countryCode!.isEmpty
|
||||
? current.languageCode
|
||||
: '${current.languageCode}-${current.countryCode}',
|
||||
title: Text(item.nativeName),
|
||||
onChanged: (_) => _onSelect(context, item.locale),
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 44,
|
||||
height: 44,
|
||||
decoration: BoxDecoration(
|
||||
color: _softBlue,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: const Icon(Icons.language_rounded, color: _blue),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
isChinese ? '选择语言' : 'Select language',
|
||||
style: const TextStyle(
|
||||
color: _navy,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w800,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
isChinese ? 'Select language' : '选择语言',
|
||||
style: const TextStyle(color: _gray, fontSize: 12),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 18),
|
||||
for (final item in AppLanguages.instance.items) ...[
|
||||
_languageOption(context, item, selected: item.locale == current),
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
const SizedBox(height: 8),
|
||||
if (mandatory)
|
||||
FilledButton(
|
||||
style: FilledButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF0B6DCC),
|
||||
foregroundColor: Colors.white,
|
||||
minimumSize: const Size.fromHeight(44),
|
||||
shape: buttonShape,
|
||||
),
|
||||
onPressed: () => _onSelect(context, current),
|
||||
child: Text(isChinese ? '继续 / Continue' : 'Continue / 继续'),
|
||||
)
|
||||
else
|
||||
OutlinedButton(
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: _navy,
|
||||
side: const BorderSide(color: _border),
|
||||
minimumSize: const Size.fromHeight(44),
|
||||
shape: buttonShape,
|
||||
),
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: Text(
|
||||
AppLocalizations.of(context).translate('common.cancel'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
// 强制弹窗:提供“继续”出口,保持当前默认语言并确认后关闭;非强制:取消
|
||||
TextButton(
|
||||
onPressed: () => mandatory
|
||||
? _onContinue(context, current)
|
||||
: Navigator.of(context).pop(),
|
||||
child: Text(mandatory ? '继续 / Continue' : '取消'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _languageOption(
|
||||
BuildContext context,
|
||||
LanguageItem item, {
|
||||
required bool selected,
|
||||
}) {
|
||||
return Semantics(
|
||||
selected: selected,
|
||||
button: true,
|
||||
child: Material(
|
||||
color: selected ? _softBlue : const Color(0xFFF6FAFE),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(color: selected ? _blue : _border),
|
||||
),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: InkWell(
|
||||
onTap: () => _onSelect(context, item.locale),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 14),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
item.nativeName,
|
||||
style: const TextStyle(
|
||||
color: _navy,
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w600,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 10),
|
||||
Icon(
|
||||
selected
|
||||
? Icons.check_circle_rounded
|
||||
: Icons.radio_button_unchecked_rounded,
|
||||
size: 20,
|
||||
color: selected ? _blue : const Color(0xFFABBBCD),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user