import 'package:flutter/material.dart'; import 'package:cherry_toast/cherry_toast.dart'; import 'package:go_router/go_router.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:together_mobile/database/hive_database.dart'; import 'package:together_mobile/models/token_model.dart'; import '../../../components/common_widgets.dart'; import 'package:together_mobile/common/constants.dart'; import 'package:together_mobile/request/signup_signin.dart'; import 'package:together_mobile/models/init_get_it.dart'; import 'package:together_mobile/models/user_model.dart'; class UsernameSigninBody extends StatefulWidget { const UsernameSigninBody({super.key}); @override State createState() => _UsernameSigninBodyState(); } class _UsernameSigninBodyState extends State { bool _isProgressShow = false; bool _isHttpSend = false; final TextEditingController usernameController = TextEditingController(); final TextEditingController passwordController = TextEditingController(); final Map _isError = { 'username': false, 'password': false, }; @override void dispose() { usernameController.dispose(); passwordController.dispose(); super.dispose(); } 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: kPrimaryColor, // strokeWidth: 6.0, // ), // ], // ), const SizedBox( width: double.infinity, height: 100, ), SizedBox( width: 250, child: SignTextField( labelText: '用户名', type: 'username', isSignup: false, controller: usernameController, isError: _isInputError, ), ), const SizedBox( width: double.infinity, height: kDefaultPadding, ), SizedBox( width: 250, child: SignTextField( obscureText: true, labelText: '密码', isSignup: false, type: 'password', controller: passwordController, isError: _isInputError, ), ), const SizedBox( height: kDefaultPadding, width: double.infinity, ), CommonElevatedButton( onPressed: () { _signin(context); }, text: '登录', ), ], ); } void _signin(BuildContext context) async { if (_isError['username']! || _isError['password']!) { CherryToast.error( title: const Text('账号或密码不符合规范'), toastDuration: const Duration(seconds: 2), animationDuration: const Duration(seconds: 1), ).show(context); return; } setState(() { if (!_isProgressShow) _isProgressShow = true; }); EasyLoading.showInfo('正在登录中......'); late Map result; if (_isProgressShow && !_isHttpSend) { _isHttpSend = true; try { result = await signinByUsername( usernameController.text, passwordController.text, ); } catch (e) { EasyLoading.showError('连接服务器失败,请稍后重试...'); return; } finally { setState(() { _isHttpSend = false; _isProgressShow = false; }); } } if (result['code'] == 10200) { getIt().init(result['data']); DeviceInfoPlugin deviceInfo = DeviceInfoPlugin(); AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo; Map res = await createToken(getIt.get().id, androidDeviceInfo.id); getIt.get().init(); getIt.get().updateToken(res['token']); await HiveDatabase.init(); EasyLoading.dismiss(); // ignore: use_build_context_synchronously context.go('/chat'); } else { // ignore: use_build_context_synchronously CherryToast.error(title: const Text('用户名或密码错误')).show(context); } } }