|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
|
|
import 'package:huixiang/generated/l10n.dart';
|
|
|
|
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';
|
|
|
|
import 'package:huixiang/utils/flutter_utils.dart';
|
|
|
|
import 'package:huixiang/view_widget/cupertino_date_picker.dart';
|
|
|
|
import 'package:image_cropper/image_cropper.dart';
|
|
|
|
import 'package:image_picker_gallery_camera/image_picker_gallery_camera.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:retrofit/retrofit.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:http_parser/http_parser.dart';
|
|
|
|
|
|
|
|
class UserInfoPage extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() {
|
|
|
|
return _UserInfoPage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _UserInfoPage extends State<UserInfoPage> {
|
|
|
|
ApiService apiService;
|
|
|
|
|
|
|
|
Map<String, dynamic> modifyInfo = {
|
|
|
|
"birth": "",
|
|
|
|
"headimg": "",
|
|
|
|
"nickname": "",
|
|
|
|
"sex": ""
|
|
|
|
};
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
SharedPreferences.getInstance().then((value) => {
|
|
|
|
user = UserInfo.fromJson(jsonDecode(value.getString('user'))),
|
|
|
|
mobile = value.getString('mobile'),
|
|
|
|
modifyInfo["nickname"] = value.getString('nick'),
|
|
|
|
modifyInfo["birth"] = user.birth,
|
|
|
|
modifyInfo["headimg"] = user.headimg,
|
|
|
|
modifyInfo["sex"] = user.sex,
|
|
|
|
age = AppUtils.getAgeByString(user.birth),
|
|
|
|
refresh(),
|
|
|
|
apiService = ApiService(Dio(), token: value.getString('token')),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
String age;
|
|
|
|
String mobile = "";
|
|
|
|
String locale = "zh";
|
|
|
|
UserInfo user;
|
|
|
|
|
|
|
|
refresh() async {
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(
|
|
|
|
S.of(context).zhanghaoxinxi,
|
|
|
|
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: [
|
|
|
|
Container(
|
|
|
|
margin: EdgeInsets.only(left: 16),
|
|
|
|
child: Text(
|
|
|
|
S.of(context).gerenxinxi,
|
|
|
|
style: TextStyle(
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
color: Color(0xFF353535),
|
|
|
|
fontSize: 16,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
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 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: [
|
|
|
|
InkWell(
|
|
|
|
onTap: () {
|
|
|
|
showImagePicker();
|
|
|
|
},
|
|
|
|
child: avatarItem(0, ""),
|
|
|
|
),
|
|
|
|
avatarItem(1, modifyInfo["nickname"]),
|
|
|
|
InkWell(
|
|
|
|
onTap: showDateSelector,
|
|
|
|
child: avatarItem(
|
|
|
|
2,
|
|
|
|
(modifyInfo["birth"] != null && modifyInfo["birth"] != "")
|
|
|
|
? modifyInfo["birth"]
|
|
|
|
: S.of(context).wanshanshengrixinxi_yhq),
|
|
|
|
),
|
|
|
|
avatarItem(3, mobile),
|
|
|
|
avatarItem(
|
|
|
|
4,
|
|
|
|
(age == null || age == "")
|
|
|
|
? S.of(context).wanshanshengrixinxi_nl
|
|
|
|
: S.of(context).sui(age)),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> itemLefts = [
|
|
|
|
S.current.touxiang,
|
|
|
|
S.current.yonghuming,
|
|
|
|
S.current.wodeshengri,
|
|
|
|
S.current.shoujihao,
|
|
|
|
S.current.wodenianling,
|
|
|
|
];
|
|
|
|
|
|
|
|
showDateSelector() async {
|
|
|
|
DateTime dateTime = await showModalBottomSheet(
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
context: context,
|
|
|
|
builder: (_) {
|
|
|
|
return CupertinoDatePickerWidget();
|
|
|
|
});
|
|
|
|
if (dateTime != null) {
|
|
|
|
modifyInfo["birth"] = DateFormat("yyyy-MM-dd").format(dateTime);
|
|
|
|
user.birth = modifyInfo["birth"];
|
|
|
|
age = AppUtils.getAge(dateTime);
|
|
|
|
modifyInfos();
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///显示图片选择方式
|
|
|
|
showImagePicker() {
|
|
|
|
showCupertinoModalPopup(
|
|
|
|
context: context,
|
|
|
|
builder: (contetx) {
|
|
|
|
return CupertinoActionSheet(
|
|
|
|
title: Text("更换头像"),
|
|
|
|
actions: [
|
|
|
|
CupertinoActionSheetAction(
|
|
|
|
child: Text('拍照'),
|
|
|
|
onPressed: () {
|
|
|
|
openCamera();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
isDefaultAction: true,
|
|
|
|
isDestructiveAction: false,
|
|
|
|
),
|
|
|
|
CupertinoActionSheetAction(
|
|
|
|
child: Text('相册'),
|
|
|
|
onPressed: () {
|
|
|
|
openStorage();
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
isDefaultAction: true,
|
|
|
|
isDestructiveAction: false,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
cancelButton: CupertinoActionSheetAction(
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
child: Text("取消"),
|
|
|
|
isDestructiveAction: true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
///拍照
|
|
|
|
openCamera() async {
|
|
|
|
if (await Permission.camera.isGranted) {
|
|
|
|
var image = await ImagePickerGC.pickImage(
|
|
|
|
context: context, source: ImgSource.Camera);
|
|
|
|
if (image == null) return;
|
|
|
|
var imageFile = await cropImage(image.path);
|
|
|
|
if (imageFile == null) return;
|
|
|
|
filePath = imageFile.path;
|
|
|
|
fileUpload();
|
|
|
|
} else {
|
|
|
|
await Permission.camera.request();
|
|
|
|
openCamera();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
String filePath;
|
|
|
|
|
|
|
|
///打开相册
|
|
|
|
openStorage() async {
|
|
|
|
if (await Permission.storage.isGranted) {
|
|
|
|
var image = await ImagePickerGC.pickImage(
|
|
|
|
context: context, source: ImgSource.Gallery);
|
|
|
|
var imageFile = await cropImage(image.path);
|
|
|
|
if (imageFile == null) return;
|
|
|
|
filePath = imageFile.path;
|
|
|
|
setState(() {});
|
|
|
|
fileUpload();
|
|
|
|
} else {
|
|
|
|
await Permission.storage.request();
|
|
|
|
openStorage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
rectHeight:500,
|
|
|
|
));
|
|
|
|
|
|
|
|
return croppedFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
///调用修改用户信息接口
|
|
|
|
modifyInfos() async {
|
|
|
|
var info = await apiService.editInfo(modifyInfo);
|
|
|
|
if (info.isSuccess) {
|
|
|
|
setState(() {
|
|
|
|
Fluttertoast.showToast(msg: "用户信息修改成功");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///文件上传
|
|
|
|
fileUpload() async {
|
|
|
|
if (filePath != null && filePath != "" && await File(filePath).exists()) {
|
|
|
|
BaseData baseData = await apiService.upload(File(filePath), 123123123);
|
|
|
|
if (baseData != null && baseData.isSuccess) {
|
|
|
|
UploadResult uploadResult = UploadResult.fromJson(baseData.data);
|
|
|
|
modifyInfo["headimg"] = uploadResult.url;
|
|
|
|
modifyInfos();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget avatarItem(type, value) {
|
|
|
|
return Container(
|
|
|
|
margin: EdgeInsets.only(top: 14, bottom: 14),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: Text(
|
|
|
|
itemLefts[type],
|
|
|
|
style: TextStyle(
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
fontSize: 14,
|
|
|
|
color: Color(0xFF353535),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
flex: 1,
|
|
|
|
),
|
|
|
|
buildValue(type, value),
|
|
|
|
if (type == 3 || type == 0 || type == 2) valueEnd(),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget valueEnd() {
|
|
|
|
return Container(
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
|
|
|
width: 8,
|
|
|
|
),
|
|
|
|
Icon(
|
|
|
|
Icons.keyboard_arrow_right,
|
|
|
|
size: 20,
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget buildValue(type, value) {
|
|
|
|
if (type == 0) {
|
|
|
|
return ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(4),
|
|
|
|
child: filePath == null || filePath == ""
|
|
|
|
? (modifyInfo["headimg"] != null && modifyInfo["headimg"] != ""
|
|
|
|
? Image.network(
|
|
|
|
modifyInfo["headimg"],
|
|
|
|
width: 42,
|
|
|
|
height: 42,
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
)
|
|
|
|
: Image.asset(
|
|
|
|
"assets/image/default_1.png",
|
|
|
|
width: 42,
|
|
|
|
height: 42,
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
))
|
|
|
|
: Image.file(
|
|
|
|
File(filePath),
|
|
|
|
width: 42,
|
|
|
|
height: 42,
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else if (type == 1) {
|
|
|
|
return Text(
|
|
|
|
value,
|
|
|
|
style: TextStyle(
|
|
|
|
fontWeight: FontWeight.bold, fontSize: 14, color: Colors.black),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Text(
|
|
|
|
value,
|
|
|
|
style: TextStyle(
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
fontSize: 14,
|
|
|
|
color: Color(0xFF727272)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|