login_page.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import 'package:flutter/material.dart';
  2. import 'dart:async';
  3. import 'package:shuqi/public.dart';
  4. import 'code_button.dart';
  5. class LoginPage extends StatefulWidget {
  6. @override
  7. State<StatefulWidget> createState() => LoginPageState();
  8. }
  9. class LoginPageState extends State {
  10. TextEditingController phoneEditer = TextEditingController();
  11. TextEditingController codeEditer = TextEditingController();
  12. int coldDownSeconds = 0;
  13. Timer? timer;
  14. fetchSmsCode() async {
  15. if (phoneEditer.text.length == 0) {
  16. return;
  17. }
  18. try {
  19. await Request.post(
  20. action: 'sms',
  21. params: {'phone': phoneEditer.text, 'type': 'login'},
  22. );
  23. setState(() {
  24. coldDownSeconds = 60;
  25. });
  26. coldDown();
  27. } catch (e) {
  28. Toast.show(e.toString());
  29. }
  30. }
  31. login() async {
  32. var phone = phoneEditer.text;
  33. var code = codeEditer.text;
  34. try {
  35. var response = await Request.post(action: 'login', params: {
  36. 'phone': phone,
  37. 'code': code,
  38. });
  39. UserManager.instance.login(response);
  40. Navigator.pop(context);
  41. } catch (e) {
  42. Toast.show(e.toString());
  43. }
  44. }
  45. @override
  46. void dispose() {
  47. if (timer != null) {
  48. timer!.cancel();
  49. }
  50. super.dispose();
  51. }
  52. coldDown() {
  53. timer = Timer(Duration(seconds: 1), () {
  54. setState(() {
  55. --coldDownSeconds;
  56. });
  57. coldDown();
  58. });
  59. }
  60. Widget buildPhone() {
  61. return Container(
  62. padding: EdgeInsets.symmetric(horizontal: 8),
  63. decoration: BoxDecoration(
  64. color: SQColor.paper,
  65. borderRadius: BorderRadius.circular(5),
  66. ),
  67. child: TextField(
  68. controller: phoneEditer,
  69. keyboardType: TextInputType.phone,
  70. style: TextStyle(fontSize: 14, color: SQColor.darkGray),
  71. decoration: InputDecoration(
  72. hintText: '请输入手机号',
  73. hintStyle: TextStyle(color: SQColor.gray),
  74. border: InputBorder.none,
  75. ),
  76. ),
  77. );
  78. }
  79. Widget buildCode() {
  80. return Container(
  81. padding: EdgeInsets.only(left: 8),
  82. decoration: BoxDecoration(
  83. color: SQColor.paper,
  84. borderRadius: BorderRadius.circular(5),
  85. ),
  86. child: Row(
  87. children: <Widget>[
  88. Flexible(
  89. child: TextField(
  90. controller: codeEditer,
  91. keyboardType: TextInputType.phone,
  92. style: TextStyle(fontSize: 14, color: SQColor.darkGray),
  93. decoration: InputDecoration(
  94. hintText: '请输入短信验证码',
  95. hintStyle: TextStyle(color: SQColor.gray),
  96. border: InputBorder.none,
  97. ),
  98. ),
  99. ),
  100. Container(color: Color(0xffdae3f2), width: 1, height: 40),
  101. CodeButton(
  102. onPressed: fetchSmsCode,
  103. coldDownSeconds: coldDownSeconds,
  104. ),
  105. ],
  106. ),
  107. );
  108. }
  109. Widget buildBody() {
  110. return Column(
  111. children: <Widget>[
  112. SizedBox(height: 20),
  113. Container(
  114. margin: EdgeInsets.symmetric(horizontal: 15),
  115. child: Column(
  116. crossAxisAlignment: CrossAxisAlignment.stretch,
  117. children: <Widget>[
  118. buildPhone(),
  119. SizedBox(height: 10),
  120. buildCode(),
  121. SizedBox(height: 10),
  122. Container(
  123. decoration: BoxDecoration(
  124. borderRadius: BorderRadius.circular(5),
  125. color: SQColor.primary,
  126. ),
  127. height: 40,
  128. child: TextButton(
  129. onPressed: login,
  130. child: Text(
  131. '登录',
  132. style: TextStyle(fontSize: 16, color: Color(0xffffffff)),
  133. ),
  134. ),
  135. ),
  136. ],
  137. ),
  138. )
  139. ],
  140. );
  141. }
  142. @override
  143. Widget build(BuildContext context) {
  144. return Scaffold(
  145. appBar: AppBar(title: Text('登录'), elevation: 0),
  146. backgroundColor: Colors.white,
  147. body: buildBody(),
  148. );
  149. }
  150. }