android - flutter : screen not scrolling up when keyboard appears in android

标签 android flutter dart flutter-layout

我正在尝试在 flutter 中制作登录屏幕

我正在使用下面的代码

ModalProgressHUD(
        dismissible: false,
        child: Container(
          color: Colors.white,
          child: SafeArea(
            child: Scaffold(
              body: Container(
                height: double.infinity,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/login_bg.png'),
                    fit: BoxFit.fill,
                  ),
                  shape: BoxShape.rectangle,
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.max,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.only(left: 15.0),
                      child: IconButton(
                        iconSize: 70,
                        icon: Image.asset(
                          'assets/images/back.png',
                        ),
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                    ),
//                    Spacer(),
                    Expanded(
                      child: SingleChildScrollView(
                        child:  Column(
                          mainAxisSize: MainAxisSize.max,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Column(
                              mainAxisSize: MainAxisSize.max,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Column(
                                  crossAxisAlignment: CrossAxisAlignment.center,
                                  mainAxisSize: MainAxisSize.max,
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: <Widget>[
                                    Container(
                                      margin: const EdgeInsets.only(
                                          top: 30, bottom: 40),
                                      child: Image.asset(
                                        'assets/images/oricon_logo.png',
                                        height: 100.0,
                                        width: 100.0,
                                      ),
                                    ),
                                    Column(
                                      crossAxisAlignment:
                                      CrossAxisAlignment.center,
                                      mainAxisSize: MainAxisSize.max,
                                      mainAxisAlignment: MainAxisAlignment.end,
                                      children: <Widget>[
                                        Form(
                                          key: _formKey,
                                          child: new Theme(
                                              data: ThemeData(
                                                  accentColor: Colors.purple,
                                                  primaryColor:
                                                  AppColors.colorBlue,
                                                  inputDecorationTheme:
                                                  new InputDecorationTheme(
                                                      labelStyle:
                                                      new TextStyle(
                                                        color: AppColors.colorBlue,
                                                        fontSize: 20.0,
                                                      ))),
                                              child: Padding(
                                                padding:
                                                const EdgeInsets.all(20.0),
                                                child: Column(
                                                  crossAxisAlignment:
                                                  CrossAxisAlignment.center,
                                                  children: <Widget>[
                                                    TextFormField(
                                                      keyboardType:
                                                      TextInputType
                                                          .emailAddress,
                                                      textCapitalization:
                                                      TextCapitalization
                                                          .none,
                                                      focusNode:
                                                      _emailFocusNode,
                                                      controller:
                                                      emailTextController,

                                                      maxLines: 1,
                                                      maxLength: 40,
                                                      // textCapitalization:
                                                      //     TextCapitalization.none,
                                                      style: TextStyle(
                                                          color: AppColors
                                                              .colorGray),
                                                      decoration:
                                                      new InputDecoration(
                                                          labelText:
                                                          "Email address",
                                                          counterText: '',
                                                          labelStyle:
                                                          TextStyle(
                                                            fontFamily:
                                                            'h_roman',
                                                            color: _emailFocusNode
                                                                .hasFocus
                                                                ? AppColors
                                                                .colorBlue
                                                                : AppColors
                                                                .colorGray,
                                                          ),
                                                          enabledBorder:
                                                          UnderlineInputBorder(
                                                            borderSide: BorderSide(
                                                                color: AppColors
                                                                    .colorGray),
                                                          ),
                                                          focusedBorder:
                                                          UnderlineInputBorder(
                                                            borderSide: BorderSide(
                                                                color: AppColors
                                                                    .colorBlue),
                                                          ),
                                                          border:
                                                          UnderlineInputBorder()),

                                                      validator:
                                                          (String email) {
                                                        if (email.length == 0) {
                                                          return 'Please enter email address';
                                                        } else {
                                                          if (!RegExp(
                                                              r"^[a-zA-Z0-9.]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
                                                              .hasMatch(
                                                              email)) {
                                                            return 'Please enter valid email address';
                                                          } else {
                                                            return null;
                                                          }
                                                        }
                                                      },
                                                      onSaved: (String val) {
                                                        // _email = val;
                                                      },
                                                      onChanged: (text) {
                                                        // _email = text;
                                                      },
                                                    ),
                                                    TextFormField(
                                                      focusNode:
                                                      _passwordFocusNode,
                                                      controller:
                                                      passTextController,
                                                      maxLength: 20,
                                                      style: TextStyle(
                                                          fontFamily: 'h_roman',
                                                          color: AppColors
                                                              .colorGray),
                                                      decoration:
                                                      new InputDecoration(
                                                          counterText: '',
                                                          suffixIcon:
                                                          IconButton(
                                                            icon: Icon(
                                                              // Based on passwordVisible state choose the icon
                                                              passwordVisible
                                                                  ? Icons
                                                                  .visibility
                                                                  : Icons
                                                                  .visibility_off,
                                                              color: AppColors
                                                                  .colorGray,
                                                            ),
                                                            onPressed: () {
                                                              setState(() {
                                                                passwordVisible =
                                                                !passwordVisible;
                                                              });
                                                            },
                                                          ),
                                                          labelText:
                                                          "Password",
                                                          labelStyle:
                                                          TextStyle(
                                                            fontFamily:
                                                            'h_roman',
                                                            color: _passwordFocusNode
                                                                .hasFocus
                                                                ? AppColors
                                                                .colorBlue
                                                                : AppColors
                                                                .colorGray,
                                                          ),
                                                          enabledBorder:
                                                          UnderlineInputBorder(
                                                            borderSide: BorderSide(
                                                                color: AppColors
                                                                    .colorGray),
                                                          ),
                                                          focusedBorder:
                                                          UnderlineInputBorder(
                                                            borderSide: BorderSide(
                                                                color: AppColors
                                                                    .colorBlue),
                                                          ),
                                                          border:
                                                          UnderlineInputBorder()),
                                                      keyboardType:
                                                      TextInputType.text,
                                                      obscureText:
                                                      passwordVisible,
                                                      validator: (String pass) {
                                                        if (pass.length == 0) {
                                                          return 'Please enter password';
                                                        } else {
                                                          return null;
                                                        }
                                                      },
                                                    ),
                                                    Align(
                                                        alignment: Alignment
                                                            .bottomRight,
                                                        child: Padding(
                                                          padding:
                                                          const EdgeInsets
                                                              .only(
                                                              top: 20),
                                                          child:
                                                          GestureDetector(
                                                            onTap: () {
                                                              showContactUsDialog(
                                                                  context);
                                                            },
                                                            child: Padding(
                                                              padding:
                                                              const EdgeInsets
                                                                  .all(8.0),
                                                              child: Text(
                                                                "Need help?",
                                                                style: TextStyle(
                                                                    fontFamily:
                                                                    'h_roman',
                                                                    color: AppColors
                                                                        .colorBlue,
                                                                    fontWeight:
                                                                    FontWeight
                                                                        .normal,
                                                                    fontSize:
                                                                    15.0),
                                                              ),
                                                            ),
                                                          ),
                                                        )),
                                                    Container(
                                                      margin:
                                                      const EdgeInsets.only(
                                                          top: 40.0),
                                                      child: MaterialButton(
                                                        textColor: Colors.white,
                                                        minWidth: 250.0,
                                                        padding:
                                                        const EdgeInsets
                                                            .all(15.0),
                                                        color:
                                                        AppColors.colorBlue,
                                                        child: Text(
                                                          "Login",
                                                          style: TextStyle(
                                                              fontFamily:
                                                              'h_bold',
                                                              color:
                                                              Colors.white,
//                                    fontWeight: FontWeight.bold,
                                                              fontSize: 20.0),
                                                        ),
                                                        shape: RoundedRectangleBorder(
                                                            borderRadius:
                                                            BorderRadius
                                                                .circular(
                                                                50.0)),
                                                        onPressed: () {
                                                          if (Utils()
                                                              .isEmptyString(
                                                              _token)) {
                                                            if (_formKey
                                                                .currentState
                                                                .validate()) {
                                                              debugPrint(
                                                                  "Email Address -> " +
                                                                      emailTextController
                                                                          .text);

                                                              debugPrint(
                                                                  "Password  -> " +
                                                                      passTextController
                                                                          .text);
                                                              Utils()
                                                                  .getStatus()
                                                                  .then(
                                                                      (connectionResult) {
                                                                    if (connectionResult) {
                                                                      setState(() {
                                                                        _saving =
                                                                        true;
                                                                      });

                                                                      _callLoginAPI(
                                                                          emailTextController
                                                                              .text,
                                                                          passTextController
                                                                              .text);
                                                                    } else {
                                                                      Utils().showMessageDialog(
                                                                          context,
                                                                          "No internet connection");
                                                                    }
                                                                  });
                                                            }
                                                          } else {
                                                            Utils().showMessageDialog(
                                                                context,
                                                                "Notification token is invalid");
                                                          }
                                                        },
                                                        splashColor: Colors
                                                            .redAccent[100],
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              )),
                                        ),
                                      ],
                                    ),
                                  ],
                                ),
                              ],
                            ),
//                          Container(
//                            margin:
//                            const EdgeInsets.only(top: 60, bottom: 20),
//                            child: Row(
//                              mainAxisAlignment: MainAxisAlignment.center,
//                              children: <Widget>[
////                          FlutterLogo(
////                            size: 40,
////                          ),
////                          FlutterLogo(
////                            size: 40,
////                          ),
//                              ],
//                            ),
//                          ),

                          ],
                        ),
                      ),
                    )
                  ],
                ),
              ),
            ),
          ),
        ),
        inAsyncCall: _saving);

上面的代码在 ios 设备中工作正常,但在 android 屏幕中键盘出现时不会向上滚动

我还检查了下面的一些链接和帖子,但它并没有帮助解决我的问题

如果需要更多信息,请告诉我。提前致谢。您的努力将受到赞赏。

最佳答案

如果升级到 Flutter 3.10 后出现此问题,请将 screen_util 库升级到最新版本并设置 useInheritedMediaQuery:true

ScreenUtilInit(
        useInheritedMediaQuery: true,
        ....

关于android - flutter : screen not scrolling up when keyboard appears in android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60051003/

相关文章:

android - 发送和读取通过 Cordova 中的原生 SMS 应用程序发送的 SMS 内容

java - Gson 类型转换错误

flutter - 如何仅在此Map Double Double中调用第二个字符串?

authentication - 如何在 flutter 应用程序的入口点检查身份验证 token

java - Android:FragmentTabHost 小部件不显示标题

json - 使用flutter解析具有多个键的json响应

flutter - 仅允许 TextFormField 中的特定输入,而无需在 Flutter 中进行验证

android - 从 Flutter 中的 Text() Widget 获取字符串内容

flutter - Dart 为参数设置默认值

java - setWebViewClient 错误 - someView 中的 someView(context) 无法应用于 ()