I am building a mobile app, I want to remove a widget when the keyboard appears on the screen, i.e when the input text field is on focus.
I have tried to use
I used the package keyboard_visibility
Then I wrapped my TextField with a KeyboardListener implemented as follows:
class KeyboardListener extends StatefulWidget {
final Widget child;
final void Function(bool) onChange;
KeyboardListener({@required this.child, @required this.onChange});
@override
_KeyboardListenerState createState() => _KeyboardListenerState();
}
class _KeyboardListenerState extends State {
int _sId;
KeyboardVisibilityNotification _kvn;
@override
void initState() {
super.initState();
_kvn = KeyboardVisibilityNotification();
_sId = _kvn.addNewListener(
onChange: widget.onChange,
);
}
@override
Widget build(BuildContext context) {
return widget.child;
}
@override
void dispose() {
_kvn.removeListener(_sId);
super.dispose();
}
}