| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import 'dart:ui';
- import 'package:flutter/widgets.dart';
- class KeyboardVisibilty extends StatefulWidget {
- const KeyboardVisibilty({
- Key? key,
- required this.child,
- this.onKeyboardShow,
- this.onKeyboardHide,
- }) : super(key: key);
- final Widget child;
- final VoidCallback? onKeyboardShow;
- final VoidCallback? onKeyboardHide;
- @override
- KeyboardVisibiltyState createState() => KeyboardVisibiltyState();
- }
- class KeyboardVisibiltyState extends State<KeyboardVisibilty>
- with WidgetsBindingObserver {
- @override
- void initState() {
- super.initState();
- WidgetsBinding.instance.addObserver(this);
- }
- @override
- void dispose() {
- WidgetsBinding.instance.removeObserver(this);
- super.dispose();
- }
- @override
- void didChangeMetrics() {
- final bottomInset = View.of(context).viewInsets.bottom;
- if (bottomInset != _lastBottomInset) {
- if (bottomInset > 0) {
- widget.onKeyboardShow?.call();
- } else {
- widget.onKeyboardHide?.call();
- }
- }
- _lastBottomInset = bottomInset;
- super.didChangeMetrics();
- }
- var _lastBottomInset = 0.0;
- @override
- Widget build(BuildContext context) {
- return widget.child;
- }
- }
|