import 'dart:io'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:huixiang/generated/l10n.dart'; import 'package:huixiang/login/login_page.dart'; import 'package:huixiang/main.dart'; import 'package:huixiang/utils/event_type.dart'; import 'package:huixiang/utils/flutter_utils.dart'; import 'package:huixiang/utils/min.dart'; import 'package:huixiang/view_widget/round_button.dart'; import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; class SettingPage extends StatefulWidget { @override State createState() { return _SettingPage(); } } class _SettingPage extends State { String locale = "tw"; String cacheTotal = "0B"; @override void initState() { super.initState(); setLanguage(); AppUtils.total().then((value) { cacheTotal = (value > 0) ? "${(value / 1024.0 ~/ 1024.0)}M" : "0B"; setState(() {}); }); } setLanguage() async { SharedPreferences shared = await SharedPreferences.getInstance(); locale = shared.getString('language'); if (mounted) setState(() {}); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( S.of(context).shezhi, style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold, ), ), centerTitle: false, backgroundColor: Color(0xFFF7F7F7), elevation: 0, leading: GestureDetector( onTap: () { Navigator.of(context).pop(); }, child: Container( alignment: Alignment.centerRight, margin: EdgeInsets.only(left: 10), padding: EdgeInsets.all(6), child: Icon( Icons.arrow_back_ios, color: Colors.black, size: 24, ), ), ), titleSpacing: 2, leadingWidth: 56, ), body: Container( decoration: new BoxDecoration( border: Border(bottom: BorderSide(color: Color(0xffF7F7F7), width: 0.0)), color: Color(0xffF7F7F7), ), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, mainAxisSize: MainAxisSize.max, children: [ SizedBox( height: 24, ), Container( margin: EdgeInsets.only(left: 16), child: Text( S.of(context).shezhi, style: TextStyle( fontWeight: FontWeight.bold, color: Color(0xFF353535), fontSize: 16, ), ), ), SizedBox( height: 16, ), GestureDetector( onTap: () { showCupertinoModalPopup( context: context, builder: (contetx) { return CupertinoActionSheet( title: Text("更换语言"), actions: [ CupertinoActionSheetAction( child: Text('简体中文'), onPressed: () { changeLanguage("zh"); }, isDefaultAction: true, isDestructiveAction: false, ), CupertinoActionSheetAction( child: Text('繁体中文'), onPressed: () { changeLanguage("tw"); }, isDefaultAction: true, isDestructiveAction: false, ), ], cancelButton: CupertinoActionSheetAction( onPressed: () { Navigator.of(context).pop(); }, child: Text("取消"), isDestructiveAction: true, ), ); }, ); }, child: settingItem( S.of(context).yuyan, locale == "zh" ? S.of(context).zhongwenjianti : S.of(context).fantizhongwen), ), GestureDetector( child: settingSingleItem(S.of(context).quanxian), onTap: () { Navigator.of(context).pushNamed('/router/permission_setting'); }, ), GestureDetector( child: settingSingleItem(S.of(context).bangzhuyufankui), onTap: () { Navigator.of(context).pushNamed('/router/help_feedback_page'); }, ), GestureDetector( child: settingItem(S.of(context).qinglihuancun, cacheTotal), onTap: () { clearCache(); }, ), GestureDetector( child: settingSingleItem(S.of(context).guanyu), onTap: () { Navigator.of(context).pushNamed('/router/about_page'); }, ), Expanded( child: Column( mainAxisAlignment: MainAxisAlignment.end, mainAxisSize: MainAxisSize.max, children: [ InkWell( onTap: () { logOut(); }, child: Container( height: 54, alignment: Alignment.center, color: Color(0xFF32A060), child: RoundButton( text: S.of(context).tuichudenglu, backgroup: Color(0xFF32A060), textColor: Colors.white, fontSize: 16, fontWeight: FontWeight.bold, ), ), ), ], ), flex: 1, ) ], ), ), ); } clearCache() async { if (Platform.isAndroid) { if(!(await Min.isInitialize())) { await Min.initialize(); } } SharedPreferences sharedPreferences = await SharedPreferences.getInstance(); List miniAppids = sharedPreferences.getStringList("miniAppid"); if (miniAppids != null && miniAppids.length > 0) { miniAppids.forEach((element) async { print("appid: $element"); String path = await Min.getAppBasePath(element); AppUtils.deleteDirectory(Directory(path)); String filePath = ""; if (Platform.isAndroid) { filePath = (await getExternalStorageDirectory()).path; } else { filePath = (await getApplicationDocumentsDirectory()).path; } AppUtils.deleteDirectory(Directory(filePath)); }); sharedPreferences.setStringList("miniAppid", []); } cacheTotal = "0B"; setState(() {}); } changeLanguage(language) { if (language == "zh") { // 语言 語言 S.load(Locale.fromSubtags(languageCode: 'zh', countryCode: 'CN')); locale = "zh"; eventBus.fire(EventType(0)); } else { S.load(Locale.fromSubtags(languageCode: 'zh', countryCode: 'TW')); locale = "tw"; eventBus.fire(EventType(1)); } SharedPreferences.getInstance().then((value) => { value.setString("language", language), }); setState(() {}); Navigator.of(context).pop(); } logOut() async { SharedPreferences sharedPreferences = await SharedPreferences.getInstance(); sharedPreferences.clear(); Navigator.pushAndRemoveUntil(context, new MaterialPageRoute( builder: (BuildContext context) { if (xgFlutterPlugin != null) { xgFlutterPlugin.stopXg(); } return LoginPage(); }, ), (route) => route == null); } Widget settingItem(left, right) { return Container( margin: EdgeInsets.fromLTRB(16, 8, 16, 8), padding: EdgeInsets.fromLTRB(20, 16, 20, 16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(8)), boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ]), child: Row( children: [ Expanded( child: Text( left, style: TextStyle( fontWeight: FontWeight.bold, fontSize: 14, color: Color(0xFF353535), ), ), flex: 1, ), Text( right, style: TextStyle(fontSize: 12, color: Color(0xFF727272)), ), SizedBox( width: 16, ), Icon( Icons.keyboard_arrow_right, size: 20, ), ], ), ); } Widget settingSingleItem(right) { return Container( margin: EdgeInsets.fromLTRB(16, 8, 16, 8), padding: EdgeInsets.fromLTRB(20, 16, 20, 16), decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(8)), boxShadow: [ BoxShadow( color: Colors.black.withAlpha(12), offset: Offset(0, 3), blurRadius: 14, spreadRadius: 0, ) ], ), child: Row( children: [ Expanded( child: Text( right, style: TextStyle( fontSize: 14, color: Color(0xFF353535), fontWeight: FontWeight.bold, ), ), ), SizedBox( width: 16, ), Icon( Icons.keyboard_arrow_right, size: 20, ), ], ), ); } }