phone_verify_widget.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import 'package:flutter/material.dart';
  2. import 'package:lszlgl/base/base_lifecycle_state.dart';
  3. import 'package:lszlgl/main.dart';
  4. import 'package:lszlgl/network/my_api.dart';
  5. import 'package:lszlgl/widget/button.dart';
  6. import 'package:pin_code_fields/pin_code_fields.dart';
  7. class PhoneVerifyWidget extends StatefulWidget {
  8. final String phone;
  9. const PhoneVerifyWidget({super.key, required this.phone});
  10. @override
  11. State<PhoneVerifyWidget> createState() => _PhoneVerifyWidgetState();
  12. }
  13. class _PhoneVerifyWidgetState extends State<PhoneVerifyWidget> {
  14. String currentText = "";
  15. String starPhone = '';
  16. /// 获取验证码
  17. Future<bool> getPhoneMsg() async {
  18. try {
  19. var res = await MyApi.get().loginWithPhoneMsg({
  20. 'mobile': widget.phone,
  21. 'scene': 21,
  22. });
  23. //print('$res.data');
  24. if (res.data ?? false) {
  25. MyNavigator.showToast('验证码已发送');
  26. return true;
  27. } else {
  28. MyNavigator.showToast('验证码发送失败');
  29. return false;
  30. }
  31. } catch (e) {
  32. logger.e(e);
  33. return false;
  34. }
  35. }
  36. @override
  37. void initState() {
  38. super.initState();
  39. getPhoneMsg();
  40. starPhone = widget.phone.replaceFirst(RegExp(r'\d{4}'), '****', 3);
  41. }
  42. @override
  43. Widget build(BuildContext context) {
  44. return PopScope(
  45. canPop: false,
  46. onPopInvoked: (bool didPop) {
  47. if (didPop) {
  48. return;
  49. } else {
  50. Navigator.of(context).pop(false);
  51. }
  52. },
  53. child: Dialog(
  54. insetPadding: const EdgeInsets.symmetric(horizontal: 22),
  55. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
  56. child: Container(
  57. padding: const EdgeInsets.fromLTRB(18, 24, 18, 18),
  58. child: Column(
  59. mainAxisSize: MainAxisSize.min,
  60. children: [
  61. const Text('短信验证码',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16),),
  62. const SizedBox(height: 8),
  63. Text('已向$starPhone发送短信验证码', style: const TextStyle(color: Colors.grey),),
  64. const SizedBox(height: 22),
  65. PinCodeTextField(
  66. length: 6,
  67. appContext: context,
  68. autoFocus: true,
  69. keyboardType: TextInputType.number,
  70. cursorColor: Colors.grey,
  71. animationType: AnimationType.scale,
  72. pinTheme: PinTheme(
  73. activeColor: const Color(0xFF25A6EE),
  74. selectedColor: const Color(0xFF25A6EE),
  75. inactiveColor: const Color(0xFF25A6EE)),
  76. onChanged: (value) {
  77. setState(() {
  78. currentText = value;
  79. });
  80. },
  81. ),
  82. const SizedBox(height: 16),
  83. MyButton(
  84. '登 录',
  85. radius: 8,
  86. fontWeight: FontWeight.bold,
  87. gradient: const LinearGradient(colors: [Color(0xFF3BD2E5), Color(0xFF247AF8)]),
  88. onTap: () async {
  89. if (currentText.length < 6) {
  90. MyNavigator.showToast('验证码错误');
  91. return;
  92. }
  93. MyNavigator.showLoading();
  94. try {
  95. var res = await MyApi.get().smsLogin({
  96. 'mobile': widget.phone,
  97. 'code': currentText,
  98. });
  99. if (res.data != null) {
  100. if (context.mounted) {
  101. Navigator.of(context).pop(true);
  102. }
  103. }
  104. } catch (e) {
  105. logger.e(e);
  106. }
  107. MyNavigator.dismissLoading();
  108. },
  109. ),
  110. Row(
  111. children: [
  112. const Spacer(),
  113. TextButton(
  114. onPressed: () {
  115. getPhoneMsg();
  116. },
  117. child: const Text(
  118. '重发验证码',
  119. style: TextStyle(fontSize: 12, color: Color(0xFF25A6EE)),
  120. ))
  121. ],
  122. )
  123. ],
  124. ),
  125. ),
  126. ),
  127. );
  128. }
  129. }