elysia/lib/page/SettingPage.dart
2025-11-04 09:53:47 +08:00

226 lines
6.6 KiB
Dart

import 'dart:developer';
import 'dart:io';
import 'package:elysia/page/child/UpdateCheckPage.dart';
import 'package:elysia/plugin/CacheTool.dart';
import 'package:elysia/plugin/Notify.dart';
import 'package:elysia/plugin/Remind.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import '../plugin/C.dart';
import '../plugin/HTTP.dart';
import '../plugin/HiverCache.dart';
import '../plugin/RouteAnimation.dart';
import 'LoginPage.dart';
import 'package:restart_app/restart_app.dart';
class SettingPage extends StatefulWidget {
const SettingPage({super.key});
@override
State<SettingPage> createState() => _SettingPageState();
}
class _SettingPageState extends State<SettingPage> {
String _cacheSize = '';
String _serverVersion = '';
@override
void initState() {
super.initState();
fetchVersion();
getCacheSize();
}
Future<void> fetchVersion() async {
try {
final result = await HTTP
.create("${C.BASE_URL}/server/version")
.setHeader(C.TOKEN)
.setRequestType(RequestType.GET)
.execute();
if (result['code'] == 200) {
setState(() {
_serverVersion = result['data'].toString();
});
}
} catch (e) {}
}
Future<void> getCacheSize() async {
int sizeInt = await CacheTool.getCacheSize();
print(sizeInt);
String size = CacheTool.formatBytes(sizeInt);
setState(() {
_cacheSize = size;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("设置", style: TextStyle(color: Colors.black)),
centerTitle: true,
backgroundColor: Colors.grey[100],
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Colors.black),
onPressed: () => Navigator.pop(context),
),
elevation: 0,
),
body: ScrollConfiguration(
behavior: const _BouncingScrollBehavior(),
child: Column(
children: [
Expanded(
child: ListView(
children: [
const SizedBox(height: 20),
_buildGroupTitle("隐私"),
_buildSettingItem(context, title: "个人资料", onTap: () {}),
_buildGroupTitle("系统"),
_buildSettingItem(
context,
title: "版本更新",
onTap: () {
Navigator.push(
context,
RouteAnimation(UpdateCheckPage(), Offset(1, 0)),
);
},
),
_buildSettingItem(
context,
title: "服务器版本",
rightText: _serverVersion.isEmpty
? "获取中..."
: "$_serverVersion",
onTap: () {},
),
_buildSettingItem(
context,
title: "APP缓存",
rightText: _cacheSize.isEmpty ? "获取中..." : "$_cacheSize",
onTap: () {
Remind.show(
context,
"提醒!",
"是否清空缓存",
onTap: (status) async {
if (status) {
await CacheTool.clearCache();
getCacheSize();
}
},
);
},
),
const SizedBox(height: 50),
],
),
),
SizedBox(
width: 350,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
backgroundColor: Colors.redAccent,
foregroundColor: Colors.red,
elevation: 0,
shadowColor: Colors.transparent,
),
onPressed: () async {
Remind.show(
context,
"退出登录",
"确认退出吗?",
onTap: (s) async {
if (!s) return;
final appDocDir =
await getApplicationDocumentsDirectory();
final configDir = Directory('${appDocDir.path}/config');
final file = File('${configDir.path}/auth.token');
await file.delete();
C.TOKEN = {};
C.PROFILE = {};
C.socket.close();
Restart.restartApp();
},
);
},
child: Text(
"退出登录",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w600,
fontSize: 16,
),
),
),
),
SizedBox(height: 50),
],
),
),
);
}
Widget _buildGroupTitle(String title) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Text(
title,
style: const TextStyle(
fontSize: 14,
color: Colors.grey,
fontWeight: FontWeight.bold,
),
),
);
}
Widget _buildSettingItem(
BuildContext context, {
required String title,
String? rightText,
VoidCallback? onTap,
}) {
return ListTile(
title: Text(title),
trailing: rightText != null
? Text(
rightText,
style: const TextStyle(color: Colors.grey, fontSize: 14),
)
: const Icon(Icons.arrow_forward_ios, size: 16),
onTap: onTap,
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
);
}
}
/// 自定义回弹滚动效果
class _BouncingScrollBehavior extends ScrollBehavior {
const _BouncingScrollBehavior();
@override
Widget buildViewportChrome(
BuildContext context,
Widget child,
AxisDirection axisDirection,
) {
return child;
}
@override
ScrollPhysics getScrollPhysics(BuildContext context) {
return const BouncingScrollPhysics();
}
}