You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

331 lines
10 KiB

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/main.dart';
import 'package:huixiang/utils/event_type.dart';
import 'package:huixiang/utils/flutter_utils.dart';
import 'package:huixiang/utils/font_weight.dart';
import 'package:huixiang/utils/min.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:huixiang/view_widget/round_button.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
class SettingPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _SettingPage();
}
}
class _SettingPage extends State<SettingPage> {
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: MyAppBar(
title: S.of(context).shezhi,
titleColor: Colors.black,
background: Color(0xFFF7F7F7),
leadingColor: Colors.black,
),
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.h,
),
Container(
margin: EdgeInsets.only(left: 16),
child: Text(
S.of(context).shezhi,
style: TextStyle(
fontWeight: MyFontWeight.medium,
color: Color(0xFF353535),
fontSize: 16.sp,
),
),
),
SizedBox(
height: 16.h,
),
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.h,
alignment: Alignment.center,
color: Color(0xFF32A060),
child: RoundButton(
text: S.of(context).tuichudenglu,
backgroup: Color(0xFF32A060),
textColor: Colors.white,
fontSize: 16.sp,
fontWeight: MyFontWeight.semi_bold,
),
),
),
],
),
flex: 1,
)
],
),
),
);
}
clearCache() async {
if (Platform.isAndroid) {
if (!(await Min.isInitialize())) {
await Min.initialize();
}
}
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
List<String> miniAppids = sharedPreferences.getStringList("miniAppid");
if (miniAppids != null && miniAppids.length > 0) {
miniAppids.forEach((element) async {
print("appid: $element");
await Min.closeCurrentApp();
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.setString("token", "");
sharedPreferences.setString("user", "");
sharedPreferences.setString("userJson", "");
sharedPreferences.setString("userId", "");
sharedPreferences.setString("mobile", "");
sharedPreferences.setString("nick", "");
Navigator.of(context)
.pushNamedAndRemoveUntil('/router/login_page', (route) => false);
if (xgFlutterPlugin != null) {
xgFlutterPlugin.stopXg();
}
}
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: MyFontWeight.medium,
fontSize: 14.sp,
color: Color(0xFF353535),
),
),
flex: 1,
),
Text(
right,
style: TextStyle(
fontSize: 12.sp,
fontWeight: MyFontWeight.regular,
color: Color(0xFF727272)),
),
SizedBox(
width: 16.w,
),
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.sp,
color: Color(0xFF353535),
fontWeight: MyFontWeight.medium,
),
),
),
SizedBox(
width: 16.w,
),
Icon(
Icons.keyboard_arrow_right,
size: 20,
),
],
),
);
}
}