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.

479 lines
14 KiB

4 years ago
import 'dart:convert';
4 years ago
import 'dart:io';
import 'package:dio/dio.dart';
4 years ago
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
4 years ago
import 'package:huixiang/generated/l10n.dart';
4 years ago
import 'package:huixiang/retrofit/data/base_data.dart';
import 'package:huixiang/retrofit/data/upload_result.dart';
import 'package:huixiang/retrofit/data/user_info.dart';
import 'package:huixiang/retrofit/retrofit_api.dart';
4 years ago
import 'package:huixiang/utils/flutter_utils.dart';
import 'package:huixiang/utils/font_weight.dart';
4 years ago
import 'package:huixiang/view_widget/cupertino_date_picker.dart';
3 years ago
import 'package:huixiang/view_widget/custom_image.dart';
4 years ago
import 'package:huixiang/view_widget/my_appbar.dart';
3 years ago
import 'package:image_pickers/image_pickers.dart';
4 years ago
import 'package:intl/intl.dart';
4 years ago
import 'package:permission_handler/permission_handler.dart';
4 years ago
import 'package:shared_preferences/shared_preferences.dart';
4 years ago
import 'package:flutter_screenutil/flutter_screenutil.dart';
4 years ago
class UserInfoPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _UserInfoPage();
}
}
class _UserInfoPage extends State<UserInfoPage> {
4 years ago
ApiService apiService;
Map<String, dynamic> modifyInfo = {
"birth": "",
"headimg": "",
"nickname": "",
"sex": "",
"signature":""
4 years ago
};
4 years ago
@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((value) => {
4 years ago
print(value.getString('user')),
4 years ago
user = UserInfo.fromJson(jsonDecode(value.getString('user'))),
4 years ago
mobile = value.getString('mobile'),
4 years ago
modifyInfo["nickname"] = user.nickname,
modifyInfo["signature"] = user.signature,
4 years ago
modifyInfo["birth"] = user.birth,
modifyInfo["headimg"] = user.headimg,
modifyInfo["sex"] = user.sex,
age = AppUtils.getAgeByString(user.birth),
refresh(),
4 years ago
apiService = ApiService(Dio(), context: context, token: value.getString('token')),
4 years ago
});
}
String age;
String mobile = "";
String locale = "zh";
4 years ago
UserInfo user;
4 years ago
refresh() async {
setState(() {});
}
4 years ago
@override
Widget build(BuildContext context) {
return Scaffold(
4 years ago
appBar: MyAppBar(
title: S.of(context).zhanghaoxinxi,
titleColor: Colors.black,
background: Color(0xFFF7F7F7),
leadingColor: Colors.black,
4 years ago
),
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
children: [
Container(
4 years ago
margin: EdgeInsets.only(left: 16.w),
4 years ago
child: Text(
S.of(context).gerenxinxi,
style: TextStyle(
4 years ago
fontWeight: FontWeight.bold,
color: Color(0xFF353535),
4 years ago
fontSize: 16.sp,
4 years ago
),
4 years ago
),
),
userInfo(),
],
),
),
);
}
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,
)
3 years ago
],),
4 years ago
child: Row(
children: [
Expanded(
child: Text(
left,
style: TextStyle(
fontWeight: FontWeight.bold,
4 years ago
fontSize: 14.sp,
4 years ago
color: Color(0xFF353535),
),
),
flex: 1,
),
Text(
right,
4 years ago
style: TextStyle(
fontSize: 12.sp,
color: Color(0xFF727272),
),
4 years ago
),
SizedBox(
4 years ago
width: 16.w,
4 years ago
),
Icon(
Icons.keyboard_arrow_right,
size: 20,
),
],
),
);
}
Widget userInfo() {
return Container(
margin: EdgeInsets.fromLTRB(16, 32, 16, 16),
padding: EdgeInsets.fromLTRB(20, 6, 20, 18),
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: Column(
children: [
4 years ago
InkWell(
onTap: () {
showImagePicker();
},
child: avatarItem(0, ""),
),
4 years ago
InkWell(
4 years ago
onTap: () {
4 years ago
editname();
},
4 years ago
child: avatarItem(1,
(modifyInfo["nickname"]) == "" ? "回乡" : modifyInfo["nickname"]),
4 years ago
),
InkWell(
onTap: () {
editSignature();
},
child:avatarItem(2,
(modifyInfo["signature"]) == "" ? "还未编辑个性签名~" : modifyInfo["signature"]),
),
4 years ago
InkWell(
onTap:(modifyInfo["birth"] == "")?showDateSelector:(){},
4 years ago
child: avatarItem(
3,
4 years ago
(modifyInfo["birth"] != null && modifyInfo["birth"] != "")
? modifyInfo["birth"]
4 years ago
: S.of(context).wanshanshengrixinxi_yhq),
4 years ago
),
avatarItem(4, mobile),
4 years ago
avatarItem(
5,
4 years ago
(age == null || age == "")
4 years ago
? S.of(context).wanshanshengrixinxi_nl
: S.of(context).sui(age)),
],
),
);
}
4 years ago
editname() async {
4 years ago
dynamic name = await Navigator.of(context).pushNamed('/router/edit_name',
arguments: {"nick": modifyInfo['nickname']});
if (name != null && name != "") {
4 years ago
modifyInfo["nickname"] = name;
setState(() {});
modifyInfos();
}
}
editSignature() async {
dynamic signature = await Navigator.of(context).pushNamed('/router/edit_signature',
arguments: {"signature": modifyInfo['signature']});
if (signature != null && signature != "") {
modifyInfo["signature"] = signature;
setState(() {});
modifyInfos();
}
}
4 years ago
List<String> itemLefts = [
S.current.touxiang,
S.current.yonghuming,
"个性签名",
4 years ago
S.current.wodeshengri,
S.current.shoujihao,
S.current.wodenianling,
];
showDateSelector() async {
4 years ago
DateTime dateTime = await showModalBottomSheet(
4 years ago
backgroundColor: Colors.transparent,
context: context,
4 years ago
builder: (_) {
4 years ago
return CupertinoDatePickerWidget();
});
if (dateTime != null ) {
4 years ago
modifyInfo["birth"] = DateFormat("yyyy-MM-dd").format(dateTime);
user.birth = modifyInfo["birth"];
4 years ago
age = AppUtils.getAge(dateTime);
4 years ago
modifyInfos();
4 years ago
setState(() {});
}
}
4 years ago
///显示图片选择方式
showImagePicker() {
showCupertinoModalPopup(
context: context,
builder: (contetx) {
return CupertinoActionSheet(
4 years ago
title: Text(S.of(context).genghuantouxiang),
4 years ago
actions: [
CupertinoActionSheetAction(
4 years ago
child: Text(S.of(context).paizhao),
4 years ago
onPressed: () {
openCamera();
Navigator.of(context).pop();
},
isDefaultAction: true,
isDestructiveAction: false,
),
CupertinoActionSheetAction(
4 years ago
child: Text(S.of(context).xiangce),
4 years ago
onPressed: () {
openStorage();
Navigator.of(context).pop();
},
isDefaultAction: true,
isDestructiveAction: false,
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () {
Navigator.of(context).pop();
},
4 years ago
child: Text(S.of(context).quxiao),
4 years ago
isDestructiveAction: true,
),
);
});
}
///拍照
openCamera() async {
if (await Permission.camera.isGranted) {
3 years ago
Media medias = await ImagePickers.openCamera(
cameraMimeType: CameraMimeType.photo,
cropConfig: CropConfig(
enableCrop: true,
width: 200,
height: 200,
),
compressSize: 500,
);
if (medias == null) return;
filePath = medias.path;
4 years ago
fileUpload();
4 years ago
} else {
await Permission.camera.request();
openCamera();
}
}
String filePath;
///打开相册
openStorage() async {
if (await Permission.storage.isGranted) {
3 years ago
List<Media> medias = await ImagePickers.pickerPaths(
galleryMode: GalleryMode.image,
selectCount: 1,
showGif: true,
showCamera: false,
compressSize: 500,
uiConfig: UIConfig(
uiThemeColor: Color(0xFFFFFFFF),
),
cropConfig: CropConfig(
enableCrop: true,
width: 200,
height: 200,
),
);
if (medias == null || medias.length == 0) return;
filePath = medias[0].path;
4 years ago
setState(() {});
4 years ago
fileUpload();
} else {
await Permission.storage.request();
openStorage();
}
}
3 years ago
// Future<File> cropImage(imagePath) async {
// File croppedFile = await ImageCropper.cropImage(
// sourcePath: imagePath,
// aspectRatioPresets: [
// CropAspectRatioPreset.square,
// // CropAspectRatioPreset.ratio3x2,
// // CropAspectRatioPreset.original,
// // CropAspectRatioPreset.ratio4x3,
// // CropAspectRatioPreset.ratio16x9
// ],
// aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
// androidUiSettings: AndroidUiSettings(
// toolbarTitle: 'Cropper',
// toolbarColor: Colors.black,
// toolbarWidgetColor: Colors.white,
// initAspectRatio: CropAspectRatioPreset.square,
// hideBottomControls: true,
// lockAspectRatio: false),
// iosUiSettings: IOSUiSettings(
// minimumAspectRatio: 1.0,
// resetAspectRatioEnabled: false,
// aspectRatioPickerButtonHidden: true,
// rectWidth: 500.w,
// rectHeight: 500.h,
// ));
// return croppedFile;
// }
4 years ago
4 years ago
///调用修改用户信息接口
modifyInfos() async {
3 years ago
var info = await apiService.editInfo(modifyInfo).catchError((onError) {});
4 years ago
if (info.isSuccess) {
SharedPreferences.getInstance().then((value){
value.setString('user', jsonEncode(modifyInfo));
});
4 years ago
setState(() {
4 years ago
SmartDialog.showToast("用户信息修改成功", alignment: Alignment.center);
4 years ago
});
}
}
///文件上传
fileUpload() async {
if (filePath != null && filePath != "" && await File(filePath).exists()) {
BaseData<UploadResult> baseData = await apiService.upload(File(filePath), 123123123,false).catchError((onError) {});
4 years ago
if (baseData != null && baseData.isSuccess) {
3 years ago
UploadResult uploadResult = baseData.data;
4 years ago
modifyInfo["headimg"] = uploadResult.url;
modifyInfos();
}
}
}
4 years ago
Widget avatarItem(type, value) {
3 years ago
print("object: $value");
4 years ago
return Container(
4 years ago
margin: EdgeInsets.only(top: 14.h, bottom: 14.h),
4 years ago
child: Row(
children: [
Expanded(
child: Text(
itemLefts[type],
style: TextStyle(
fontWeight: FontWeight.bold,
4 years ago
fontSize: 14.sp,
4 years ago
color: Color(0xFF353535),
),
),
flex: 1,
),
buildValue(type, value),
if (type != 5 && type != 4 && type != 3 && (((modifyInfo["birth"] == "")) || type != 3))
valueEnd(),
4 years ago
],
),
);
}
Widget valueEnd() {
return Container(
child: Icon(
Icons.keyboard_arrow_right,
size: 20.sp,
)
4 years ago
);
}
Widget buildValue(type, value) {
if (type == 0) {
return ClipRRect(
borderRadius: BorderRadius.circular(4),
4 years ago
child: filePath == null || filePath == ""
? (modifyInfo["headimg"] != null && modifyInfo["headimg"] != ""
3 years ago
? MImage(
4 years ago
modifyInfo["headimg"],
3 years ago
width: 42,
height: 42,
4 years ago
fit: BoxFit.cover,
)
: Image.asset(
4 years ago
"assets/image/default_1.png",
3 years ago
width: 42,
height: 42,
4 years ago
fit: BoxFit.cover,
))
: Image.file(
File(filePath),
3 years ago
width: 42,
height: 42,
4 years ago
fit: BoxFit.cover,
),
4 years ago
);
} else if (type == 1) {
return Text(
value,
maxLines: 1,
overflow:TextOverflow.ellipsis,
4 years ago
style: TextStyle(
4 years ago
fontWeight: FontWeight.bold,
4 years ago
fontSize: 14.sp,
4 years ago
color: Colors.black,
),
4 years ago
);
} else {
return Expanded(child: Text(
4 years ago
value,
maxLines: 1,
overflow:TextOverflow.ellipsis,
textAlign: TextAlign.right,
4 years ago
style: TextStyle(
4 years ago
fontWeight: FontWeight.bold,
4 years ago
fontSize: 14.sp,
4 years ago
color: Color(0xFF727272),
),
));
4 years ago
}
}
}