import 'dart:ui';
|
import 'dart:io';
|
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/material.dart';
|
import '../../api/user_api.dart';
|
import '../../model/user/user_info.dart';
|
import '../../ui/widget/button.dart';
|
import '../../ui/widget/video_item.dart';
|
import '../../utils/image_util.dart';
|
import '../../api/http.dart';
|
import '../../ui/common/browser.dart';
|
import '../../ui/widget/dialog.dart';
|
import '../../ui/widget/nav.dart';
|
import '../../utils/cache_util.dart';
|
import '../../utils/config_util.dart';
|
import '../../utils/event_bus_util.dart';
|
import '../../utils/pageutils.dart';
|
import '../../utils/push_util.dart';
|
import '../../utils/setting_util.dart';
|
import '../../utils/string_util.dart';
|
import '../../utils/ui_constant.dart';
|
import '../../utils/ui_utils.dart';
|
import '../../utils/user_util.dart';
|
import 'package:package_info/package_info.dart';
|
import 'package:image_picker/image_picker.dart';
|
|
void main() {
|
runApp(MyApp());
|
}
|
|
class MyApp extends StatelessWidget {
|
// This widget is the root of your application.
|
@override
|
Widget build(BuildContext context) {
|
return MaterialApp(
|
title: '个人信息',
|
theme: ThemeData(primaryColor: const Color(0xFFF5F5F5)),
|
home: PersonInfoPage(title: ''),
|
);
|
}
|
}
|
|
class PersonInfoPage extends StatefulWidget {
|
PersonInfoPage({Key? key, required this.title}) : super(key: key);
|
|
// This widget is the home page of your application. It is stateful, meaning
|
// that it has a State object (defined below) that contains fields that affect
|
// how it looks.
|
|
// This class is the configuration for the state. It holds the values (in this
|
// case the title) provided by the parent (in this case the App widget) and
|
// used by the build method of the State. Fields in a Widget subclass are
|
// always marked "final".
|
|
final String title;
|
|
@override
|
_PersonInfoPageState createState() => _PersonInfoPageState();
|
}
|
|
class _PersonInfoPageState extends State<PersonInfoPage>
|
with SingleTickerProviderStateMixin {
|
UserInfo? _user;
|
|
String? editPortrait;
|
String? editNickName;
|
int? editSex;
|
DateTime? editBirthday;
|
String? editSign;
|
|
@override
|
void initState() {
|
super.initState();
|
UserUtil.getUserInfo().then((value) {
|
setState(() {
|
_user = value;
|
});
|
});
|
}
|
|
void selectImg() async {
|
File? image = await ImageUtil.selectAndCropImage();
|
if (image == null) {
|
return;
|
}
|
//image.path
|
setState(() {
|
editPortrait = image.path;
|
});
|
}
|
|
@override
|
Widget build(BuildContext context) {
|
return Scaffold(
|
backgroundColor: const Color(0xFFDADADA),
|
body: _user == null
|
? Container()
|
: Column(
|
children: [
|
TopNavBar(
|
title: "个人信息",
|
rightText: canSave() ? "保存" : null,
|
rightClick: canSave()
|
? () {
|
//保存
|
UserApiUtil.updateUserInfo(context,
|
nickName: editNickName,
|
sex: editSex,
|
birthDay: editBirthday == null
|
? null
|
: "${editBirthday!.year}/${editBirthday!.month}/${editBirthday!.day}",
|
sign: editSign,
|
portraitPath: editPortrait)
|
.then((value) {
|
if (value == null) {
|
return;
|
}
|
|
if (value["IsPost"] == "true") {
|
ToastUtil.toast("修改成功", context);
|
popPage(context);
|
} else {
|
ToastUtil.toast(value["Error"], context);
|
}
|
});
|
}
|
: null,
|
),
|
Expanded(
|
child: SingleChildScrollView(
|
child: Column(children: [
|
Container(
|
height: 10,
|
color: const Color(0xFFDADADA),
|
),
|
getPortraitItemView(onClick: () {
|
//选择头像
|
selectImg();
|
}),
|
getCommonItemView(
|
title: "昵称",
|
content: editNickName ?? _user!.nickname!,
|
onClick: () {
|
showEditNickName(editNickName ?? _user!.nickname!);
|
}),
|
getCommonItemView(
|
title: "性别",
|
content:
|
_getSexDesc((editSex ?? int.parse(_user!.sex!))),
|
onClick: () {
|
showEditSex(editSex ?? int.parse(_user!.sex!));
|
}),
|
getCommonItemView(
|
title: "生日",
|
content: _getBirthDay(),
|
onClick: () {
|
showEditBirthDay((editBirthday ??
|
((_user!.birthday == null ||
|
_user!.birthday!.isEmpty)
|
? null
|
: parseDate(_user!.birthday!))));
|
}),
|
getCommonItemView(
|
title: "邮箱",
|
content: _user!.email!,
|
onClick: () {},
|
canIn: false),
|
getCommonItemView(
|
title: "ID",
|
content: _user!.id!,
|
onClick: () {},
|
canIn: false),
|
Container(
|
height: 10,
|
color: const Color(0xFFDADADA),
|
),
|
getSignItemView(onClick: () {
|
showEditSign(editSign ?? _user!.sign);
|
}),
|
]))),
|
],
|
));
|
}
|
|
bool canSave() {
|
return editNickName != null ||
|
editSex != null ||
|
editBirthday != null ||
|
editSign != null ||
|
editPortrait != null;
|
}
|
|
String _getSexDesc(int? sex) {
|
switch (sex) {
|
case 1:
|
return "男";
|
case 2:
|
return "女";
|
}
|
return "未知";
|
}
|
|
String _getBirthDay() {
|
DateTime? date = (editBirthday ??
|
((_user!.birthday == null || _user!.birthday!.isEmpty)
|
? null
|
: parseDate(_user!.birthday!)));
|
if (date == null) {
|
return "请选择出生日期";
|
}
|
|
return "${date.year}/${date.month}/${date.day}";
|
}
|
|
DateTime parseDate(String date) {
|
List<String> sts = date.split("/");
|
|
return DateTime(int.parse(sts[0]), int.parse(sts[1]), int.parse(sts[2]));
|
}
|
|
Widget getCommonItemView(
|
{required String title,
|
String content = "",
|
required GestureTapCallback onClick,
|
bool canIn = true}) {
|
return Container(
|
height: 53,
|
margin: const EdgeInsets.fromLTRB(0, 0, 0, 1),
|
color: Colors.white,
|
child: InkWell(
|
onTap: () {
|
onClick();
|
},
|
child: Container(
|
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
|
child: Flex(
|
crossAxisAlignment: CrossAxisAlignment.center,
|
direction: Axis.horizontal,
|
children: [
|
Text(
|
title,
|
style:
|
const TextStyle(fontSize: 16, color: Color(0xFF333333)),
|
),
|
Expanded(
|
child: Flex(
|
direction: Axis.horizontal,
|
mainAxisAlignment: MainAxisAlignment.end,
|
children: [
|
Container(
|
child: Text(content,
|
style: const TextStyle(
|
fontSize: 14, color: Color(0xFF959595))),
|
margin: const EdgeInsets.fromLTRB(0, 0, 13.5, 0),
|
),
|
canIn
|
? Image.asset(
|
"assets/imgs/icon_person_info_input.png",
|
height: 13.5,
|
)
|
: Container()
|
],
|
))
|
],
|
)),
|
),
|
);
|
}
|
|
Widget getPortraitItemView({required GestureTapCallback onClick}) {
|
return Container(
|
height: 75,
|
margin: const EdgeInsets.fromLTRB(0, 0, 0, 1),
|
color: Colors.white,
|
child: InkWell(
|
onTap: () {
|
onClick();
|
},
|
child: Container(
|
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
|
child: Flex(
|
crossAxisAlignment: CrossAxisAlignment.center,
|
direction: Axis.horizontal,
|
children: [
|
const Text(
|
"头像",
|
style: TextStyle(fontSize: 16, color: Color(0xFF333333)),
|
),
|
Expanded(
|
child: Flex(
|
direction: Axis.horizontal,
|
mainAxisAlignment: MainAxisAlignment.end,
|
children: [
|
Container(
|
child: ClipRRect(
|
borderRadius: BorderRadius.circular(60),
|
child: editPortrait == null
|
? CommonImage(
|
_user!.portrait!,
|
height: 57,
|
width: 57,
|
)
|
: Image.file(
|
File(editPortrait!),
|
fit: BoxFit.cover,
|
height: 57,
|
width: 57,
|
)),
|
margin: const EdgeInsets.fromLTRB(0, 0, 13.5, 0),
|
),
|
Image.asset(
|
"assets/imgs/icon_person_info_input.png",
|
height: 13.5,
|
)
|
],
|
))
|
],
|
)),
|
),
|
);
|
}
|
|
Widget getSignItemView({required GestureTapCallback onClick}) {
|
return Container(
|
height: 200,
|
margin: const EdgeInsets.fromLTRB(0, 0, 0, 1),
|
color: Colors.white,
|
child: InkWell(
|
onTap: () {
|
onClick();
|
},
|
child: Container(
|
padding: const EdgeInsets.fromLTRB(20, 0, 20, 0),
|
child:
|
Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
|
Container(
|
height: 18,
|
),
|
Flex(
|
crossAxisAlignment: CrossAxisAlignment.center,
|
direction: Axis.horizontal,
|
children: [
|
const Text(
|
"个性签名",
|
style: TextStyle(fontSize: 16, color: Color(0xFF333333)),
|
),
|
Expanded(
|
child: Flex(
|
direction: Axis.horizontal,
|
mainAxisAlignment: MainAxisAlignment.end,
|
children: [
|
Image.asset(
|
"assets/imgs/icon_person_info_input.png",
|
height: 13.5,
|
)
|
],
|
))
|
],
|
),
|
Container(
|
height: 21,
|
),
|
Text(
|
_getSign(),
|
textAlign: TextAlign.start,
|
style: const TextStyle(color: Color(0xFFBEBEBE), fontSize: 15),
|
)
|
]),
|
),
|
));
|
}
|
|
_getSign() {
|
return editSign ??
|
((_user!.sign == null || _user!.sign!.isEmpty)
|
? "这个人很懒,什么也没有留下"
|
: _user!.sign!);
|
}
|
|
showAlertDilaog(String title, Widget content, VoidCallback sure,
|
{VoidCallback? cancel}) {
|
showCupertinoDialog(
|
context: context,
|
builder: (context) {
|
return AlertDialog(
|
title: Text(title),
|
content: content,
|
actions: <Widget>[
|
TextButton(
|
child: Text('取消'),
|
onPressed: () {
|
Navigator.of(context).pop();
|
},
|
),
|
TextButton(
|
child: Text('确定'),
|
onPressed: () {
|
sure();
|
},
|
),
|
],
|
);
|
});
|
}
|
|
void showEditNickName(String nickName) {
|
TextEditingController controller = TextEditingController();
|
controller.text = nickName;
|
showAlertDilaog(
|
"修改昵称",
|
TextField(
|
controller: controller,
|
), () {
|
if (controller.text.isEmpty) {
|
return;
|
}
|
Navigator.of(context).pop();
|
setState(() {
|
editNickName = controller.text;
|
});
|
});
|
}
|
|
void showEditSex(int sex) {
|
int? tempSex;
|
setState(() {
|
tempSex = sex;
|
});
|
DialogUtil.showDialogBottom(
|
context,
|
Container(
|
margin: const EdgeInsets.all(10),
|
alignment: Alignment.bottomCenter,
|
child: Stack(alignment: Alignment.bottomCenter, children: [
|
Container(
|
margin: const EdgeInsets.only(bottom: 50),
|
decoration: BoxDecoration(
|
color: Colors.white,
|
borderRadius: BorderRadius.circular(10)),
|
child: Container(
|
height: 100,
|
alignment: Alignment.center,
|
child: Column(
|
children: [
|
InkWell(
|
onTap: () {
|
setState(() {
|
editSex = 1;
|
});
|
Navigator.of(context).pop();
|
},
|
child: Container(
|
height: 50,
|
alignment: Alignment.center,
|
child: const Text(
|
"男",
|
style: TextStyle(
|
fontSize: 18,
|
color: ColorConstant.theme),
|
))),
|
InkWell(
|
onTap: () {
|
setState(() {
|
editSex = 2;
|
});
|
Navigator.of(context).pop();
|
},
|
child: Container(
|
height: 50,
|
alignment: Alignment.center,
|
child: const Text(
|
"女",
|
style: TextStyle(
|
fontSize: 18,
|
color: ColorConstant.theme),
|
))),
|
],
|
))),
|
MyFillButton(
|
"取消",
|
20,
|
height: 40,
|
color: Colors.grey,
|
fontSize: 14,
|
onClick: () {
|
popPage(context);
|
},
|
)
|
])));
|
}
|
|
//编辑生日
|
void showEditBirthDay(DateTime? dateTime) async {
|
dateTime ??= DateTime.now();
|
Locale myLocale = Localizations.localeOf(context);
|
DateTime? date = await showDatePicker(
|
context: context,
|
initialDate: dateTime,
|
firstDate: DateTime(1900),
|
lastDate: DateTime(DateTime.now().year + 1),
|
locale: myLocale);
|
if (date != null) {
|
setState(() {
|
editBirthday = date;
|
});
|
}
|
}
|
|
void showEditSign(String? sign) {
|
sign ??= "";
|
TextEditingController controller = TextEditingController();
|
controller.text = sign;
|
showAlertDilaog(
|
"编辑签名",
|
TextField(
|
controller: controller,
|
maxLines: 5,
|
maxLength: 200,
|
decoration: const InputDecoration(
|
focusedBorder: InputBorder.none,
|
border: InputBorder.none,
|
counterStyle: TextStyle(color: Color(0xFF666666)),
|
hintText: "请输入签名内容",
|
hintStyle: TextStyle(fontSize: 15, color: Color(0xFF999999))),
|
), () {
|
if (controller.text.isEmpty) {
|
return;
|
}
|
|
Navigator.of(context).pop();
|
setState(() {
|
editSign = controller.text;
|
});
|
});
|
}
|
}
|