Search code examples
material-designflutterfloating-action-button

Hide FAB when onscreen keyboard appear


In Flutter, how to make FAB button hide when onscreen keyboard appear?

FAB button cover up other element when on screenkeyboard show up.

enter image description here


Solution

  • Wrap your FloatingActionButton with a Visibility widget and control the visibility with a bool value.

    Snippet:
    Widget build(context) {
      bool keyboardIsOpen = MediaQuery.of(context).viewInsets.bottom != 0;
      return Scaffold(
        body: // ...
        floatingActionButton: Visibility(
          visible: !keyboardIsOpen,
          child: // your FloatingActionButton
        ),
      );
    }
    

    This solution also gets rid of the animation when the button disappears and appears.

    Be sure that the class extends StatefulWidget and you have created a state for it