Files
Caption-Trans/lib/ui/widgets/settings_dialog.dart
T

172 lines
5.1 KiB
Dart

import 'package:flutter/material.dart';
import 'package:caption_trans/l10n/app_localizations.dart';
/// Settings dialog for configuring API keys, language, and preferences.
class SettingsDialog extends StatefulWidget {
final void Function(Locale) onLocaleChanged;
final Locale currentLocale;
const SettingsDialog({
super.key,
required this.onLocaleChanged,
required this.currentLocale,
});
@override
State<SettingsDialog> createState() => _SettingsDialogState();
}
class _SettingsDialogState extends State<SettingsDialog> {
late Locale _selectedLocale;
@override
void initState() {
super.initState();
_selectedLocale = widget.currentLocale;
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final l10n = AppLocalizations.of(context)!;
return Dialog(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
backgroundColor: const Color(0xFF1A1A2E),
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 460),
child: Padding(
padding: const EdgeInsets.all(28),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// Title bar
Row(
children: [
Icon(Icons.settings_rounded,
color: theme.colorScheme.primary),
const SizedBox(width: 10),
Text(
l10n.settingsTitle,
style: theme.textTheme.titleLarge?.copyWith(
fontWeight: FontWeight.bold,
),
),
const Spacer(),
IconButton(
icon: const Icon(Icons.close_rounded),
onPressed: () => Navigator.of(context).pop(),
),
],
),
const SizedBox(height: 24),
// Language selection
Text(
l10n.language,
style: theme.textTheme.bodySmall?.copyWith(
color: Colors.white.withValues(alpha: 0.6),
fontWeight: FontWeight.w500,
),
),
const SizedBox(height: 8),
Row(
children: [
_LanguageChip(
label: '中文',
isSelected: _selectedLocale.languageCode == 'zh',
onTap: () => setState(() => _selectedLocale = const Locale('zh')),
),
const SizedBox(width: 8),
_LanguageChip(
label: 'English',
isSelected: _selectedLocale.languageCode == 'en',
onTap: () => setState(() => _selectedLocale = const Locale('en')),
),
],
),
const SizedBox(height: 28),
// Action buttons
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text(l10n.cancel),
),
const SizedBox(width: 8),
ElevatedButton(
onPressed: () {
widget.onLocaleChanged(_selectedLocale);
Navigator.of(context).pop();
},
style: ElevatedButton.styleFrom(
backgroundColor: theme.colorScheme.primary,
foregroundColor: Colors.white,
),
child: Text(l10n.save),
),
],
),
],
),
),
),
);
}
}
class _LanguageChip extends StatelessWidget {
final String label;
final bool isSelected;
final VoidCallback onTap;
const _LanguageChip({
required this.label,
required this.isSelected,
required this.onTap,
});
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(8),
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
decoration: BoxDecoration(
color: isSelected
? theme.colorScheme.primary.withValues(alpha: 0.2)
: Colors.white.withValues(alpha: 0.05),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: isSelected
? theme.colorScheme.primary
: Colors.white.withValues(alpha: 0.1),
),
),
child: Text(
label,
style: TextStyle(
color: isSelected
? theme.colorScheme.primary
: Colors.white.withValues(alpha: 0.7),
fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
fontSize: 13,
),
),
),
);
}
}