import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:path_provider/path_provider.dart'; import 'package:ysairplane/base/YSBase.dart'; import 'package:ysairplane/code/YSChangePayPass.dart'; import 'package:ysairplane/code/YSLogin.dart'; import 'package:ysairplane/code/YSSetPayPass.dart'; import 'package:ysairplane/tools/YSNetWorking.dart'; import 'package:ysairplane/tools/YSTools.dart'; import 'package:shared_preferences/shared_preferences.dart'; class YSSetting extends StatefulWidget { @override _YSSettingState createState() => _YSSettingState(); } class _YSSettingState extends State { String cacheSizeStr = '0.0B'; bool _isOpen = false; @override void initState() { Future.delayed(Duration(seconds: 0)).then((value){ loadCache(); }); super.initState(); } @override Widget build(BuildContext context) { return YSBase( ystitle: '系统设置', yschild: Container( height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-44, width: MediaQuery.of(context).size.width, color: Color(0xFFF5F6F8), child: Column( children: [ Container( margin: EdgeInsets.only(top: 10,bottom: 0.5), padding: EdgeInsets.only(left: 15,right: 15), height: 50, color: Colors.white, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('消息通知',style: TextStyle(fontSize: 16,color: Color(0xFF000000)),), GestureDetector( onTap: (){ setState(() { _isOpen = !_isOpen; }); }, child: Container( height: hsp(60), width: hsp(100), child: Image.asset(_isOpen==false?'lib/images/kaiguankai.png':'lib/images/kaiguanguan.png'), ), ) ], ), ), GestureDetector( onTap: (){ showDialog( context:context, builder:(context){ return AlertDialog( title: Text('提示',style: TextStyle(fontSize: 16,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),), content: Text('确认清除缓存?',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),), actions: [ CupertinoButton( padding: EdgeInsets.all(0), child: Text('取消',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),), onPressed: (){ Navigator.pop(context); }, ), CupertinoButton( padding: EdgeInsets.all(0), child: Text('确认',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),), onPressed: (){ Navigator.pop(context); _clearCache(); }, ), ], ); } ); }, child: Container( padding: EdgeInsets.only(left: 15,right: 15), height: 50, color: Colors.white, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text('清理缓存',style: TextStyle(fontSize: 16,color: Color(0xFF000000)),), Row( children: [ Text(cacheSizeStr!=null?cacheSizeStr:'',style: TextStyle(color: Color(0xFF9A9A9A),fontSize: 16),), Icon(Icons.keyboard_arrow_right,size: 20,color: Color(0xFF9A9A9A),) ], ) ], ), ), ), ListView.separated( itemBuilder: (context,index){ return GestureDetector( onTap: (){ Navigator.of(context).push( CupertinoPageRoute( builder: (context){ return index==0?YSSetPayPass():YSChangeOldPayPass(); } ) ); }, child: Container( padding: EdgeInsets.only(left: 15,right: 15), height: 50, color: Colors.white, child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(index==0?'设置支付密码':'修改支付密码',style: TextStyle(fontSize: 16,color: Color(0xFF000000)),), Icon(Icons.keyboard_arrow_right,size: 20,color: Color(0xFF9A9A9A),) ], ), ), ); }, separatorBuilder: (context,index){ return Divider(color: Color(0xFFF5F6F8),height: 0.5,thickness: 0.5,); }, itemCount: 2, padding: EdgeInsets.only(top: hsp(20)), shrinkWrap: true, physics: NeverScrollableScrollPhysics(), ), GestureDetector( child: Container( margin: EdgeInsets.only(top: 35), height: 45, width: MediaQuery.of(context).size.width-40, alignment: Alignment.center, decoration: BoxDecoration( color: Color(0xFF007AFF), borderRadius: BorderRadius.all(Radius.circular(6)) ), child: Text('退出当前账号',style: TextStyle(fontSize: 17,color: Colors.white,fontWeight: FontWeight.bold),), ), onTap: (){ Navigator.of(context).pushAndRemoveUntil( CupertinoPageRoute( builder: (context){ Future _prefer = SharedPreferences.getInstance(); _prefer.then((value){ value.remove('token'); }); return YSLogin(isOut: true,); } ), (route) => false); }, ) ], ), ), ); } void _clearCache() async { Directory tempDir = await getTemporaryDirectory(); //删除缓存目录 await delDir(tempDir); await loadCache(); ysFlutterToast(context, '清除缓存成功'); } Future delDir(FileSystemEntity file) async { if (file is Directory) { final List children = file.listSync(); for (final FileSystemEntity child in children) { var isHave =await child.exists(); //返回真假 if(isHave){ await delDir(child); } } } await file.delete(); } Future loadCache() async { Directory tempDir = await getTemporaryDirectory(); double value = await _getTotalSizeOfFilesInDir(tempDir); /*tempDir.list(followLinks: false,recursive: true).listen((file){ //打印每个缓存文件的路径 print(file.path); });*/ print('临时目录大小: ' + value.toString()); setState(() { cacheSizeStr = _renderSize(value); // _cacheSizeStr用来存储大小的值 }); } Future _getTotalSizeOfFilesInDir(final FileSystemEntity file) async { if (file is File) { int length = await file.length(); return double.parse(length.toString()); } if (file is Directory) { final List children = file.listSync(); double total = 0; if (children != null) for (final FileSystemEntity child in children) total += await _getTotalSizeOfFilesInDir(child); return total; } return 0; } _renderSize(double value) { if (null == value) { return 0; } List unitArr = List() ..add('B') ..add('K') ..add('M') ..add('G'); int index = 0; while (value > 1024) { index++; value = value / 1024; } String size = value.toStringAsFixed(2); return size + unitArr[index]; } }