YSSetting.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutterappfuyou/code/base/YSNetWorking.dart';
  5. import 'package:flutterappfuyou/code/base/YSTools.dart';
  6. import 'package:flutterappfuyou/code/version2/YSChangePassWord.dart';
  7. import 'package:flutterappfuyou/code/version2/YSUrlPage.dart';
  8. import 'package:package_info/package_info.dart';
  9. import 'package:path_provider/path_provider.dart';
  10. import 'package:shared_preferences/shared_preferences.dart';
  11. import 'YSBindInfo.dart';
  12. import 'YSlogin.dart';
  13. import 'base/YSBase.dart';
  14. class YSSetting extends StatefulWidget {
  15. @override
  16. _YSSettingState createState() => _YSSettingState();
  17. }
  18. class _YSSettingState extends State<YSSetting> {
  19. String webStr,healthName;
  20. int healthId;
  21. List _showArray = [
  22. {'title':'账号绑定'},
  23. {'title':'推送设置','value':false},
  24. {'title':'服务条款'},
  25. {'title':'隐私政策'},
  26. {'title':'密码设置'},
  27. {'title':'清理缓存','value':'0.0B'},
  28. {'title':'版本号'},
  29. {'title':'账号注销'},
  30. {'title':'退出登录'}
  31. ];
  32. @override
  33. void initState() {
  34. Future.delayed(Duration(seconds: 0)).then((value){
  35. _getUserData();
  36. loadCache();
  37. });
  38. super.initState();
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. return YSBase(
  43. ystitle: '设置',
  44. yschild: Container(
  45. child: Column(
  46. children: [
  47. Container(
  48. child: ListView.separated(
  49. itemBuilder: (context,index){
  50. Map item = _showArray[index];
  51. return Container(
  52. color: Colors.white,
  53. width: ysWidth(context),
  54. padding: EdgeInsets.only(top: 18,bottom: 18,left: 18,right: 18),
  55. child: LayoutBuilder(
  56. builder: (context,conSize){
  57. return GestureDetector(
  58. onTap: () async{
  59. if(item['title']=='账号绑定'){
  60. Navigator.of(context).push(
  61. CupertinoPageRoute(
  62. builder: (context){
  63. return YSBindInfo();
  64. }
  65. )
  66. );
  67. }else if(item['title']=='推送设置'){
  68. // item['value'] = !item['value'];
  69. // setState(() {});
  70. }else if(item['title']=='服务条款'||item['title']=='隐私政策'){
  71. Navigator.of(context).push(
  72. CupertinoPageRoute(
  73. builder: (context){
  74. return YSUrlPage(
  75. url: item['title']=='服务条款'?'https://fy.niwoshenghuo.com/tool/service':'https://fy.niwoshenghuo.com/tool/privacy',
  76. title: item['title'],
  77. );
  78. }
  79. )
  80. );
  81. }else if(item['title']=='密码设置'){
  82. Navigator.of(context).push(
  83. CupertinoPageRoute(
  84. builder: (context){
  85. return YSChangePassWord();
  86. }
  87. )
  88. );
  89. }else if(item['title']=='清理缓存'){
  90. showDialog(
  91. context:context,
  92. builder:(context){
  93. return AlertDialog(
  94. title: Text('提示',style: TextStyle(fontSize: 16,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  95. content: Text('确认清除缓存?',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  96. actions: [
  97. CupertinoButton(
  98. padding: EdgeInsets.all(0),
  99. child: Text('取消',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  100. onPressed: (){
  101. Navigator.pop(context);
  102. },
  103. ),
  104. CupertinoButton(
  105. padding: EdgeInsets.all(0),
  106. child: Text('确认',style: TextStyle(fontSize: 13,color: Color(0xFF292929),decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  107. onPressed: (){
  108. Navigator.pop(context);
  109. _clearCache();
  110. },
  111. ),
  112. ],
  113. );
  114. }
  115. );
  116. }else if(item['title']=='版本号'){
  117. }else if(item['title']=='账号注销'){
  118. User.instance.idCard = null;
  119. bool isCheck = await checkValue(context,tips: '您已经领取两筛免费券,如需注销请联系发券机构');
  120. if(isCheck){
  121. ysShowCenterAlertView(context, YSTipsAlertView(
  122. tipsStr: '是否注销此账号?',
  123. valueSetter: (value) async{
  124. if(value){
  125. Map dict = await ysRequestHttpNoLoading(context, requestType.post, 'sign/delete', {});
  126. if(dict!=null){
  127. SharedPreferences prefer = await SharedPreferences.getInstance();
  128. prefer.remove('chapters');
  129. prefer.remove('token');
  130. prefer.remove('isMeans');
  131. Navigator.of(context).push(
  132. CupertinoPageRoute(
  133. builder: (context){
  134. return YSLogin(isCheck: true,);
  135. }
  136. )
  137. );
  138. }
  139. }
  140. },
  141. ));
  142. }
  143. }else if(item['title']=='退出登录'){
  144. ysShowCenterAlertView(context, YSTipsAlertView(
  145. tipsStr: '是否退出登录?',
  146. valueSetter: (value) async{
  147. if(value){
  148. SharedPreferences prefer = await SharedPreferences.getInstance();
  149. prefer.remove('token');
  150. User.instance.idCard = null;
  151. Navigator.of(context).push(
  152. CupertinoPageRoute(
  153. builder: (context){
  154. return YSLogin(isCheck: true,);
  155. }
  156. )
  157. );
  158. }
  159. },
  160. ));
  161. }
  162. },
  163. behavior: HitTestBehavior.opaque,
  164. child: Row(
  165. children: [
  166. Container(
  167. width: conSize.maxWidth*0.3,
  168. child: Text(item['title'],style: TextStyle(color: Color(0xFF292929),fontSize: 14,decoration: TextDecoration.none,fontWeight: FontWeight.normal),),
  169. ),
  170. Container(
  171. width: conSize.maxWidth*0.7,
  172. child: item['title']=='推送设置'?GestureDetector(
  173. onTap: (){
  174. item['value'] = !item['value'];
  175. setState(() {});
  176. },
  177. behavior: HitTestBehavior.opaque,
  178. child: Image.asset(
  179. 'lib/images/${item['value']?'shut':'open'}.png',
  180. height: 20,
  181. width: 80,
  182. ),
  183. ):Row(
  184. children: [
  185. Container(
  186. width: conSize.maxWidth*0.7-(item['title']!='版本号'?20:0),
  187. alignment: Alignment.centerRight,
  188. child: Text(item['value']??'',style: TextStyle(color: Color(0xFFAEB3BD),fontSize: 14,decoration: TextDecoration.none,fontWeight: FontWeight.normal)),
  189. ),
  190. if(item['title']!='版本号')Icon(Icons.chevron_right,size: 20,color: Color(0xFFAEB3BD),)
  191. ],
  192. ),
  193. alignment: Alignment.centerRight,
  194. ),
  195. ],
  196. ),
  197. );
  198. },
  199. ),
  200. );
  201. },
  202. separatorBuilder: (context,index){
  203. Map item = _showArray[index];
  204. return item['title']=='推送设置'||item['title']=='账号注销'?Container(
  205. height: 5,
  206. ):Container(
  207. height: 1.4,
  208. padding: EdgeInsets.only(left: 18,right: 18),
  209. width: ysWidth(context),
  210. child: Image.asset('lib/images/line.png',fit: BoxFit.fitWidth,),
  211. );
  212. },
  213. itemCount: _showArray.length,
  214. shrinkWrap: true,
  215. physics: NeverScrollableScrollPhysics(),
  216. padding: EdgeInsets.all(0),
  217. ),
  218. )
  219. ],
  220. ),
  221. ),
  222. );
  223. }
  224. _getUserData() async{
  225. Map dict = await ysRequestHttp(context, requestType.get, 'user/info', {});
  226. if(dict!=null){
  227. healthName = dict['health_name'];
  228. healthId = dict['health_id'];
  229. _getWebData();
  230. }
  231. }
  232. _getWebData() async{
  233. Map dict = await ysRequestHttpNoLoading(context, requestType.get, 'basic/info', {});
  234. if(dict!=null){
  235. webStr = dict['data']['service'];
  236. setState(() {});
  237. }
  238. }
  239. void _clearCache() async {
  240. Directory tempDir = await getTemporaryDirectory();
  241. //删除缓存目录
  242. await delDir(tempDir);
  243. await loadCache();
  244. ysFlutterToast(context, '清除缓存成功');
  245. }
  246. Future<Null> delDir(FileSystemEntity file) async {
  247. if (file is Directory) {
  248. final List<FileSystemEntity> children = file.listSync();
  249. for (final FileSystemEntity child in children) {
  250. var isHave =await child.exists(); //返回真假
  251. if(isHave){
  252. await delDir(child);
  253. }
  254. }
  255. }
  256. await file.delete();
  257. }
  258. Future<Null> loadCache() async {
  259. Directory tempDir = await getTemporaryDirectory();
  260. double value = await _getTotalSizeOfFilesInDir(tempDir);
  261. /*tempDir.list(followLinks: false,recursive: true).listen((file){
  262. //打印每个缓存文件的路径
  263. print(file.path);
  264. });*/
  265. print('临时目录大小: ' + value.toString());
  266. Map cacheMap = _showArray.firstWhere((element) => element['title']=='清理缓存');
  267. Map versionMap = _showArray.firstWhere((element) => element['title']=='版本号');
  268. PackageInfo packageInfo = await PackageInfo.fromPlatform();
  269. versionMap['value'] = packageInfo.version;
  270. cacheMap['value'] = _renderSize(value);
  271. // LogUtil.d('packageInfo========${versionMap['value']}=====${cacheMap['value']}');
  272. setState(() {});
  273. }
  274. Future<double> _getTotalSizeOfFilesInDir(final FileSystemEntity file) async {
  275. if (file is File) {
  276. int length = await file.length();
  277. return double.parse(length.toString());
  278. }
  279. if (file is Directory) {
  280. final List<FileSystemEntity> children = file.listSync();
  281. double total = 0;
  282. if (children != null)
  283. for (final FileSystemEntity child in children)
  284. total += await _getTotalSizeOfFilesInDir(child);
  285. return total;
  286. }
  287. return 0;
  288. }
  289. _renderSize(double value) {
  290. if (null == value) {
  291. return 0;
  292. }
  293. List<String> unitArr = List()
  294. ..add('B')
  295. ..add('K')
  296. ..add('M')
  297. ..add('G');
  298. int index = 0;
  299. while (value > 1024) {
  300. index++;
  301. value = value / 1024;
  302. }
  303. String size = value.toStringAsFixed(2);
  304. return size + unitArr[index];
  305. }
  306. }