setting_page.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:package_info/package_info.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:provider/provider.dart';
  6. import 'package:shared_preferences/shared_preferences.dart';
  7. import 'package:sport/bean/UpdateInfo.dart';
  8. import 'package:sport/provider/bluetooth.dart';
  9. import 'package:sport/provider/user_model.dart';
  10. import 'package:sport/router/navigator_util.dart';
  11. import 'package:sport/router/routes.dart';
  12. import 'package:sport/services/api/inject_api.dart';
  13. import 'package:sport/services/api/resp.dart';
  14. import 'package:sport/utils/toast.dart';
  15. import 'package:sport/widgets/appbar.dart';
  16. import 'package:sport/widgets/button_primary.dart';
  17. import 'package:sport/widgets/dialog/alert_dialog.dart';
  18. import 'package:sport/widgets/dialog/request_dialog.dart';
  19. import 'package:sport/widgets/image.dart';
  20. import 'package:sport/widgets/list_tile.dart';
  21. import 'package:sport/widgets/space.dart';
  22. import 'package:url_launcher/url_launcher.dart';
  23. class SettingPage extends StatefulWidget {
  24. @override
  25. _PageState createState() => _PageState();
  26. }
  27. class _PageState extends State<SettingPage> with InjectApi {
  28. String _cacheSizeStr = "";
  29. String _version = "";
  30. @override
  31. void initState() {
  32. super.initState();
  33. loadCache();
  34. PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
  35. setState(() {
  36. _version = packageInfo.version;
  37. });
  38. });
  39. }
  40. ///加载缓存
  41. Future<Null> loadCache() async {
  42. Directory tempDir = await getTemporaryDirectory();
  43. print(tempDir);
  44. double value = await _getTotalSizeOfFilesInDir(tempDir);
  45. /*tempDir.list(followLinks: false,recursive: true).listen((file){
  46. //打印每个缓存文件的路径
  47. print(file.path);
  48. });*/
  49. print('临时目录大小: ' + value.toString());
  50. setState(() {
  51. _cacheSizeStr = _renderSize(value); // _cacheSizeStr用来存储大小的值
  52. });
  53. return;
  54. }
  55. Future<double> _getTotalSizeOfFilesInDir(final FileSystemEntity file) async {
  56. if (file is File) {
  57. int length = await file.length();
  58. return double.parse(length.toString());
  59. }
  60. if (file is Directory) {
  61. double total = 0;
  62. try {
  63. final List<FileSystemEntity> children = file.listSync();
  64. if (children != null) for (final FileSystemEntity child in children) total += await _getTotalSizeOfFilesInDir(child).catchError((e){});
  65. } catch (e) {
  66. // print(e);
  67. }
  68. return total;
  69. }
  70. return 0;
  71. }
  72. _renderSize(double value) {
  73. if (null == value) {
  74. return 0;
  75. }
  76. List<String> unitArr = List()..add('B')..add('K')..add('M')..add('G');
  77. int index = 0;
  78. while (value > 1024) {
  79. index++;
  80. value = value / 1024;
  81. }
  82. String size = value.toStringAsFixed(2);
  83. return size + unitArr[index];
  84. }
  85. void _clearCache() async {
  86. Directory tempDir = await getTemporaryDirectory();
  87. //删除缓存目录
  88. await delDir(tempDir);
  89. await loadCache();
  90. ToastUtil.show('清除缓存成功');
  91. }
  92. ///递归方式删除目录
  93. Future<Null> delDir(FileSystemEntity file) async {
  94. if (file is Directory) {
  95. try {
  96. final List<FileSystemEntity> children = file.listSync();
  97. for (final FileSystemEntity child in children) {
  98. await delDir(child);
  99. }
  100. } catch (e) {
  101. print(e);
  102. }
  103. }
  104. await file.delete();
  105. }
  106. @override
  107. Widget build(BuildContext context) {
  108. const contentPadding = EdgeInsets.symmetric(horizontal: 0.0);
  109. return Scaffold(
  110. backgroundColor: Colors.white,
  111. body: CustomScrollView(
  112. slivers: <Widget>[
  113. buildSliverAppBar(context, "设置"),
  114. SliverPadding(
  115. padding: const EdgeInsets.symmetric(horizontal: 12),
  116. sliver: SliverToBoxAdapter(
  117. child: ListView(
  118. padding: EdgeInsets.zero,
  119. physics: NeverScrollableScrollPhysics(),
  120. shrinkWrap: true,
  121. children: divideTiles(
  122. context: context,
  123. includeLast: true,
  124. tiles: [
  125. ListTile(
  126. title: Text('消息提醒',style: TextStyle(fontSize: 16.0),),
  127. trailing: arrowRight5(),
  128. contentPadding: contentPadding,
  129. onTap: () => NavigatorUtil.go(context, Routes.messageSetting),
  130. ),
  131. ListTile(
  132. title: Text('帐号管理',style: TextStyle(fontSize: 16.0),),
  133. trailing: arrowRight5(),
  134. contentPadding: contentPadding,
  135. onTap: () => NavigatorUtil.go(context, Routes.account),
  136. ),
  137. ListTile(
  138. title: Row(
  139. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  140. children: <Widget>[
  141. Text('清理缓存',style: TextStyle(fontSize: 16.0),),
  142. Text(
  143. "$_cacheSizeStr",
  144. style: Theme.of(context).textTheme.bodyText2,
  145. )
  146. ],
  147. ),
  148. onTap: () => _clearCache(),
  149. trailing: arrowRight5(),
  150. contentPadding: contentPadding,
  151. ),
  152. ListTile(
  153. title: Text('用户协议',style: TextStyle(fontSize: 16.0),),
  154. trailing: arrowRight5(),
  155. contentPadding: contentPadding,
  156. onTap: () => NavigatorUtil.go(context, Routes.serviceagreement),
  157. ),
  158. ListTile(
  159. title: Text('隐私协议',style: TextStyle(fontSize: 16.0),),
  160. trailing: arrowRight5(),
  161. contentPadding: contentPadding,
  162. onTap: () => NavigatorUtil.go(context, Routes.privacypolicy),
  163. ),
  164. ListTile(
  165. title: Row(
  166. children: <Widget>[
  167. Expanded(
  168. child: Text(Platform.isAndroid ? '检查新版本' : '版本信息',style: TextStyle(fontSize: 16.0),),
  169. ),
  170. Text("当前版本:V$_version")
  171. ],
  172. ),
  173. onTap: () async {
  174. if (Platform.isAndroid) {
  175. request(context, () async {
  176. String version = (await PackageInfo.fromPlatform()).version;
  177. RespData<UpdateInfo> data = await api.checkUpdate(version).catchError((err) {});
  178. if (data.code == 0) {
  179. UpdateInfo info = data.data;
  180. if (!info.needUpdate) {
  181. ToastUtil.show("当前已是最新版本");
  182. return;
  183. }
  184. if (await showDialog(
  185. context: context,
  186. builder: (context) => CustomAlertDialog(
  187. title: '发现新版本 ${info.lastVersion.version}',
  188. child: Container(
  189. width: double.infinity,
  190. padding: const EdgeInsets.symmetric(horizontal: 24.0),
  191. child: Text(
  192. "${info.lastVersion.detail}",
  193. style: TextStyle(fontSize: 14, color: Color(0xff333333), height: 1.4),
  194. )),
  195. textOk: '立即更新',
  196. ok: () => Navigator.of(context).pop(true)),
  197. ) ==
  198. true) {
  199. launch(info.lastVersion.downloadUrl);
  200. }
  201. }else{
  202. ToastUtil.show("当前已是最新版本");
  203. }
  204. });
  205. }
  206. },
  207. trailing: arrowRight5(),
  208. contentPadding: contentPadding,
  209. )
  210. ],
  211. ).toList(),
  212. ),
  213. ),
  214. ),
  215. SliverPadding(
  216. padding: const EdgeInsets.symmetric(horizontal: 12),
  217. sliver: SliverToBoxAdapter(
  218. child: Column(
  219. children: <Widget>[
  220. Space(
  221. height: 24,
  222. ),
  223. PrimaryButton(
  224. content: "退出登录",
  225. callback: () async {
  226. if (await showDialog(
  227. context: context,
  228. builder: (context) => CustomAlertDialog(title: '是否退出登录', ok: () => Navigator.of(context).pop(true)),
  229. ) ==
  230. true) {
  231. SharedPreferences prefs = await SharedPreferences.getInstance();
  232. prefs.clear();
  233. Provider.of<Bluetooth>(context,listen: false).disconnectDevice();
  234. Provider.of<UserModel>(context,listen: false).clear();
  235. NavigatorUtil.goLogin(context);
  236. }
  237. },
  238. ),
  239. ],
  240. ),
  241. ),
  242. )
  243. ],
  244. ));
  245. }
  246. }