隐藏在键盘颤动下的文本字段

Posted

技术标签:

【中文标题】隐藏在键盘颤动下的文本字段【英文标题】:Textfield hiding under keyboard flutter 【发布时间】:2020-09-17 23:54:37 【问题描述】:

之前文本字段使用键盘上去,但现在它不去并隐藏在键盘下。我已经尝试了互联网上的所有解决方案,如 resizeToAvoidBottomInset、resizeToAvoidBottomPadding 等,但没有一个对我有用。这非常令人沮丧。你们能帮帮我吗?

import 'package:darpandentalhome/services/auth.dart';
import 'package:darpandentalhome/shared/const.dart';
import 'package:darpandentalhome/shared/loading.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:flutter_svg/flutter_svg.dart';

class SignIn extends StatefulWidget 

  final Function toggleView;
  SignIn(this.toggleView);

  @override
  _SignInState createState() => _SignInState();


class _SignInState extends State<SignIn> 

  final AuthService _auth = AuthService();
  final _formKey = GlobalKey<FormState>();
  GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();

  bool loading = false;

  String email = '';
  String password = '';

  @override
  Widget build(BuildContext context) 
    return Scaffold(
      key: _scaffoldKey,
      backgroundColor: Color(0xfff9f9f9),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            Container(
              width: MediaQuery. of(context). size. width,
              child: SvgPicture.asset('assets/images/Illustration.svg', fit: BoxFit.cover,),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Center(
                child: Text(
                  'Darpan Dental Home',
                  style: GoogleFonts.rubik(
                    textStyle: TextStyle(
                      fontSize: 35,
                      fontWeight: FontWeight.w500
                    ),
                  ),
                ),
              ),
            ),
            loading ? Loading() : SizedBox(height: 6),
            Form(
              key: _formKey,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Padding(
                      padding: const EdgeInsets.fromLTRB(25,10,20,0),
                      child: Text(
                        'Email:',
                        style: GoogleFonts.rubik(
                          textStyle: TextStyle(
                              fontSize: 18,
                          ),
                        )
                      )
                  ),
                  Padding(
                    padding: const EdgeInsets.fromLTRB(20,10,20,10),
                    child: TextFormField(
                      textInputAction: TextInputAction.next,
                      onEditingComplete: () => FocusScope.of(context).nextFocus(),
                      validator: (val) => val.isEmpty ? "Please Enter your email" : null,
                      onChanged: (val) 
                        setState(() 
                          email = val;
                        );
                      ,
                      style: GoogleFonts.rubik(
                        textStyle: TextStyle(
                          fontSize: 15,
                        ),
                      ),
                      cursorColor: Colors.black,
                      decoration: textInputDecoration.copyWith(hintText: 'sanjivgurung@gmail.com'),
                    ),
                  ),
                  Padding(
                      padding: const EdgeInsets.fromLTRB(25,10,20,0),
                      child: Text(
                        'Password:',
                          style: GoogleFonts.rubik(
                            textStyle: TextStyle(
                              fontSize: 18,
                            ),
                          )
                      )
                  ),
                    Padding(
                      padding: const EdgeInsets.fromLTRB(20,10,20,20),
                      child: TextFormField(
                        textInputAction: TextInputAction.done,
                        validator: (val) => val.length<6 ? "Please enter a password 6+ character" : null,
                        onChanged: (val) 
                          setState(() 
                            password = val;
                          );
                        ,
                          style: GoogleFonts.rubik(
                            textStyle: TextStyle(
                              fontSize: 15,
                            ),
                          ),
                          obscureText: true,
                          cursorColor: Colors.black,
                          decoration: textInputDecoration.copyWith(hintText: '**********'),
                      ),
                  )
                ],
              ),
            ),
            MaterialButton(
              height: 55,
              minWidth: 230,
              elevation: 0,
              shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(10.0)),
              color: Color(0xff4CBBB9),
              onPressed: () async 
                if(_formKey.currentState.validate())
                  setState(() 
                    loading = true;
                  );
                  dynamic result = await _auth.signInWithEmailAndPassword(email, password);
                  if(result==null) 
                    setState(() 
                      loading = false;
                    );
                    _scaffoldKey.currentState.showSnackBar(
                        SnackBar(
                          behavior: SnackBarBehavior.floating,
                          elevation: 0,
                          duration: Duration(milliseconds: 800),
                          backgroundColor: Colors.red[700],
                          content: Text(
                              'Error Signing In',
                              style: GoogleFonts.rubik(
                                textStyle: TextStyle(
                                    color: Colors.white,
                                    fontSize: 18,
                                  letterSpacing: 1.5
                                ),
                              )
                          ),
                        )
                    );
                  
                
              ,
              child: Text(
                'Sign In',
                  style: GoogleFonts.rubik(
                    textStyle: TextStyle(
                      fontSize: 17,
                      color: Colors.white,
                      fontWeight: FontWeight.w500
                    ),
                  )
              ),
            ),
            Center(
                child: Padding(
                  padding: EdgeInsets.only(top: 10, bottom: 10),
                  child: InkWell(
                    onTap: () 
                      widget.toggleView();
                    ,
                    child: Text(
                      'Want a new account?',
                      style: GoogleFonts.rubik(
                        textStyle: TextStyle(
                          fontSize: 15,
                          color: Color(0xffCE5B51),
                        ),
                      ),
                    ),
                  ),
                )
            ),
          ],
        ),
      ),
    );
  

当我单击文本字段时,我也会收到此消息。有什么问题吗?

W/IInputConnectionWrapper(18968): beginBatchEdit on inactive InputConnection
W/IInputConnectionWrapper(18968): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper(18968): getTextAfterCursor on inactive InputConnection
W/IInputConnectionWrapper(18968): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper(18968): endBatchEdit on inactive InputConnection

Screen after keyboard is up

Screen before keyboard is up

【问题讨论】:

您仍然可以向上滚动到想要的字段吗? 这能回答你的问题吗? Flutter Keyboard makes textfield hide 不,当键盘启动时我不能。 但我可以手动滚动。问题是它没有提供键盘并专注于以前使用的选定 texfield。 【参考方案1】:

通过从 androidManifest.xml 文件中删除这行代码并完全重新启动应用程序来解决此问题。之前它不起作用,因为我只是删除线路并热重启应用程序。为了将更改保存在 AndroidManifest.xml 文件中,您必须完全重启应用才能看到更改。

<item name="android:windowFullscreen">true</item>

谢谢。

【讨论】:

以上是关于隐藏在键盘颤动下的文本字段的主要内容,如果未能解决你的问题,请参考以下文章

颤动键盘使文本域隐藏

如何在颤动中将焦点转移到下一个文本字段?

隐藏在键盘下的 SKStoreReviewController 按钮

如何在颤动应用程序中隐藏单击按钮?

如何知道键盘何时隐藏文本字段?

iPhone - 键盘隐藏文本字段