Hibok
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

494 wiersze
14 KiB

  1. import 'dart:async';
  2. import 'package:chat/data/UserData.dart';
  3. import 'package:chat/data/WebData.dart';
  4. import 'package:chat/data/constants.dart';
  5. import 'package:chat/generated/i18n.dart';
  6. import 'package:chat/utils/CustomUI.dart';
  7. import 'package:chat/utils/HttpUtil.dart';
  8. import 'package:chat/utils/TokenMgr.dart';
  9. import 'package:chat/utils/screen.dart';
  10. import 'package:dio/dio.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:flutter/services.dart';
  13. import 'package:wifi_info_plugin/wifi_info_plugin.dart';
  14. import 'package:oktoast/oktoast.dart';
  15. import '../utils/OtherLogin.dart';
  16. const RATE_NUM = 0.82;
  17. const BlueColor = Constants.BlueTextColor;
  18. enum PageType {
  19. register,
  20. changePWD,
  21. bindPhone,
  22. }
  23. class RegisterPage extends StatefulWidget {
  24. @required
  25. final int type;
  26. RegisterPage({Key key, this.type});
  27. _RegisterPageState createState() => _RegisterPageState();
  28. }
  29. class _RegisterPageState extends State<RegisterPage> {
  30. String _phoneNumber = '';
  31. String _selectType = UserData().language == LanguageType.Vietnamese
  32. ? phone.keys.toList()[1]
  33. : phone.keys.toList()[0];
  34. String _verifyCode = '';
  35. String _passWorld = '';
  36. // String _rePassWorld = '';
  37. bool isChangePWD = false;
  38. int _seconds = 0;
  39. String _verifyStr = '';
  40. Timer _timer;
  41. @override
  42. void initState() {
  43. super.initState();
  44. isChangePWD = widget.type == PageType.changePWD.index;
  45. }
  46. @override
  47. void dispose() {
  48. _cancelTimer();
  49. super.dispose();
  50. }
  51. _startTimer() {
  52. _seconds = 60 * 5;
  53. _timer = new Timer.periodic(new Duration(seconds: 1), (timer) {
  54. _seconds--;
  55. _verifyStr = '$_seconds(s)';
  56. setState(() {});
  57. if (_seconds == 0) {
  58. _verifyStr = I18n.of(context).send_again;
  59. _cancelTimer();
  60. }
  61. });
  62. }
  63. _cancelTimer() {
  64. _timer?.cancel();
  65. }
  66. @override
  67. Widget build(BuildContext context) {
  68. String title = I18n.of(context).number_registration;
  69. if (widget.type == PageType.changePWD.index) {
  70. title = I18n.of(context).change_password;
  71. } else if (widget.type == PageType.bindPhone.index) {
  72. title = I18n.of(context).bind_phone1;
  73. }
  74. Widget appBar = new AppBar(
  75. backgroundColor: AppColors.NewAppbarBgColor,
  76. title: new Text(
  77. title,
  78. style: TextStyle(color: AppColors.NewAppbarTextColor),
  79. textScaleFactor: 1.0,
  80. ),
  81. leading: CustomUI.buildCustomLeading(context),
  82. centerTitle: true,
  83. );
  84. return Scaffold(
  85. appBar: appBar,
  86. body: SafeArea(
  87. child: Center(
  88. child: Container(
  89. height: MediaQuery.of(context).size.height,
  90. width: MediaQuery.of(context).size.width,
  91. child: _buildBody(),
  92. ),
  93. )),
  94. resizeToAvoidBottomPadding: false,
  95. );
  96. }
  97. Widget _buildBody() {
  98. return new Column(
  99. children: <Widget>[
  100. _buildAcountInput(),
  101. _buildSecurityInput(),
  102. _buildPasswordInput(),
  103. _buildLoginButton(),
  104. widget.type == PageType.register.index
  105. ? _buildOtherLogin()
  106. : Container(),
  107. ],
  108. );
  109. }
  110. //构建底部第三方登陆
  111. Widget _buildOtherLogin() {
  112. return new Expanded(
  113. child: Column(
  114. mainAxisAlignment: MainAxisAlignment.end,
  115. children: <Widget>[
  116. new Container(
  117. alignment: Alignment.bottomCenter,
  118. child: OtherLogin().builder(context),
  119. )
  120. ],
  121. ),
  122. );
  123. }
  124. //账号输入框
  125. Widget _buildAcountInput() {
  126. double height = 54.5;
  127. return new Container(
  128. alignment: Alignment.center,
  129. margin: EdgeInsets.only(top: 8.5),
  130. height: height,
  131. padding: EdgeInsets.only(left: 17),
  132. decoration: BoxDecoration(
  133. color: Colors.white,
  134. border: Border(
  135. top: Constants.GreyBorderSide,
  136. bottom: Constants.GreyBorderSide)),
  137. child: Stack(
  138. children: <Widget>[
  139. Container(
  140. width: Screen.width,
  141. height: height,
  142. alignment: Alignment.centerLeft,
  143. child: Icon(
  144. Icons.phone_android,
  145. color: BlueColor,
  146. size: Constants.TextFieldIconSize,
  147. ),
  148. ),
  149. Positioned(
  150. left: 46,
  151. child: Container(
  152. padding: EdgeInsets.only(top: 5),
  153. margin: EdgeInsets.all(0),
  154. alignment: Alignment.centerLeft,
  155. child: new DropdownButtonHideUnderline(
  156. child: new DropdownButton(
  157. items: phone.keys.map((key) {
  158. return DropdownMenuItem(
  159. child: new Text(key, textScaleFactor: 1.0),
  160. value: key,
  161. );
  162. }).toList(),
  163. onChanged: (value) {
  164. setState(() {
  165. _selectType = value;
  166. });
  167. },
  168. value: _selectType,
  169. elevation: 24, //设置阴影的高度
  170. style: new TextStyle(
  171. //设置文本框里面文字的样式
  172. color: Constants.BlackTextColor,
  173. fontSize: 12,
  174. ),
  175. iconSize: 25.0,
  176. )),
  177. ),
  178. ),
  179. Positioned(
  180. left: 90,
  181. child: Container(
  182. alignment: Alignment.center,
  183. width: Screen.width - 90,
  184. height: height,
  185. child: TextField(
  186. keyboardAppearance: Brightness.light,
  187. decoration: new InputDecoration(
  188. hintText: I18n.of(context).enter_number,
  189. hintStyle: TextStyle(fontSize: 14),
  190. border: InputBorder.none,
  191. ),
  192. maxLines: 1,
  193. style: TextStyle(textBaseline: TextBaseline.alphabetic),
  194. inputFormatters: [
  195. WhitelistingTextInputFormatter.digitsOnly,
  196. ],
  197. onChanged: (str) {
  198. _phoneNumber = str;
  199. setState(() {});
  200. },
  201. keyboardType: TextInputType.phone,
  202. ),
  203. ),
  204. )
  205. ],
  206. ));
  207. }
  208. //验证码
  209. Widget _buildSecurityInput() {
  210. Widget verifyCodeEdit = new TextField(
  211. keyboardAppearance: Brightness.light,
  212. decoration: new InputDecoration(
  213. hintText: I18n.of(context).enter_code,
  214. hintStyle: TextStyle(fontSize: 14),
  215. icon: new Icon(
  216. Icons.security,
  217. color: BlueColor,
  218. size: Constants.TextFieldIconSize,
  219. ),
  220. border: InputBorder.none,
  221. ),
  222. maxLines: 1,
  223. keyboardType: TextInputType.number,
  224. style: TextStyle(textBaseline: TextBaseline.alphabetic),
  225. inputFormatters: [
  226. WhitelistingTextInputFormatter.digitsOnly,
  227. LengthLimitingTextInputFormatter(6)
  228. ],
  229. onChanged: (str) {
  230. _verifyCode = str;
  231. setState(() {});
  232. },
  233. );
  234. void getSms() async {
  235. var data = {
  236. "mobile": phone[_selectType] + _phoneNumber,
  237. "type": 0,
  238. "language": UserData().language,
  239. };
  240. data['sign'] = TokenMgr().getSign(data);
  241. try {
  242. Response res = await HttpUtil()
  243. .post('user/send/sms', data: data, isShowLoading: true);
  244. var resData = res.data;
  245. if (resData['code'] == 0) {
  246. setState(() {
  247. _startTimer();
  248. });
  249. } else {
  250. showToast(resData['msg']);
  251. }
  252. } catch (e) {}
  253. }
  254. Widget verifyCodeBtn = new InkWell(
  255. onTap: (_seconds == 0)
  256. ? () {
  257. if (_phoneNumber == null || _phoneNumber == "") {
  258. showToast(I18n.of(context).enter_number);
  259. return;
  260. }
  261. getSms();
  262. }
  263. : null,
  264. child: new Container(
  265. margin: EdgeInsets.only(right: 21.5),
  266. padding: EdgeInsets.symmetric(vertical: 4, horizontal: 6),
  267. decoration: BoxDecoration(
  268. border: Border.all(color: BlueColor),
  269. borderRadius: BorderRadius.all(Radius.circular(15))),
  270. alignment: Alignment.center,
  271. child: new Text(
  272. '${_verifyStr == '' ? I18n.of(context).send_code : _verifyStr}',
  273. textScaleFactor: 1.0,
  274. style: new TextStyle(fontSize: 11.34, color: BlueColor),
  275. ),
  276. ),
  277. );
  278. return new Container(
  279. alignment: Alignment.center,
  280. height: 54.5,
  281. padding: EdgeInsets.only(left: 17),
  282. decoration: BoxDecoration(
  283. color: Colors.white,
  284. border: Border(bottom: Constants.GreyBorderSide)),
  285. child: new Stack(
  286. alignment: Alignment.center,
  287. children: <Widget>[
  288. verifyCodeEdit,
  289. Positioned(
  290. right: 0,
  291. child: verifyCodeBtn,
  292. )
  293. ],
  294. ),
  295. );
  296. }
  297. //密码
  298. Widget _buildPasswordInput() {
  299. return new Container(
  300. alignment: Alignment.center,
  301. height: 54.5,
  302. padding: EdgeInsets.only(left: 17),
  303. decoration: BoxDecoration(
  304. color: Colors.white,
  305. border: Border(bottom: Constants.GreyBorderSide)),
  306. child: new TextField(
  307. keyboardAppearance: Brightness.light,
  308. decoration: new InputDecoration(
  309. hintText: I18n.of(context).set_password2,
  310. hintStyle: TextStyle(fontSize: 14),
  311. icon: new Icon(
  312. Icons.lock,
  313. color: BlueColor,
  314. size: Constants.TextFieldIconSize,
  315. ),
  316. border: InputBorder.none,
  317. ),
  318. maxLines: 1,
  319. inputFormatters: [LengthLimitingTextInputFormatter(20)],
  320. obscureText: true,
  321. style: TextStyle(textBaseline: TextBaseline.alphabetic),
  322. onChanged: (str) {
  323. _passWorld = str;
  324. setState(() {});
  325. },
  326. ),
  327. );
  328. }
  329. // //密码
  330. // Widget _buildRePasswordInput() {
  331. // return new Container(
  332. // child: new TextField(
  333. // decoration: new InputDecoration(
  334. // hintText: '请重新输入登陆密码',
  335. // icon: new Icon(Icons.lock),
  336. // border: InputBorder.none,
  337. // ),
  338. // maxLines: 1,
  339. // obscureText: true,
  340. // onChanged: (str) {
  341. // _rePassWorld = str;
  342. // setState(() {});
  343. // },
  344. // ),
  345. // width: MediaQuery.of(context).size.width * RATE_NUM,
  346. // );
  347. // }
  348. //下一步按钮
  349. Widget _buildLoginButton() {
  350. Text text = new Text(I18n.of(context).submit,
  351. textScaleFactor: 1.0,
  352. style: TextStyle(
  353. fontSize: Constants.ShaderButtonFontSize, color: Colors.white));
  354. void postRegister(phoneNum) async {
  355. var type = 0;
  356. if (widget.type == PageType.register.index) {
  357. type = 0;
  358. } else if (widget.type == PageType.bindPhone.index) {
  359. type = 1;
  360. } else {
  361. return;
  362. }
  363. var data = {
  364. "mobile": phoneNum,
  365. "code": _verifyCode,
  366. "pwd": _passWorld,
  367. "cPwd": _passWorld,
  368. "type": type,
  369. "language": UserData().language,
  370. };
  371. data['sign'] = TokenMgr().getSign(data);
  372. data['userid'] = UserData().basicInfo.userId;
  373. data['lng'] = UserData().longitude;
  374. data['lat'] = UserData().latitude;
  375. try {
  376. WifiInfoWrapper wifiObject = await WifiInfoPlugin.wifiDetails;
  377. if (wifiObject != null) {
  378. data['routerName'] = wifiObject.ssid;
  379. data['mac'] = wifiObject.bssId;
  380. }
  381. } catch (e) {
  382. print(e);
  383. }
  384. try {
  385. Response res = await HttpUtil()
  386. .post('user/binding/mobile', data: data, isShowLoading: true);
  387. var resData = res.data;
  388. showToast(resData['msg']);
  389. if (resData['code'] == 0) {
  390. UserData().mobile = int.parse(phoneNum);
  391. if (widget.type == PageType.register.index) {
  392. HttpUtil().login(phoneNum, _passWorld, context);
  393. }
  394. if (widget.type == PageType.bindPhone.index) {
  395. Navigator.of(context).pop();
  396. }
  397. }
  398. } catch (e) {}
  399. }
  400. void postChangePwd(phoneNum) async {
  401. var data = {
  402. "mobile": phoneNum,
  403. "code": _verifyCode,
  404. "newpassword": _passWorld,
  405. "confirmPassword": _passWorld,
  406. };
  407. data['sign'] = TokenMgr().getSign(data);
  408. try {
  409. Response res = await HttpUtil()
  410. .post('password/setting/password', data: data, isShowLoading: true);
  411. var resData = res.data;
  412. showToast(resData['msg']);
  413. if (resData['code'] == 0) {
  414. Navigator.of(context).pop();
  415. } else if (resData['code'] == -7) {
  416. showToast(I18n.of(context).not_register);
  417. }
  418. } catch (e) {}
  419. }
  420. callback() {
  421. if (_phoneNumber == null || _phoneNumber == "") {
  422. showToast(I18n.of(context).enter_number);
  423. return;
  424. }
  425. if (_verifyCode == null || _verifyCode == "" || _verifyCode.length != 6) {
  426. showToast(I18n.of(context).enter_code);
  427. return;
  428. }
  429. if (_passWorld == null || _passWorld == "") {
  430. showToast(I18n.of(context).enter_password);
  431. return;
  432. }
  433. if (_passWorld.length < 6) {
  434. showToast(I18n.of(context).password_limit);
  435. return;
  436. }
  437. var phoneNum = phone[_selectType] + _phoneNumber;
  438. !isChangePWD ? postRegister(phoneNum) : postChangePwd(phoneNum);
  439. }
  440. return InkWell(
  441. onTap: callback,
  442. child: Container(
  443. margin: EdgeInsets.only(top: 60),
  444. alignment: Alignment.center,
  445. height: Constants.ShaderButtonHeight,
  446. width: Screen.width * RATE_NUM,
  447. decoration: BoxDecoration(
  448. color: Constants.ConfrimButtonColor,
  449. border:
  450. Border.all(color: AppColors.NewAppbarTextColor.withAlpha(140)),
  451. borderRadius:
  452. BorderRadius.all(Radius.circular(Constants.BigButtonRadius))),
  453. child: text,
  454. ),
  455. );
  456. }
  457. }