YSSetting.dart 8.9 KB

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