Flutter: keyboard disappears immediately when editing my text fields

后端 未结 6 828
醉酒成梦
醉酒成梦 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: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(
        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.

提交回复
热议问题