import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:vnm_version/widgets/login_registe_button.dart';
import 'package:vnm_version/widgets/login_register_error_tips.dart';
import '../../../config/url_parameters_constant.dart';
import '../../../router/routers.dart';
import '../../../utils/http/dio_utils.dart';
import '../../../utils/http/http_custom_error.dart';
import '../../../utils/http/url_util.dart';
import '../../../utils/navigator_utils.dart';
import '../../../widgets/common_input_container.dart';
import '../../../widgets/forgot_password_tips.dart';
import '../../../widgets/register_login_bottom.dart';
class InputAccountWidget extends StatefulWidget {
const InputAccountWidget({super.key});
@override
State<StatefulWidget> createState() {
return _InputAccountState();
}
}
class _InputAccountState extends State<InputAccountWidget> {
bool accountError = false;
late FocusNode focusNode;
String account = "";
String errorTips = "";
///增加代码
final keyboardVisibilityController = KeyboardVisibilityController();
ScrollPhysics scrollPhysics = const AlwaysScrollableScrollPhysics();
@override
void initState() {
super.initState();
focusNode = FocusNode();
///订阅键盘可见性变化
keyboardVisibilityController.onChange.listen((bool visible) {
ScrollPhysics sc;
if (visible) {
sc = const NeverScrollableScrollPhysics();
} else {
sc = const AlwaysScrollableScrollPhysics();
}
///关键点:延迟后再setState,
Future.delayed(const Duration(milliseconds: 500), () {
setState(() {
scrollPhysics = sc;
});
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: KeyboardVisibilityBuilder(builder: (context, isKeyboardVisible) {
return SizedBox(
width: ScreenUtil().screenWidth,
height: ScreenUtil().screenHeight,
child: Stack(children: [
Positioned.fill(
child: Container(
color: Colors.white,
width: double.infinity,
height: double.infinity,
child: const Image(
image: AssetImage("assets/images/login_register_bg.png"),
fit: BoxFit.fitWidth,
alignment: Alignment.topCenter),
)),
Positioned.fill(
child: SingleChildScrollView(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
///设置滚动模式
physics: scrollPhysics,
child: SizedBox(
width: ScreenUtil().screenWidth,
height: ScreenUtil().screenHeight,
child: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.end,
children: [
Container(
width: ScreenUtil().screenWidth,
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(
'assets/images/login_bg_for_move.png'),
fit: BoxFit.fitWidth,
alignment: Alignment.topCenter,
),
),
child: Column(
children: [
SizedBox(height: 70.h),
Image(
image: const AssetImage(
"assets/images/forgot_password_text.png"),
height: 84.h,
),
SizedBox(height: 30.h),
const ForgotPasswordTips(0),
SizedBox(height: 30.h),
CommonInputContainer(
accountError, focusNode.hasFocus,
(value) {
setState(() {
account = value;
if (accountError) {
accountError = false;
}
});
},
false,
focusNode,
AppLocalizations.of(context)?.account ??
"Account",
SvgPicture.asset(
"assets/images/login_account.svg"),
null),
LoginRegisterErrorTipsWidget(
accountError,
true,
AppLocalizations.of(context)
?.accountNotExist ??
"The account you entered does not exist",
45.w),
SizedBox(height: 10.h),
LoginRegisterButtons(_submit,
"assets/images/submit_enabled.png",
iconDisabled:
"assets/images/submit_disabled.png",
isButtonEnabled:
account.isNotEmpty && !accountError)
],
),
),
Container(
width: double.infinity,
height: 130.h,
color: Colors.white),
RegisterLoginBottomButton(() {}, () {}),
]))))
]));
}));
}
@override
void dispose() {
focusNode.dispose();
super.dispose();
}
void _submit() {
Map<String, dynamic> params = {UrlParametersConstant.account: account};
DioUtils.main.postRequest(UrlUtil.checkUserBindPhone, jsonMap: params).then(
(responseMap) {
NavigatorUtils.pushName(context, Routers.inputPhone,
parameters: {UrlParametersConstant.account: account});
}, onError: (e) {
if (e is ResponseCustomError) {
if (e.code == 11009) {
setState(() {
accountError = true;
errorTips = AppLocalizations.of(context)?.accountNotExist ??
"The account you entered does not exist";
});
} else if (e.code == 11040) {
setState(() {
accountError = true;
errorTips =
AppLocalizations.of(context)?.accountNotBoundToMobileNumber ??
"Account not bound to mobile number";
});
} else {
setState(() {
accountError = true;
errorTips =
AppLocalizations.of(context)?.unknownError ?? "Unknown error";
});
}
}
});
}
}
现在拖动上面的部分,会发现无法滑动