Flutter: keyboard disappears immediately when editing my text fields

后端 未结 6 631
醉酒成梦
醉酒成梦 2021-02-12 22:58

Sign up form with flutter

I\'m trying to build an app front end with flutter and it\'s my first time so I faced some bugs like this one: there is no way to edit my textf

相关标签:
6条回答
  • 2021-02-12 23:24

    You should define all of the these:

      final scaffoldKey = GlobalKey<ScaffoldState>();
      final formKey = GlobalKey<FormState>();
      final TextEditingController _controller = new TextEditingController();
    

    as fields of the SignUp_Page not at build function.

    0 讨论(0)
  • 2021-02-12 23:29

    You have the following code in your build function:

    final formKey = GlobalKey<FormState>();
    

    This is the problem. You have to either make it static or move to initState()

    0 讨论(0)
  • 2021-02-12 23:30

    If you are using StatelessWidget, transform it into StatefulWidget. Make sure the key is state's property, not widget's. This happens because your form key is getting recreated over and over again with each rebuild. To keep the key, it should be a part of state.

    0 讨论(0)
  • 2021-02-12 23:33

    I'm going to post my answer here, even though it is not exactly the same as problem as the OP.

    I searched the internet high and low for this issue of the keyboard appearing and disappearing, and
    1. this was the only SO question I found that describes the keyboard appear disappear issue, and
    2. the other solutions out there, like @apc's answer and this github answer are all focused on the formKey being static and/or a GlobalKey, and that was definitely not the cause of my problem (having unchanging keys or re-created keys for my form, and/or it's fields, had no effect at all on the issue).

    My specific problem involved a class AuthService with ChangeNotifier with a number of Future methods.
    After literally days of trying different approaches I found that the only way that I could avoid the keyboard-appearing-and-instantly-disappearing-on-form-field-focus issue was to have the AuthService() be at the top level of my app, in runApp() :

    void main() => runApp(
      ChangeNotifierProvider<AuthService>(
        child: TheWidgetThatIsMyAppAndHasAllBusinessLogicInIt(),
        builder: (BuildContext context) {
          return AuthService();
        },
      ),
    );
    

    (Having the ChangeNotifierProvider anywhere else in the code has the keyboard disappear issue)

    I've then had to get creative about how I structured my logic under this - because I only really care about the AuthService in certain parts of my application, but I'm very new to Flutter and it's all been good learning.

    I don't know why this is, and I'd love to learn more, and I know I haven't spelled out my full situation.....
    but I've spent literally days solving this, I needed to put my answer here to hopefully help others.

    0 讨论(0)
  • 2021-02-12 23:47

    I had the same problem and as @apc said just make the key static or intialize it in iniState()...

    if you use stateless widget make it static

    static final GlobalKey<FormState> _key = GlobalKey<FormState>();

    0 讨论(0)
  • 2021-02-12 23:48

    I see that you have declared formkey inside build function.
    It should be outside the build function. Checkout here

    0 讨论(0)
提交回复
热议问题