YSUserInfo.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import 'dart:io';
  2. import 'package:city_pickers/city_pickers.dart';
  3. import 'package:city_pickers/modal/result.dart';
  4. import 'package:flutter/cupertino.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:qiniu_flutter_sdk/qiniu_flutter_sdk.dart';
  7. import 'package:shared_preferences/shared_preferences.dart';
  8. import 'package:ysairplane2/base/YSBase.dart';
  9. import 'package:ysairplane2/code/YSCity.dart';
  10. import 'package:ysairplane2/tools/YSNetWorking.dart';
  11. import 'package:ysairplane2/tools/YSTools.dart';
  12. import 'package:image_picker/image_picker.dart';
  13. class YSUserInfo extends StatefulWidget {
  14. @override
  15. _YSUserInfoState createState() => _YSUserInfoState();
  16. }
  17. class _YSUserInfoState extends State<YSUserInfo> {//,{'title':'绑定微信','content':''},{'title':'绑定支付宝','content':''},
  18. ImagePicker _picker = ImagePicker();
  19. //{'title':'公司名称','content':'请输入公司名称'},{'title':'公司职务','content':'请输入职务'},
  20. // {'title':'公司行业','content':'请输入行业'},{'title':'网址','content':'请输入网址'},{'title':'所在地区','content':'请选择公司所在地区'}
  21. List array = [{'title':'头像','content':''},{'title':'真实姓名','content':'请输入真实姓名'},{'title':'性别','content':'请选择性别'},
  22. {'title':'地区','content':'请选择地区'},{'title':'登录手机','content':''},
  23. {'title':'邮箱地址','content':'请输入邮箱地址'}];
  24. List chooses = ['男','女'];
  25. String _tokenStr = '';
  26. String _urlStr = '';
  27. String _phone = '';
  28. String _name = '';
  29. String _email = '';
  30. String _city = '';
  31. String _avatar = '';
  32. TextEditingController _field = TextEditingController();
  33. @override
  34. void initState() {
  35. Future.delayed(Duration(seconds: 0)).then((value){
  36. _getUserInfoData();
  37. _getQiNiuTokenData();
  38. });
  39. super.initState();
  40. }
  41. @override
  42. void dispose() {
  43. _field.dispose();
  44. super.dispose();
  45. }
  46. @override
  47. Widget build(BuildContext context) {
  48. return YSBase(
  49. ystitle: '个人资料',
  50. yschild: Container(
  51. height: MediaQuery.of(context).size.height-MediaQuery.of(context).padding.top-44,
  52. width: MediaQuery.of(context).size.width,
  53. color: Color(0xFFF5F6F8),
  54. child: ListView.separated(
  55. itemBuilder: (context,index){
  56. return GestureDetector(
  57. onTap: () async{
  58. if(index==0){
  59. showCupertinoModalPopup(
  60. context: context,
  61. builder: (context) {
  62. return CupertinoActionSheet(
  63. actions: <Widget>[
  64. CupertinoActionSheetAction(
  65. child: Text('拍摄照片'),
  66. onPressed: () {
  67. Navigator.pop(context);
  68. _picker.getImage(source: ImageSource.camera,imageQuality: 50).then((value){
  69. _onUpload(value);
  70. });
  71. },
  72. ),
  73. CupertinoActionSheetAction(
  74. child: Text('选择图片'),
  75. onPressed: () {
  76. Navigator.pop(context);
  77. _picker.getImage(source: ImageSource.gallery,imageQuality: 50).then((value){
  78. _onUpload(value);
  79. });
  80. },
  81. ),
  82. ],
  83. cancelButton: CupertinoActionSheetAction(
  84. child: Text('取消'),
  85. onPressed: () {
  86. Navigator.pop(context);
  87. },
  88. ),
  89. );
  90. }
  91. );
  92. }else if(index==6||index==7||index==3||index==12){
  93. if(index==3||index==12){
  94. FocusScope.of(context).unfocus();
  95. Result result = await CityPickers.showCityPicker(
  96. context: context,
  97. );
  98. print(result);
  99. if(result!=null){
  100. _city = result.provinceName+' '+result.cityName+' '+result.areaName;
  101. _postEditUserData({'region':_city});
  102. }
  103. }else{
  104. }
  105. }else if(index==1||index==5||index==8||index==9||index==10||index==11){
  106. _field.text = '';
  107. if(index==1){
  108. _field.text = _name;
  109. }else if(index==5){
  110. _field.text = _email;
  111. }
  112. showModalBottomSheet(
  113. isScrollControlled: true,
  114. barrierColor: Colors.black26,
  115. backgroundColor: Colors.transparent,
  116. context: context,
  117. builder: (context){
  118. return SingleChildScrollView(
  119. padding: EdgeInsets.only(bottom: MediaQuery.of(context).viewInsets.bottom),
  120. child: Container(
  121. decoration: BoxDecoration(
  122. color: Colors.white,
  123. borderRadius: BorderRadius.all(Radius.circular(10))
  124. ),
  125. child: Column(
  126. children: [
  127. Container(
  128. padding: EdgeInsets.only(top: 10,bottom: 10,left: 10,right: 10),
  129. child: Row(
  130. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  131. children: [
  132. Text(index==1?'真实姓名':index==5?'邮箱地址':index==8?'公司名称':index==9?'公司职务':index==10?'公司行业':'网址',
  133. style: TextStyle(fontSize: 14,color: Color(0xFF000000),decoration: TextDecoration.none,fontWeight: FontWeight.bold),),
  134. GestureDetector(
  135. onTap: (){Navigator.pop(context);},
  136. child: Icon(Icons.close,size: 20,color: Color(0xFF9A9A9A),),
  137. )
  138. ],
  139. ),
  140. ),
  141. Container(
  142. height: 50,
  143. width: MediaQuery.of(context).size.width-30,
  144. child: CupertinoTextField(
  145. placeholder: '请输入',
  146. style: TextStyle(fontSize: 15,color: Color(0xFF9A9A9A),decoration: TextDecoration.none,fontWeight: FontWeight.normal,),
  147. placeholderStyle: TextStyle(fontSize: 15,color: Color(0xFF9A9A9A),decoration: TextDecoration.none,fontWeight: FontWeight.normal),
  148. autofocus: true,
  149. decoration: BoxDecoration(),
  150. maxLines: 10,
  151. controller: _field,
  152. keyboardType: index==5?TextInputType.emailAddress:TextInputType.text,
  153. ),
  154. decoration: BoxDecoration(
  155. border: Border(bottom: BorderSide(color: Color(0xFFF5F6F8),width: 0.5))
  156. ),
  157. margin: EdgeInsets.only(top: 10),
  158. ),
  159. GestureDetector(
  160. onTap: (){
  161. if(_field.text.isEmpty)return;
  162. Navigator.pop(context);
  163. if(index==1){
  164. _postEditUserData({'nickname':_field.text});
  165. _name = _field.text;
  166. }else if(index==5){
  167. if(isEmail(_field.text)==false){
  168. ysFlutterToast(context, '邮箱格式错误');
  169. return;
  170. }
  171. _postEditUserData({'mail':_field.text});
  172. _email = _field.text;
  173. }
  174. },
  175. child: Container(
  176. margin: EdgeInsets.all(15),
  177. height: 40,
  178. width: MediaQuery.of(context).size.width-40,
  179. alignment: Alignment.center,
  180. decoration: BoxDecoration(
  181. color: Color(0xFF007AFF),
  182. borderRadius: BorderRadius.all(Radius.circular(6))
  183. ),
  184. child: Text('完成',style: TextStyle(fontSize: 17,color: Colors.white,fontWeight: FontWeight.bold,decoration: TextDecoration.none),),
  185. ),
  186. )
  187. ],
  188. ),
  189. ),
  190. );
  191. }
  192. );
  193. }else{
  194. }
  195. },
  196. child: Container(
  197. height: 50,
  198. color: Colors.white,
  199. padding: EdgeInsets.only(left: 15,right: 15),
  200. child: Row(
  201. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  202. children: [
  203. Text('${array[index]['title']}',style: TextStyle(fontSize: 16,color: Color(0xFF000000)),),
  204. index==0?Container(
  205. height: 40,
  206. width: 40,
  207. decoration: BoxDecoration(
  208. borderRadius: BorderRadius.all(Radius.circular(20)),
  209. color: Colors.black12
  210. ),
  211. child: _avatar.isNotEmpty?ClipRRect(
  212. borderRadius: BorderRadius.all(Radius.circular(20)),
  213. child: ysImageLoad(
  214. imageUrl: _avatar,
  215. fit: BoxFit.fill,
  216. height: 40,
  217. width: 40,
  218. ),
  219. ):Container(),
  220. ):index==6||index==7||index==3||index==12?Row(
  221. children: [
  222. Text(index==3&&_city.isNotEmpty?_city:'${array[index]['content']}',style: TextStyle(fontSize: 16,color: Color(0xFF9A9A9A)),),
  223. Icon(Icons.keyboard_arrow_right,size: 20,color: Color(0xFF9A9A9A),)
  224. ],
  225. ):index==1||index==4||index==5||index==8||index==9||index==10||index==11?Text(
  226. index==1&&_name.isNotEmpty?_name:index==5&&_email.isNotEmpty?_email:
  227. index==4&&_phone.isNotEmpty?_phone:'${array[index]['content']}',
  228. style: TextStyle(fontSize: 16,color: Color(0xFF9A9A9A)),
  229. ):DropdownButton(
  230. value: '${chooses[0]}',
  231. items: [
  232. for(int i=0;i<chooses.length;i++)DropdownMenuItem(child: Text('${chooses[i]}'),value: '${chooses[i]}',),
  233. ],
  234. onChanged: (value){
  235. },
  236. underline: Container(),
  237. icon: Icon(Icons.keyboard_arrow_down,size: 20,color: Color(0xFF999999),),
  238. style: TextStyle(fontSize: 15,color: Color(0xFF999999)),
  239. )
  240. ],
  241. ),
  242. ),
  243. );
  244. },
  245. separatorBuilder: (context,index){
  246. return Container(
  247. color: Color(0xFFF5F6F8),
  248. height: index==3||index==7?10:0.5,
  249. );
  250. },
  251. itemCount: array.length,
  252. padding: EdgeInsets.only(top: 10,bottom: 30),
  253. ),
  254. ),
  255. );
  256. }
  257. _postEditUserData(var request) async{
  258. Map dict = await ysRequestHttp(context,type: requestType.post,api: '/app/applets/center/info/update',parameter: request,isLoading: false,
  259. isToken: true,refresh: (){_postEditUserData(request);});
  260. if(dict!=null){
  261. setState(() {});
  262. }
  263. }
  264. _onUpload(var value) async {
  265. if (value == null) {
  266. return;
  267. }
  268. print(value.path);
  269. print(_tokenStr);
  270. Storage storage = Storage();
  271. storage.putFile(
  272. File(value.path),
  273. _tokenStr,
  274. options: PutOptions(
  275. key: DateTime.now().millisecondsSinceEpoch.toString() +'.' +value.path.split('.').last,
  276. ),
  277. ).then((result) async{
  278. _postEditUserData({'avatar':_urlStr+'${result.key}'});
  279. });
  280. // final syStorage = new SyFlutterQiniuStorage();
  281. // //监听上传进度
  282. // syStorage.onChanged().listen((dynamic percent) {
  283. // print(percent);
  284. // });
  285. // //上传文件
  286. // var result = await syStorage.upload(value.path, _tokenStr, DateTime.now().millisecondsSinceEpoch.toString() +'.' +value.path.split('.').last);
  287. // if(result.success==true){
  288. // _postEditUserData({'avatar':_urlStr+'${result.result['key']}'});
  289. // }
  290. // print(result);
  291. }
  292. _getQiNiuTokenData() async{
  293. SharedPreferences prefer = await SharedPreferences.getInstance();
  294. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/getToken',parameter: {},isLoading: false,isToken: false);
  295. if(dict!=null){
  296. _avatar = prefer.getString('avatar');
  297. _tokenStr = dict['token'];
  298. _urlStr = dict['prefix'];
  299. setState(() {
  300. });
  301. }
  302. }
  303. _getUserInfoData() async{
  304. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/center/info',parameter: {},isLoading: false,isToken: true);
  305. if(dict!=null){
  306. _phone = dict['data']['phoneNumber'];
  307. _name = dict['data']['nickname'];
  308. _email = dict['data']['mail']??'';
  309. }
  310. }
  311. }