315 lines
10 KiB
Dart
315 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../../../auth/data/providers/auth_provider.dart';
|
|
import '../providers/settings_providers.dart';
|
|
|
|
class SettingsPage extends ConsumerStatefulWidget {
|
|
const SettingsPage({super.key});
|
|
|
|
@override
|
|
ConsumerState<SettingsPage> createState() => _SettingsPageState();
|
|
}
|
|
|
|
class _SettingsPageState extends ConsumerState<SettingsPage> {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
Future.microtask(() {
|
|
ref.read(accountProfileProvider.notifier).loadProfile();
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final settings = ref.watch(settingsProvider);
|
|
final profile = ref.watch(accountProfileProvider);
|
|
final theme = Theme.of(context);
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('设置')),
|
|
body: ListView(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
children: [
|
|
// --- Profile Section ---
|
|
_SectionHeader(title: '个人信息'),
|
|
ListTile(
|
|
leading: CircleAvatar(
|
|
backgroundColor: theme.colorScheme.primary,
|
|
child: Text(
|
|
profile.displayName.isNotEmpty
|
|
? profile.displayName[0].toUpperCase()
|
|
: '?',
|
|
style: const TextStyle(
|
|
color: Colors.white, fontWeight: FontWeight.bold),
|
|
),
|
|
),
|
|
title: Text(
|
|
profile.displayName.isNotEmpty ? profile.displayName : '加载中...',
|
|
),
|
|
subtitle: Text(profile.email),
|
|
trailing: IconButton(
|
|
icon: const Icon(Icons.edit),
|
|
onPressed: () => _showEditNameDialog(profile.displayName),
|
|
),
|
|
),
|
|
ListTile(
|
|
leading: const Icon(Icons.lock_outline),
|
|
title: const Text('修改密码'),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: _showChangePasswordSheet,
|
|
),
|
|
const Divider(),
|
|
|
|
// --- Appearance Section ---
|
|
_SectionHeader(title: '外观'),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
|
|
child: SegmentedButton<ThemeMode>(
|
|
segments: const [
|
|
ButtonSegment(
|
|
value: ThemeMode.dark,
|
|
label: Text('深色'),
|
|
icon: Icon(Icons.dark_mode)),
|
|
ButtonSegment(
|
|
value: ThemeMode.light,
|
|
label: Text('浅色'),
|
|
icon: Icon(Icons.light_mode)),
|
|
ButtonSegment(
|
|
value: ThemeMode.system,
|
|
label: Text('跟随系统'),
|
|
icon: Icon(Icons.settings_brightness)),
|
|
],
|
|
selected: {settings.themeMode},
|
|
onSelectionChanged: (modes) {
|
|
ref.read(settingsProvider.notifier).setThemeMode(modes.first);
|
|
},
|
|
),
|
|
),
|
|
const Divider(),
|
|
|
|
// --- Notifications Section ---
|
|
_SectionHeader(title: '通知'),
|
|
SwitchListTile(
|
|
secondary: const Icon(Icons.notifications_outlined),
|
|
title: const Text('推送通知'),
|
|
value: settings.notificationsEnabled,
|
|
onChanged: (v) {
|
|
ref.read(settingsProvider.notifier).setNotificationsEnabled(v);
|
|
},
|
|
),
|
|
SwitchListTile(
|
|
secondary: const Icon(Icons.volume_up_outlined),
|
|
title: const Text('提示音'),
|
|
value: settings.soundEnabled,
|
|
onChanged: settings.notificationsEnabled
|
|
? (v) => ref.read(settingsProvider.notifier).setSoundEnabled(v)
|
|
: null,
|
|
),
|
|
SwitchListTile(
|
|
secondary: const Icon(Icons.vibration),
|
|
title: const Text('震动反馈'),
|
|
value: settings.hapticFeedback,
|
|
onChanged: settings.notificationsEnabled
|
|
? (v) =>
|
|
ref.read(settingsProvider.notifier).setHapticFeedback(v)
|
|
: null,
|
|
),
|
|
const Divider(),
|
|
|
|
// --- About Section ---
|
|
_SectionHeader(title: '关于'),
|
|
const ListTile(
|
|
leading: Icon(Icons.info_outline),
|
|
title: Text('应用版本'),
|
|
subtitle: Text('iAgent v1.0.0'),
|
|
),
|
|
if (settings.selectedTenantName != null)
|
|
ListTile(
|
|
leading: const Icon(Icons.business),
|
|
title: const Text('租户'),
|
|
subtitle: Text(settings.selectedTenantName!),
|
|
),
|
|
const Divider(),
|
|
|
|
// --- Logout ---
|
|
ListTile(
|
|
leading: const Icon(Icons.logout, color: Colors.red),
|
|
title:
|
|
const Text('退出登录', style: TextStyle(color: Colors.red)),
|
|
onTap: () => _confirmLogout(),
|
|
),
|
|
const SizedBox(height: 32),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showEditNameDialog(String currentName) {
|
|
final controller = TextEditingController(text: currentName);
|
|
showDialog(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text('修改显示名称'),
|
|
content: TextField(
|
|
controller: controller,
|
|
autofocus: true,
|
|
decoration: const InputDecoration(
|
|
labelText: '显示名称',
|
|
hintText: '输入新的显示名称',
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(ctx).pop(),
|
|
child: const Text('取消'),
|
|
),
|
|
FilledButton(
|
|
onPressed: () async {
|
|
final name = controller.text.trim();
|
|
if (name.isEmpty) return;
|
|
Navigator.of(ctx).pop();
|
|
final success = await ref
|
|
.read(accountProfileProvider.notifier)
|
|
.updateDisplayName(name);
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(success ? '名称已更新' : '更新失败'),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
child: const Text('保存'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _showChangePasswordSheet() {
|
|
final currentCtrl = TextEditingController();
|
|
final newCtrl = TextEditingController();
|
|
final confirmCtrl = TextEditingController();
|
|
|
|
showModalBottomSheet(
|
|
context: context,
|
|
isScrollControlled: true,
|
|
builder: (ctx) => Padding(
|
|
padding: EdgeInsets.fromLTRB(
|
|
24,
|
|
24,
|
|
24,
|
|
MediaQuery.of(ctx).viewInsets.bottom + 24,
|
|
),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text('修改密码', style: Theme.of(ctx).textTheme.titleLarge),
|
|
const SizedBox(height: 16),
|
|
TextField(
|
|
controller: currentCtrl,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(labelText: '当前密码'),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: newCtrl,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(labelText: '新密码'),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextField(
|
|
controller: confirmCtrl,
|
|
obscureText: true,
|
|
decoration: const InputDecoration(labelText: '确认新密码'),
|
|
),
|
|
const SizedBox(height: 20),
|
|
FilledButton(
|
|
onPressed: () async {
|
|
if (newCtrl.text != confirmCtrl.text) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('两次输入的密码不一致')),
|
|
);
|
|
return;
|
|
}
|
|
if (newCtrl.text.length < 6) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
const SnackBar(content: Text('新密码至少6个字符')),
|
|
);
|
|
return;
|
|
}
|
|
Navigator.of(ctx).pop();
|
|
final result = await ref
|
|
.read(accountProfileProvider.notifier)
|
|
.changePassword(
|
|
currentPassword: currentCtrl.text,
|
|
newPassword: newCtrl.text,
|
|
);
|
|
if (mounted) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text(
|
|
result.success
|
|
? '密码已修改'
|
|
: result.message ?? '修改失败',
|
|
),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
child: const Text('确认修改'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void _confirmLogout() async {
|
|
final confirmed = await showDialog<bool>(
|
|
context: context,
|
|
builder: (ctx) => AlertDialog(
|
|
title: const Text('退出登录'),
|
|
content: const Text('确定要退出登录吗?'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(ctx).pop(false),
|
|
child: const Text('取消'),
|
|
),
|
|
FilledButton(
|
|
onPressed: () => Navigator.of(ctx).pop(true),
|
|
child: const Text('退出'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (confirmed == true) {
|
|
await ref.read(authStateProvider.notifier).logout();
|
|
if (mounted) {
|
|
context.go('/login');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class _SectionHeader extends StatelessWidget {
|
|
final String title;
|
|
const _SectionHeader({required this.title});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 16, 16, 4),
|
|
child: Text(
|
|
title,
|
|
style: Theme.of(context).textTheme.titleSmall?.copyWith(
|
|
color: Theme.of(context).colorScheme.primary,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|