1

I have a page with TextFormField. (autofocus:true). When pressing the Continue button, the user is taken to the next screen, the keyboard should close before that

FocusManager.instance.primaryFocus?.unfocus();
Navigator.push(context, 
  CupertinoPageRoute(
    builder: (context) => const OnBoarding6(),
));

But when I go to the next page, I get an error 'A RenderFlex overflowed by 68 pixels on the bottom' because the keyboard is still closing while the next screen is appearing.

enter image description here

How to avoid this?

4
  • 1
    Just to be clear, this is just something that is annoying with a debug build, correct? Does this affect a release build in any way? Commented May 5 at 18:39
  • Have you inspected your next page widget layout? what if, aside from the keyboard, your layout structure also introduces "a RenderFlex overflowed by 68 pixels on the bottom"... Maybe the layout of the next page isn't flexible or expandable enough, or maybe you should include a SingleChildScrollView. Commented May 6 at 14:55
  • If the presence of an onscreen keyboard breaks your layout, that means you've designed your app to only work with a specific size and aspect ratio. Any app with a layout designed in such a way is going to immediately run into problems once it's pushed out to users because they will have a vast array of mobile devices with sizes and aspect ratios different from the specific device you've been developing on. So the answer to your question is to refactor the layout on the second page to not have this problem to begin with, using for example a scroll view or an adaptive layout. Commented May 6 at 17:49
  • Try using ListView() instead of Column() on next page this makes next screen scrollable, and no more overflow errors. Only use Column() when you need properties like mainAxisAlignment my recommendation. Commented Jun 13 at 7:47

2 Answers 2

0

Take a try like the following code:

FocusNode _focusNode = FocusNode();

@override
void initState() {
  WidgetsBinding.instance.addPostFrameCallback((_) {
    FocusScope.of(context).requestFocus(_focusNode);
  });
}

@override
void dispose() {
  _focusNode.unfocus();
}

/// Test
Widget build(BuildContext context) {
  return Column(
    children: [
      TextFormField(
        focusNode: _focusNode,
        autofocus: false,
      ),
      GestureDetector(
        onTap: () {
          /// Option 1
          _focusNode.unfocus();

          /// Option 2
          FocusScope.of(context).requestFocus(FocusNode());

          Navigator.push(context,
            CupertinoPageRoute(builder: (context) => const OnBoarding6()));
        },
        child: Text('Button'),
      ),
    ],
  );
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try using ListView() instead of Column()

this makes next screen scrollable, and no more overflow errors.

always use listview in body of scaffold. only use Column() when you need properties like mainAxisAlignment: . Other wise use ListView() or warp column with SingleChildScrollView().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.