YSSetting.dart 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:path_provider/path_provider.dart';
  5. import 'package:ysairplane/base/YSBase.dart';
  6. import 'package:ysairplane/code/YSChangePayPass.dart';
  7. import 'package:ysairplane/code/YSLogin.dart';
  8. import 'package:ysairplane/code/YSSetPayPass.dart';
  9. import 'package:ysairplane/tools/YSNetWorking.dart';
  10. import 'package:ysairplane/tools/YSTools.dart';
  11. import 'package:shared_preferences/shared_preferences.dart';
  12. class YSSetting extends StatefulWidget {
  13. @override
  14. _YSSettingState createState() => _YSSettingState();
  15. }
  16. class _YSSettingState extends State<YSSetting> {
  17. String cacheSizeStr = '0.0B';
  18. bool _isOpen = false;
  19. @override
  20. void initState() {
  21. Future.delayed(Duration(seconds: 0)).then((value){
  22. loadCache();
  23. });
  24. super.initState();
  25. }
  26. @override
  27. Widget build(BuildContext context) {
  28. return YSBase(
  29. ystitle: '系统设置',
  30. yschild: Container(
  31. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-44,
  32. width: MediaQuery.of(context).size.width,
  33. color: Color(0xFFF5F6F8),
  34. child: Column(
  35. children: [
  36. Container(
  37. margin: EdgeInsets.only(top: 10,bottom: 0.5),
  38. padding: EdgeInsets.only(left: 15,right: 15),
  39. height: 50,
  40. color: Colors.white,
  41. child: Row(
  42. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  43. children: [
  44. Text('消息通知',style: TextStyle(fontSize: 16,color: Color(0xFF000000)),),
  45. GestureDetector(
  46. onTap: (){
  47. setState(() {
  48. _isOpen = !_isOpen;
  49. });
  50. },
  51. child: Container(
  52. height: hsp(60),
  53. width: hsp(100),
  54. child: Image.asset(_isOpen==false?'lib/images/kaiguankai.png':'lib/images/kaiguanguan.png'),
  55. ),
  56. )
  57. ],
  58. ),
  59. ),
  60. GestureDetector(
  61. onTap: (){
  62. showDialog(
  63. context:context,
  64. builder:(context){
  65. return AlertDialog(
  66. title: Text('提示',style: TextStyle(fontSize: 16,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  67. content: Text('确认清除缓存?',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  68. actions: [
  69. CupertinoButton(
  70. padding: EdgeInsets.all(0),
  71. child: Text('取消',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  72. onPressed: (){
  73. Navigator.pop(context);
  74. },
  75. ),
  76. CupertinoButton(
  77. padding: EdgeInsets.all(0),
  78. child: Text('确认',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  79. onPressed: (){
  80. Navigator.pop(context);
  81. _clearCache();
  82. },
  83. ),
  84. ],
  85. );
  86. }
  87. );
  88. },
  89. child: Container(
  90. padding: EdgeInsets.only(left: 15,right: 15),
  91. height: 50,
  92. color: Colors.white,
  93. child: Row(
  94. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  95. children: [
  96. Text('清理缓存',style: TextStyle(fontSize: 16,color: Color(0xFF000000)),),
  97. Row(
  98. children: [
  99. Text(cacheSizeStr!=null?cacheSizeStr:'',style: TextStyle(color: Color(0xFF9A9A9A),fontSize: 16),),
  100. Icon(Icons.keyboard_arrow_right,size: 20,color: Color(0xFF9A9A9A),)
  101. ],
  102. )
  103. ],
  104. ),
  105. ),
  106. ),
  107. ListView.separated(
  108. itemBuilder: (context,index){
  109. return GestureDetector(
  110. onTap: (){
  111. Navigator.of(context).push(
  112. CupertinoPageRoute(
  113. builder: (context){
  114. return index==0?YSSetPayPass():YSChangeOldPayPass();
  115. }
  116. )
  117. );
  118. },
  119. child: Container(
  120. padding: EdgeInsets.only(left: 15,right: 15),
  121. height: 50,
  122. color: Colors.white,
  123. child: Row(
  124. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  125. children: [
  126. Text(index==0?'设置支付密码':'修改支付密码',style: TextStyle(fontSize: 16,color: Color(0xFF000000)),),
  127. Icon(Icons.keyboard_arrow_right,size: 20,color: Color(0xFF9A9A9A),)
  128. ],
  129. ),
  130. ),
  131. );
  132. },
  133. separatorBuilder: (context,index){
  134. return Divider(color: Color(0xFFF5F6F8),height: 0.5,thickness: 0.5,);
  135. },
  136. itemCount: 2,
  137. padding: EdgeInsets.only(top: hsp(20)),
  138. shrinkWrap: true,
  139. physics: NeverScrollableScrollPhysics(),
  140. ),
  141. GestureDetector(
  142. child: Container(
  143. margin: EdgeInsets.only(top: 35),
  144. height: 45,
  145. width: MediaQuery.of(context).size.width-40,
  146. alignment: Alignment.center,
  147. decoration: BoxDecoration(
  148. color: Color(0xFF007AFF),
  149. borderRadius: BorderRadius.all(Radius.circular(6))
  150. ),
  151. child: Text('退出当前账号',style: TextStyle(fontSize: 17,color: Colors.white,fontWeight: FontWeight.bold),),
  152. ),
  153. onTap: (){
  154. Navigator.of(context).pushAndRemoveUntil(
  155. CupertinoPageRoute(
  156. builder: (context){
  157. Future<SharedPreferences> _prefer = SharedPreferences.getInstance();
  158. _prefer.then((value){
  159. value.remove('token');
  160. });
  161. return YSLogin(isOut: true,);
  162. }
  163. ), (route) => false);
  164. },
  165. )
  166. ],
  167. ),
  168. ),
  169. );
  170. }
  171. void _clearCache() async {
  172. Directory tempDir = await getTemporaryDirectory();
  173. //删除缓存目录
  174. await delDir(tempDir);
  175. await loadCache();
  176. ysFlutterToast(context, '清除缓存成功');
  177. }
  178. Future<Null> delDir(FileSystemEntity file) async {
  179. if (file is Directory) {
  180. final List<FileSystemEntity> children = file.listSync();
  181. for (final FileSystemEntity child in children) {
  182. var isHave =await child.exists(); //返回真假
  183. if(isHave){
  184. await delDir(child);
  185. }
  186. }
  187. }
  188. await file.delete();
  189. }
  190. Future<Null> loadCache() async {
  191. Directory tempDir = await getTemporaryDirectory();
  192. double value = await _getTotalSizeOfFilesInDir(tempDir);
  193. /*tempDir.list(followLinks: false,recursive: true).listen((file){
  194. //打印每个缓存文件的路径
  195. print(file.path);
  196. });*/
  197. print('临时目录大小: ' + value.toString());
  198. setState(() {
  199. cacheSizeStr = _renderSize(value); // _cacheSizeStr用来存储大小的值
  200. });
  201. }
  202. Future<double> _getTotalSizeOfFilesInDir(final FileSystemEntity file) async {
  203. if (file is File) {
  204. int length = await file.length();
  205. return double.parse(length.toString());
  206. }
  207. if (file is Directory) {
  208. final List<FileSystemEntity> children = file.listSync();
  209. double total = 0;
  210. if (children != null)
  211. for (final FileSystemEntity child in children)
  212. total += await _getTotalSizeOfFilesInDir(child);
  213. return total;
  214. }
  215. return 0;
  216. }
  217. _renderSize(double value) {
  218. if (null == value) {
  219. return 0;
  220. }
  221. List<String> unitArr = List()
  222. ..add('B')
  223. ..add('K')
  224. ..add('M')
  225. ..add('G');
  226. int index = 0;
  227. while (value > 1024) {
  228. index++;
  229. value = value / 1024;
  230. }
  231. String size = value.toStringAsFixed(2);
  232. return size + unitArr[index];
  233. }
  234. }