Skip to content

Instantly share code, notes, and snippets.

@nhancv
Created March 5, 2021 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhancv/08670cb390323db8bc2be9bcafb0f178 to your computer and use it in GitHub Desktop.
Save nhancv/08670cb390323db8bc2be9bcafb0f178 to your computer and use it in GitHub Desktop.
Flutter keyboard detector
import 'package:flutter/material.dart';
import 'package:nft/utils/app_route.dart';
// // AppRoute from nft template
// WKeyboardDetector(
// onState: (bool isOpen, double keyboardHeight) {
// print('isOpen: $isOpen');
// },
// ),
class WKeyboardDetector extends StatefulWidget {
const WKeyboardDetector({
this.onState,
Key key,
}) : super(key: key);
final Function(bool isOpen, double keyboardHeight) onState;
@override
_WKeyboardDetectorState createState() => _WKeyboardDetectorState();
}
class _WKeyboardDetectorState extends State<WKeyboardDetector> {
bool _keyboardVisible = false;
double _keyboardHeight = 0;
set keyboardVisible(bool value) {
if (_keyboardVisible != value) {
_keyboardVisible = value;
if (widget.onState != null) {
widget.onState(_keyboardVisible, _keyboardHeight);
}
}
}
@override
Widget build(BuildContext context) {
_keyboardHeight = MediaQuery.of(AppRoute.I.appContext).viewInsets.bottom;
keyboardVisible = _keyboardHeight > 0;
return Container();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment