setting_page.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import 'package:filesize/filesize.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:flutter_screenutil/flutter_screenutil.dart';
  5. import 'package:go_router/go_router.dart';
  6. import 'package:news_app/constant/size_res.dart';
  7. import 'package:news_app/extension/base.dart';
  8. import 'package:news_app/util/toast_util.dart';
  9. import '../../constant/api_const.dart';
  10. import '../../constant/color_res.dart';
  11. import '../../constant/config.dart';
  12. import '../../event/logout_event.dart';
  13. import '../../http/http_util.dart';
  14. import '../../main.dart';
  15. import '../../model/base_response.dart';
  16. import '../../model/user_model.dart';
  17. import '../../util/cache_util.dart';
  18. import '../../widget/my_txt.dart';
  19. /// @author: bo.zeng
  20. /// @email: cnhbwds@gmail.com
  21. /// @date: 2025 2025/4/9 16:00
  22. /// @description:
  23. class SettingPage extends ConsumerStatefulWidget {
  24. const SettingPage({super.key});
  25. @override
  26. ConsumerState<SettingPage> createState() => _SettingPageState();
  27. }
  28. class _SettingPageState extends ConsumerState<SettingPage> {
  29. final ValueNotifier<int> _cacheSize = ValueNotifier<int>(0);
  30. @override
  31. void initState() {
  32. super.initState();
  33. _loadCacheSize();
  34. }
  35. // 加载缓存大小
  36. Future<void> _loadCacheSize() async {
  37. final size = await CacheUtil.getCacheSize();
  38. _cacheSize.value = size;
  39. }
  40. // 清理缓存
  41. Future<void> _clearCache() async {
  42. try {
  43. await CacheUtil.clearCache();
  44. await _loadCacheSize();
  45. showToast('缓存已清除');
  46. } catch (e) {
  47. showToast('清理失败: $e');
  48. }
  49. }
  50. void logOutUserAction() async {
  51. final result = await HttpUtil().post(apiLogout, data: {});
  52. BaseResponse response = BaseResponse.fromJson(result);
  53. if (response.isSuccess) {
  54. showToast('注销成功');
  55. context.go('/main');
  56. eventBus.fire(LogoutEvent());
  57. } else {
  58. showToast("${response.msg}");
  59. }
  60. }
  61. void logOutUserAlertAction() {
  62. UserModel user = ref.watch(globalUserProvider);
  63. showDialog(
  64. context: context,
  65. builder: (BuildContext context) {
  66. return AlertDialog(
  67. shape: RoundedRectangleBorder(
  68. borderRadius: BorderRadius.circular(20),
  69. ),
  70. title: Text(
  71. '重要提醒',
  72. textAlign: TextAlign.center,
  73. style: TextStyle(
  74. fontSize: 18.sp,
  75. fontWeight: FontWeight.bold,
  76. color: Colors.red,
  77. ),
  78. ),
  79. content: SizedBox(
  80. width: MediaQuery.of(context).size.width * 0.8,
  81. height: 230.h,
  82. child: Column(
  83. crossAxisAlignment: CrossAxisAlignment.start,
  84. children: [
  85. myTxt(
  86. text: '注销前请认真阅读以下重要提醒。账号注销后,您将无法再使用该账号,包括但不限于:',
  87. fontSize: 14.sp,
  88. fontWeight: FontWeight.bold,
  89. textAlign: TextAlign.left,
  90. maxLines: 10,
  91. ),
  92. myTxt(
  93. text:
  94. '1.无法登录、使用新消费传媒账号,并移除该账号下所有登录方式\n2.该账号下的个人资料和历史信息都将无法找回\n3.取消新消费传媒会员身份且无法恢复\n4.账号中所有的资产及权益被清除',
  95. fontSize: 14.sp,
  96. fontWeight: FontWeight.normal,
  97. textAlign: TextAlign.left,
  98. maxLines: 10,
  99. ),
  100. if (user.isShow == 1)
  101. myTxt(
  102. text: '5.无法继续在商城的交易、售后等流程',
  103. fontSize: 14.sp,
  104. fontWeight: FontWeight.normal,
  105. textAlign: TextAlign.left,
  106. maxLines: 10,
  107. ),
  108. ],
  109. ),
  110. ),
  111. actionsAlignment: MainAxisAlignment.center,
  112. actions: [
  113. GestureDetector(
  114. onTap: () {
  115. Navigator.of(context).pop();
  116. logOutUserAction();
  117. },
  118. child: Container(
  119. margin: EdgeInsets.symmetric(horizontal: 28.w),
  120. height: 42.h,
  121. decoration: BoxDecoration(
  122. borderRadius: BorderRadius.circular(20.r),
  123. gradient: LinearGradient(
  124. colors: [color5F59F7, color6592FD],
  125. begin: Alignment.centerLeft,
  126. end: Alignment.centerRight,
  127. ),
  128. ),
  129. alignment: Alignment.center,
  130. child: myTxt(text: "下一步", color: Colors.white, fontSize: 15.sp),
  131. ),
  132. ),
  133. ],
  134. );
  135. },
  136. );
  137. }
  138. @override
  139. Widget build(BuildContext context) {
  140. return Scaffold(
  141. backgroundColor: Colors.grey[200],
  142. appBar: AppBar(
  143. title: myTxt(text: '设置', fontSize: 18.sp, fontWeight: FontWeight.bold),
  144. centerTitle: true,
  145. ),
  146. body: Padding(
  147. padding: EdgeInsets.only(top: 10.h),
  148. child: Column(
  149. spacing: 10.h,
  150. children: [
  151. /* Switch(
  152. value: themeProvider.themeMode == ThemeMode.dark,
  153. onChanged: (value) {
  154. themeProvider.toggleTheme(value);
  155. },
  156. ),*/
  157. Container(
  158. color: Colors.white,
  159. child: Column(
  160. children: [
  161. // ListTile(
  162. // title: myTxt(text: "帐户管理", fontSize: 16.sp),
  163. // trailing: Icon(
  164. // Icons.arrow_forward_ios,
  165. // size: 16.sp,
  166. // color: Colors.grey,
  167. // ),
  168. // ),
  169. // Divider(
  170. // height: 0.1.h,
  171. // color: Colors.grey[200],
  172. // indent: horizontalPadding,
  173. // endIndent: horizontalPadding,
  174. // ),
  175. GestureDetector(
  176. onTap: () {
  177. context.push("/user/changePassword");
  178. },
  179. child: ListTile(
  180. title: myTxt(text: "密码管理", fontSize: 16.sp),
  181. trailing: Icon(
  182. Icons.arrow_forward_ios,
  183. size: 16.sp,
  184. color: Colors.grey,
  185. ),
  186. ),
  187. ),
  188. ],
  189. ),
  190. ),
  191. Container(
  192. color: Colors.white,
  193. child: Column(
  194. children: [
  195. GestureDetector(
  196. onTap: () {
  197. context.push("/user/privacy");
  198. },
  199. child: ListTile(
  200. title: myTxt(text: "隐私政策", fontSize: 16.sp),
  201. trailing: Icon(
  202. Icons.arrow_forward_ios,
  203. size: 16.sp,
  204. color: Colors.grey,
  205. ),
  206. ),
  207. ),
  208. Divider(
  209. height: 0.1.h,
  210. color: Colors.grey[200],
  211. indent: horizontalPadding,
  212. endIndent: horizontalPadding,
  213. ),
  214. ListTile(
  215. title: myTxt(text: "当前版本", fontSize: 16.sp),
  216. trailing: Row(
  217. mainAxisSize: MainAxisSize.min,
  218. children: [
  219. myTxt(text: "V1.0.0", color: Colors.grey),
  220. Icon(
  221. Icons.arrow_forward_ios,
  222. size: 16.sp,
  223. color: Colors.grey,
  224. ),
  225. ],
  226. ),
  227. ),
  228. Divider(
  229. height: 0.1.h,
  230. color: Colors.grey[200],
  231. indent: horizontalPadding,
  232. endIndent: horizontalPadding,
  233. ),
  234. GestureDetector(
  235. onTap: () {
  236. _clearCache();
  237. },
  238. child: ListTile(
  239. title: myTxt(text: "清除缓存", fontSize: 16.sp),
  240. trailing: Row(
  241. mainAxisSize: MainAxisSize.min,
  242. children: [
  243. // myTxt(text: "${filesize(size)}", color: Colors.grey),
  244. ValueListenableBuilder<int>(
  245. valueListenable: _cacheSize,
  246. builder: (context, size, _) {
  247. return myTxt(
  248. text: filesize(size),
  249. color: Colors.grey,
  250. );
  251. },
  252. ),
  253. Icon(
  254. Icons.arrow_forward_ios,
  255. size: 16.sp,
  256. color: Colors.grey,
  257. ),
  258. ],
  259. ),
  260. ),
  261. ),
  262. ],
  263. ),
  264. ),
  265. GestureDetector(
  266. onTap: () {
  267. // logOutUserAction();
  268. logOutUserAlertAction();
  269. },
  270. child: Container(
  271. color: Colors.white,
  272. alignment: Alignment.center,
  273. padding: EdgeInsets.symmetric(vertical: 12.h),
  274. child: myTxt(text: "注销帐户", color: Colors.red),
  275. ),
  276. ),
  277. GestureDetector(
  278. onTap: () {
  279. context.go('/main');
  280. eventBus.fire(LogoutEvent());
  281. },
  282. child: Container(
  283. color: Colors.white,
  284. alignment: Alignment.center,
  285. padding: EdgeInsets.symmetric(vertical: 12.h),
  286. child: myTxt(text: "退出登录", color: Colors.black),
  287. ),
  288. ),
  289. ],
  290. ),
  291. ),
  292. );
  293. }
  294. }