import 'package:flutter/material.dart'; import 'package:cherry_toast/cherry_toast.dart'; import '../../common_widgets.dart'; import 'package:together_mobile/common/constants.dart'; import 'package:together_mobile/request/signin_signup.dart'; class UsernameSignupBody extends StatefulWidget { const UsernameSignupBody({super.key}); @override State createState() => _UsernameSignupBodyState(); } class _UsernameSignupBodyState extends State { bool _isProgressShow = false; bool _isHttpSend = false; final TextEditingController usernameController = TextEditingController(); final TextEditingController password1Controller = TextEditingController(); final TextEditingController password2Controller = TextEditingController(); final Map _isError = { 'username': false, 'password1': false, 'password2': false, }; void _isInputError(String type, bool error) { setState(() { _isError[type] = error; }); } @override Widget build(BuildContext context) { return Column( crossAxisAlignment: CrossAxisAlignment.center, children: [ Stack( alignment: Alignment.center, children: [ const SizedBox( width: double.infinity, height: 100, ), if (_isProgressShow) const CircularProgressIndicator( color: kSecondaryColor, strokeWidth: 6.0, ), ], ), SizedBox( width: 250, child: SignTextField( color: kSecondaryColor, labelText: '用户名', type: 'username', controller: usernameController, isError: _isInputError, ), ), const SizedBox( width: double.infinity, height: kDefaultPadding, ), SizedBox( width: 250, child: SignTextField( color: kSecondaryColor, obscureText: true, labelText: '密码', type: 'password1', controller: password1Controller, isError: _isInputError, ), ), const SizedBox( width: double.infinity, height: kDefaultPadding, ), SizedBox( width: 250, child: SignTextField( color: kSecondaryColor, obscureText: true, labelText: '确认密码', type: 'password2', controller: password2Controller, isError: _isInputError, ), ), const SizedBox( height: kDefaultPadding, width: double.infinity, ), elevatedButton( onPressed: () { confirm(context); }, text: '确定', color: kSecondaryColor, ), ], ); } void confirm(BuildContext context) async { if (_isError['username']! || _isError['password1']! || _isError['password2']!) { CherryToast.error( title: const Text('账号或密码不符合规范'), toastDuration: const Duration(seconds: 2), animationDuration: const Duration(seconds: 1), ).show(context); return; } if (password1Controller.text != password2Controller.text) { CherryToast.error( title: const Text('密码不一致'), toastDuration: const Duration(seconds: 2), animationDuration: const Duration(seconds: 1), ).show(context); return; } setState(() { if (!_isProgressShow) _isProgressShow = true; }); Map? result; if (_isProgressShow && !_isHttpSend) { _isHttpSend = true; result = await signup(); } setState(() { if (result!['code'] == 10000) { _isHttpSend = false; _isProgressShow = false; } }); } @override void dispose() { usernameController.dispose(); password1Controller.dispose(); password2Controller.dispose(); super.dispose(); } }