setting.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import 'package:flutter/material.dart';
  2. import "package:image_picker/image_picker.dart";
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:dio/dio.dart';
  5. import '../styles/colors.dart';
  6. import '../styles/api.dart';
  7. import 'dart:io';
  8. import 'dart:async';
  9. import 'dart:typed_data';
  10. import 'dart:convert';
  11. import 'dart:ui';
  12. import '../styles/totast.dart';
  13. class Setting extends StatefulWidget {
  14. @override
  15. SettingState createState() => SettingState();
  16. }
  17. class SettingState extends State<Setting> {
  18. File _image;
  19. Map userInfo = {};
  20. Future getImage() async {
  21. var image = await ImagePicker.pickImage(source: ImageSource.gallery);
  22. image.readAsBytes().then((bytes) async {
  23. String encoded1 = 'data:image/jpeg;base64,' + base64Encode(bytes);
  24. print(encoded1);
  25. FormData formData = new FormData.from({"base64": encoded1});
  26. Toast.show(context, '加载中', -1, 'loading');
  27. final response =
  28. await Dio().post(domain + 'assets/uploadImg', data: formData);
  29. final res = json.decode(response.toString());
  30. if (res['success']) {
  31. updateUserInfo(res['data'], 'icon');
  32. }
  33. });
  34. setState(() {
  35. _image = image;
  36. });
  37. }
  38. @override
  39. void initState() {
  40. // TODO: implement initState
  41. super.initState();
  42. getUserInfo(false);
  43. }
  44. @override
  45. Widget build(BuildContext context) {
  46. ScreenUtil.instance = ScreenUtil(width: 375, height: 667)..init(context);
  47. // TODO: implement build
  48. return new WillPopScope(
  49. child: Scaffold(
  50. appBar: AppBar(
  51. backgroundColor: PRIMARY_COLOR,
  52. title: Text('系统设置'),
  53. centerTitle: true,
  54. elevation: 0,
  55. ),
  56. body: RefreshIndicator(
  57. color: PRIMARY_COLOR,
  58. backgroundColor: BG_SUB_COLOR,
  59. displacement: 10,
  60. onRefresh: () async {
  61. await new Future.delayed(const Duration(seconds: 1));
  62. getUserInfo(false);
  63. },
  64. child: SingleChildScrollView(
  65. physics: AlwaysScrollableScrollPhysics(),
  66. child: ConstrainedBox(
  67. constraints: BoxConstraints(
  68. minHeight: window.physicalSize.height /
  69. window.devicePixelRatio -
  70. 56 -
  71. MediaQueryData.fromWindow(window).padding.top),
  72. child: Container(
  73. color: BG_COLOR,
  74. child: Column(
  75. children: <Widget>[
  76. Container(
  77. color: BG_SUB_COLOR,
  78. padding: EdgeInsets.only(top: 10),
  79. child: Column(
  80. children: <Widget>[_settingItem('头像')],
  81. ),
  82. )
  83. ],
  84. ),
  85. ),
  86. ))),
  87. floatingActionButton: FloatingActionButton(
  88. onPressed: () {
  89. print('点击照片');
  90. Toast.show(context, '加载中', -1, 'loading');
  91. },
  92. tooltip: 'Pick Image',
  93. child: Icon(Icons.add_a_photo),
  94. )),
  95. onWillPop: () {
  96. Toast.hide();
  97. print("返回键点击了");
  98. Navigator.pop(context);
  99. });
  100. }
  101. Widget _settingItem(name) {
  102. return Container(
  103. padding: EdgeInsets.only(left: 15, right: 15),
  104. height: 60,
  105. color: BG_COLOR,
  106. child: GestureDetector(
  107. child: Row(
  108. children: <Widget>[
  109. Text(name,
  110. style: TextStyle(
  111. color: Colors.white,
  112. fontSize: 14,
  113. fontWeight: FontWeight.w400)),
  114. Expanded(flex: 1, child: _centerWidget('头像')),
  115. Image.asset(
  116. 'images/icon_inter.png',
  117. width: 24,
  118. )
  119. ],
  120. ),
  121. onTap: () {
  122. if (name == '头像') {
  123. getImage();
  124. }
  125. },
  126. ),
  127. );
  128. }
  129. Widget _centerWidget(name) {
  130. Widget _wiget;
  131. print(name);
  132. if (name == '头像') {
  133. _wiget = UnconstrainedBox(
  134. alignment: Alignment.centerRight,
  135. child: SizedBox(
  136. width: 36,
  137. height: 36,
  138. child: CircleAvatar(
  139. backgroundImage: NetworkImage(
  140. userInfo.containsKey('icon') ? userInfo['icon'] : '')),
  141. ),
  142. );
  143. }
  144. return _wiget;
  145. }
  146. void updateUserInfo(value, key) async {
  147. print(value);
  148. print(key);
  149. FormData formdata =
  150. new FormData.from({key: value, "id": isDebug ? debugID : ''});
  151. print(formdata);
  152. final response =
  153. await Dio().post(domain + 'userInfo/update', data: formdata);
  154. print(response);
  155. final res = json.decode(response.toString());
  156. if (res['success']) {
  157. Toast.hide();
  158. getUserInfo(false);
  159. Toast.show(context, '修改成功', 1500, 'success');
  160. }
  161. }
  162. void getUserInfo(isFreash) async {
  163. Future.delayed(Duration(seconds: 1000), () {
  164. if (!userInfo.containsKey('nickname')) {
  165. Toast.show(context, '加载中', -1, 'loading');
  166. isFreash = true;
  167. }
  168. });
  169. final response = await Dio().get(domain + 'userInfo/getUserInfo',
  170. data: {"id": isDebug ? debugID : ''});
  171. final res = json.decode(response.toString());
  172. if (isFreash) {
  173. Toast.hide();
  174. }
  175. if (res['success']) {
  176. print(res['data']);
  177. setState(() {
  178. userInfo = res['data'];
  179. });
  180. }
  181. }
  182. }