import 'package:flutter/material.dart'; import 'package:lszlgl/base/base_lifecycle_state.dart'; import 'package:lszlgl/main.dart'; import 'package:lszlgl/network/my_api.dart'; import 'package:lszlgl/widget/button.dart'; import 'package:pin_code_fields/pin_code_fields.dart'; class PhoneVerifyWidget extends StatefulWidget { final String phone; const PhoneVerifyWidget({super.key, required this.phone}); @override State createState() => _PhoneVerifyWidgetState(); } class _PhoneVerifyWidgetState extends State { String currentText = ""; String starPhone = ''; /// 获取验证码 Future getPhoneMsg() async { try { var res = await MyApi.get().loginWithPhoneMsg({ 'mobile': widget.phone, 'scene': 21, }); //print('$res.data'); if (res.data ?? false) { MyNavigator.showToast('验证码已发送'); return true; } else { MyNavigator.showToast('验证码发送失败'); return false; } } catch (e) { logger.e(e); return false; } } @override void initState() { super.initState(); getPhoneMsg(); starPhone = widget.phone.replaceFirst(RegExp(r'\d{4}'), '****', 3); } @override Widget build(BuildContext context) { return PopScope( canPop: false, onPopInvoked: (bool didPop) { if (didPop) { return; } else { Navigator.of(context).pop(false); } }, child: Dialog( insetPadding: const EdgeInsets.symmetric(horizontal: 22), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), child: Container( padding: const EdgeInsets.fromLTRB(18, 24, 18, 18), child: Column( mainAxisSize: MainAxisSize.min, children: [ const Text('短信验证码',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 16),), const SizedBox(height: 8), Text('已向$starPhone发送短信验证码', style: const TextStyle(color: Colors.grey),), const SizedBox(height: 22), PinCodeTextField( length: 6, appContext: context, autoFocus: true, keyboardType: TextInputType.number, cursorColor: Colors.grey, animationType: AnimationType.scale, pinTheme: PinTheme( activeColor: const Color(0xFF25A6EE), selectedColor: const Color(0xFF25A6EE), inactiveColor: const Color(0xFF25A6EE)), onChanged: (value) { setState(() { currentText = value; }); }, ), const SizedBox(height: 16), MyButton( '登 录', radius: 8, fontWeight: FontWeight.bold, gradient: const LinearGradient(colors: [Color(0xFF3BD2E5), Color(0xFF247AF8)]), onTap: () async { if (currentText.length < 6) { MyNavigator.showToast('验证码错误'); return; } MyNavigator.showLoading(); try { var res = await MyApi.get().smsLogin({ 'mobile': widget.phone, 'code': currentText, }); if (res.data != null) { if (context.mounted) { Navigator.of(context).pop(true); } } } catch (e) { logger.e(e); } MyNavigator.dismissLoading(); }, ), Row( children: [ const Spacer(), TextButton( onPressed: () { getPhoneMsg(); }, child: const Text( '重发验证码', style: TextStyle(fontSize: 12, color: Color(0xFF25A6EE)), )) ], ) ], ), ), ), ); } }