405 lines
12 KiB
Dart
405 lines
12 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import 'package:get_it/get_it.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
||
import 'package:maibu_satabot_v2/core/localization/locale_cubit.dart';
|
||
import 'package:maibu_satabot_v2/core/router/route_paths.dart';
|
||
import 'package:maibu_satabot_v2/core/storage/user_storage.dart';
|
||
import 'package:maibu_satabot_v2/features/auth/presentation/bloc/auth_cubit.dart';
|
||
import 'package:maibu_satabot_v2/features/main_container/presentation/cubit/tab_config_cubit.dart';
|
||
import 'package:maibu_satabot_v2/features/v2/device_list/presentation/float_bar/float_bar_controller.dart';
|
||
|
||
final sl = GetIt.instance;
|
||
|
||
/// 系统设置综合页面
|
||
class SystemSettingsPage extends StatefulWidget {
|
||
const SystemSettingsPage({super.key});
|
||
|
||
@override
|
||
State<SystemSettingsPage> createState() => _SystemSettingsPageState();
|
||
}
|
||
|
||
class _SystemSettingsPageState extends State<SystemSettingsPage> {
|
||
/// 悬浮条开关状态
|
||
bool _floatBarEnabled = true;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
// 初始化时获取当前状态(使用静态属性)
|
||
_floatBarEnabled = FloatBarController.isVisible;
|
||
print('🔍 [设置页面] initState,初始状态: $_floatBarEnabled');
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
backgroundColor: const Color(0xFFF5F6F8),
|
||
appBar: AppBar(
|
||
backgroundColor: Colors.white,
|
||
elevation: 0,
|
||
leading: IconButton(
|
||
icon: const Icon(Icons.arrow_back, color: Color(0xFF1D2129)),
|
||
onPressed: () => Navigator.pop(context),
|
||
),
|
||
title: Text(
|
||
AppLocalizations.of(context).translate('my.settings'),
|
||
style: const TextStyle(
|
||
fontSize: 18,
|
||
fontWeight: FontWeight.bold,
|
||
color: Color(0xFF1D2129),
|
||
),
|
||
),
|
||
centerTitle: true,
|
||
),
|
||
body: ListView(
|
||
padding: const EdgeInsets.all(16),
|
||
children: [
|
||
// Tab 设置
|
||
_buildTabSettingsSection(),
|
||
const SizedBox(height: 12),
|
||
// 悬浮条设置
|
||
_buildFloatBarSection(),
|
||
const SizedBox(height: 12),
|
||
// 语言设置
|
||
_buildLanguageSection(),
|
||
const SizedBox(height: 12),
|
||
// 退出登录
|
||
_buildLogoutButton(context),
|
||
const SizedBox(height: 20),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
/// Tab 设置
|
||
Widget _buildTabSettingsSection() {
|
||
return Container(
|
||
padding: const EdgeInsets.all(16),
|
||
decoration: BoxDecoration(
|
||
color: Colors.white,
|
||
borderRadius: BorderRadius.circular(12),
|
||
boxShadow: const [
|
||
BoxShadow(
|
||
color: Color(0x0D000000),
|
||
blurRadius: 8,
|
||
offset: Offset(0, 2),
|
||
),
|
||
],
|
||
),
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
const Text(
|
||
'Tab 设置',
|
||
style: TextStyle(
|
||
fontSize: 16,
|
||
fontWeight: FontWeight.bold,
|
||
color: Color(0xFF1D2129),
|
||
),
|
||
),
|
||
const SizedBox(height: 16),
|
||
BlocBuilder<TabConfigCubit, dynamic>(
|
||
builder: (context, state) {
|
||
if (state.runtimeType.toString() != 'TabConfigLoaded') {
|
||
return const Center(child: CircularProgressIndicator());
|
||
}
|
||
|
||
final tabs = state.config.items;
|
||
|
||
return ListView.builder(
|
||
shrinkWrap: true,
|
||
physics: const NeverScrollableScrollPhysics(),
|
||
itemCount: tabs.length,
|
||
itemBuilder: (context, index) {
|
||
final tab = tabs[index];
|
||
return Padding(
|
||
padding: const EdgeInsets.only(bottom: 12),
|
||
child: Row(
|
||
children: [
|
||
Expanded(
|
||
child: Text(
|
||
tab.name,
|
||
style: const TextStyle(
|
||
fontSize: 14,
|
||
color: Color(0xFF1D2129),
|
||
),
|
||
),
|
||
),
|
||
Switch(
|
||
value: tab.isEnabled,
|
||
onChanged: (value) {
|
||
context.read<TabConfigCubit>().toggleTab(tab.id);
|
||
},
|
||
activeColor: const Color(0xFF165DFF),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
},
|
||
);
|
||
},
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
/// 语言设置
|
||
Widget _buildLanguageSection() {
|
||
return Container(
|
||
padding: const EdgeInsets.all(16),
|
||
decoration: BoxDecoration(
|
||
color: Colors.white,
|
||
borderRadius: BorderRadius.circular(12),
|
||
boxShadow: const [
|
||
BoxShadow(
|
||
color: Color(0x0D000000),
|
||
blurRadius: 8,
|
||
offset: Offset(0, 2),
|
||
),
|
||
],
|
||
),
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
Text(
|
||
AppLocalizations.of(context).translate('my.language'),
|
||
style: const TextStyle(
|
||
fontSize: 16,
|
||
fontWeight: FontWeight.bold,
|
||
color: Color(0xFF1D2129),
|
||
),
|
||
),
|
||
const SizedBox(height: 16),
|
||
BlocBuilder<LocaleCubit, Locale>(
|
||
builder: (context, locale) {
|
||
return Column(
|
||
children: [
|
||
_buildLanguageOption(
|
||
AppLocalizations.of(context).translate('my.chinese'),
|
||
'zh',
|
||
locale.languageCode == 'zh',
|
||
() => context.read<LocaleCubit>().setLocale(
|
||
const Locale('zh', 'CN'),
|
||
),
|
||
),
|
||
const SizedBox(height: 12),
|
||
_buildLanguageOption(
|
||
AppLocalizations.of(context).translate('my.english'),
|
||
'en',
|
||
locale.languageCode == 'en',
|
||
() => context.read<LocaleCubit>().setLocale(
|
||
const Locale('en', 'US'),
|
||
),
|
||
),
|
||
],
|
||
);
|
||
},
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
Widget _buildLanguageOption(
|
||
String label,
|
||
String code,
|
||
bool isSelected,
|
||
VoidCallback onTap,
|
||
) {
|
||
return GestureDetector(
|
||
onTap: onTap,
|
||
child: Container(
|
||
padding: const EdgeInsets.all(12),
|
||
decoration: BoxDecoration(
|
||
color: isSelected
|
||
? const Color(0xFF165DFF).withOpacity(0.1)
|
||
: Colors.transparent,
|
||
borderRadius: BorderRadius.circular(8),
|
||
border: Border.all(
|
||
color: isSelected
|
||
? const Color(0xFF165DFF)
|
||
: const Color(0xFFE5E6EB),
|
||
width: 1,
|
||
),
|
||
),
|
||
child: Row(
|
||
children: [
|
||
Expanded(
|
||
child: Text(
|
||
label,
|
||
style: TextStyle(
|
||
fontSize: 14,
|
||
color: isSelected
|
||
? const Color(0xFF165DFF)
|
||
: const Color(0xFF1D2129),
|
||
fontWeight: isSelected ? FontWeight.w600 : FontWeight.normal,
|
||
),
|
||
),
|
||
),
|
||
if (isSelected)
|
||
const Icon(
|
||
Icons.check_circle,
|
||
color: Color(0xFF165DFF),
|
||
size: 20,
|
||
),
|
||
],
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
/// 退出登录按钮
|
||
Widget _buildLogoutButton(BuildContext context) {
|
||
return GestureDetector(
|
||
onTap: () {
|
||
_showLogoutDialog(context);
|
||
},
|
||
child: Container(
|
||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||
decoration: BoxDecoration(
|
||
color: Colors.white,
|
||
borderRadius: BorderRadius.circular(12),
|
||
boxShadow: const [
|
||
BoxShadow(
|
||
color: Color(0x0D000000),
|
||
blurRadius: 8,
|
||
offset: Offset(0, 2),
|
||
),
|
||
],
|
||
),
|
||
child: Center(
|
||
child: Text(
|
||
AppLocalizations.of(context).translate('login.logout'),
|
||
style: const TextStyle(
|
||
fontSize: 16,
|
||
fontWeight: FontWeight.w500,
|
||
color: Color(0xFFF53F3F),
|
||
),
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
/// 悬浮条设置
|
||
Widget _buildFloatBarSection() {
|
||
return Container(
|
||
padding: const EdgeInsets.all(16),
|
||
decoration: BoxDecoration(
|
||
color: Colors.white,
|
||
borderRadius: BorderRadius.circular(12),
|
||
boxShadow: const [
|
||
BoxShadow(
|
||
color: Color(0x0D000000),
|
||
blurRadius: 8,
|
||
offset: Offset(0, 2),
|
||
),
|
||
],
|
||
),
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: [
|
||
const Text(
|
||
'悬浮条设置',
|
||
style: TextStyle(
|
||
fontSize: 16,
|
||
fontWeight: FontWeight.bold,
|
||
color: Color(0xFF1D2129),
|
||
),
|
||
),
|
||
const SizedBox(height: 16),
|
||
_buildFloatBarSwitch(),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
/// 悬浮条开关
|
||
Widget _buildFloatBarSwitch() {
|
||
print(
|
||
'🔍 [设置页面] _buildFloatBarSwitch 被调用,_floatBarEnabled: $_floatBarEnabled',
|
||
);
|
||
|
||
return Row(
|
||
children: [
|
||
Expanded(
|
||
child: Column(
|
||
crossAxisAlignment: CrossAxisAlignment.start,
|
||
children: const [
|
||
Text(
|
||
'显示悬浮条',
|
||
style: TextStyle(fontSize: 14, color: Color(0xFF1D2129)),
|
||
),
|
||
SizedBox(height: 4),
|
||
Text(
|
||
'在Tab页面显示设备状态悬浮条',
|
||
style: TextStyle(fontSize: 12, color: Color(0xFF8F959E)),
|
||
),
|
||
],
|
||
),
|
||
),
|
||
Switch(
|
||
value: _floatBarEnabled,
|
||
onChanged: (value) {
|
||
print('🔍 [设置页面] 开关被点击,新值: $value,当前状态: $_floatBarEnabled');
|
||
|
||
// 🔥 立即更新本地状态
|
||
setState(() {
|
||
_floatBarEnabled = value;
|
||
});
|
||
print('🔍 [设置页面] 本地状态已更新为: $_floatBarEnabled');
|
||
|
||
// 🔥 使用静态方法设置新值
|
||
FloatBarController.setVisible(value);
|
||
print('🔍 [设置页面] setVisible 已调用完成');
|
||
|
||
// 显示提示
|
||
if (context.mounted) {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
SnackBar(
|
||
content: Text(value ? '悬浮条已开启' : '悬浮条已关闭'),
|
||
duration: const Duration(seconds: 2),
|
||
),
|
||
);
|
||
}
|
||
},
|
||
activeColor: const Color(0xFF165DFF),
|
||
),
|
||
],
|
||
);
|
||
}
|
||
|
||
/// 显示退出登录确认对话框
|
||
void _showLogoutDialog(BuildContext context) {
|
||
showDialog(
|
||
context: context,
|
||
builder: (context) => AlertDialog(
|
||
title: Text(AppLocalizations.of(context).translate('login.logout')),
|
||
content: const Text('确定要退出登录吗?'),
|
||
actions: [
|
||
TextButton(
|
||
onPressed: () => Navigator.pop(context),
|
||
child: const Text('取消'),
|
||
),
|
||
TextButton(
|
||
onPressed: () async {
|
||
Navigator.pop(context);
|
||
// 执行退出登录逻辑
|
||
await GetIt.I<UserStorage>().deleteUser();
|
||
GetIt.I<AuthCubit>().logout();
|
||
context.go(RoutePaths.login);
|
||
},
|
||
style: TextButton.styleFrom(
|
||
foregroundColor: const Color(0xFFF53F3F),
|
||
),
|
||
child: const Text('确定'),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
}
|