Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextField will be cleared when the soft keyboard disappears #62

Closed
shingohu opened this issue Mar 15, 2019 · 3 comments
Closed

TextField will be cleared when the soft keyboard disappears #62

shingohu opened this issue Mar 15, 2019 · 3 comments

Comments

@shingohu
Copy link

shingohu commented Mar 15, 2019

1.input text
2.close the softkeyboard,the text will be cleared

Every time the build call,the _oldPWControll print as a new instance

class Demo extends HookWidget {
  final _oldPWControll = TextEditingController();

  final _oldPWFN = FocusNode();
  @override
  Widget build(BuildContext context) {
    print(_oldPWControll);
    return Scaffold(
      resizeToAvoidBottomInset: true,
      body: TextField(
        controller: _oldPWControll,
        textInputAction: TextInputAction.next,
        focusNode: _oldPWFN,
        decoration: InputDecoration(
          hintText: "请输入",
          contentPadding: EdgeInsets.fromLTRB(35.0, 12.0, 12.0, 12.0),
        ),
      ),
    );
  }
}

When Demo as the home Widget of MaterialApp,it is work
but push to the Demo,problems will arise

@rrousselGit
Copy link
Owner

You should not create these as fields of the class.

Instead create them using hooks inside the build method

Widget build(BuildContext context) {
  final controller = useMemoized(() => TextEditingController());
  ... 
}

@shingohu
Copy link
Author

you mean I can't define any final variables directly from KookWidget?

@rrousselGit
Copy link
Owner

Nor can you do so with any other kind of widgets.

Widgets are immutable. You need a State subclass using hooks to preserve values between builds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants