王学岗——解决flutter弹出软键盘后布局无限滚动的情况

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";
          });
        }
      }
    });
  }
}

现在拖动上面的部分,会发现无法滑动

相关推荐
jcLee9517 小时前
Flutter/Dart:使用日志模块Logger Easier
flutter·log4j·dart·logger
tmacfrank18 小时前
Flutter 异步编程简述
flutter
tmacfrank18 小时前
Flutter 基础知识总结
flutter
叫我菜菜就好19 小时前
【Flutter_Web】Flutter编译Web第三篇(网络请求篇):dio如何改造方法,变成web之后数据如何处理
前端·网络·flutter
AiFlutter1 天前
Flutter-底部分享弹窗(showModalBottomSheet)
java·前端·flutter
m0_748247802 天前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
迷雾漫步者2 天前
Flutter组件————PageView
flutter·跨平台·dart
迷雾漫步者2 天前
Flutter组件————FloatingActionButton
前端·flutter·dart
coder_pig3 天前
📝小记:Ubuntu 部署 Jenkins 打包 Flutter APK
flutter·ubuntu·jenkins
捡芝麻丢西瓜3 天前
flutter自学笔记5- dart 编码规范
flutter·dart