setting_page.dart 9.7 KB

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