前言
flutter 监听键盘弹起,收回是个不好处理的问题,
下面的代码也许能帮到你
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
import 'package:flutter/material.dart'; import 'package:flutter/services.dart';
class KeyboardEventWidget extends StatefulWidget {
final Widget Function(double bottomMargin)? childBuild;
final Widget? child;
final Function(double bottomMargin)? onKbShowBegin;
final Function(double bottomMargin)? onKbShowEnd;
final Function(double bottomMargin)? onKbShowing;
final Function(double bottomMargin)? onKbHideBegin;
final Function(double bottomMargin)? onKbHideEnd;
final Function(double bottomMargin)? onKbHiding;
final Function(double bottomMargin)? onKbSliding;
const KeyboardEventWidget({ this.childBuild, this.child, this.onKbShowBegin, this.onKbShowing, this.onKbShowEnd, this.onKbHideBegin, this.onKbHiding, this.onKbHideEnd, this.onKbSliding, super.key });
@override State<KeyboardEventWidget> createState() => KeyboardEventWidgetState(); }
class KeyboardEventWidgetState extends State<KeyboardEventWidget> with WidgetsBindingObserver {
double bottomMargin = 0;
bool isKbHiding = false;
bool isKbShowing = false;
bool isKbShowEnd = false;
final Duration checkEndDelay = const Duration(milliseconds: 200);
@override Widget build(BuildContext context) { if (widget.child!=null) { return widget.child!; } else if (widget.childBuild!=null) { return widget.childBuild!(bottomMargin); } return const SizedBox(); }
@override void initState() { super.initState(); SystemChannels.textInput.invokeMethod('TextInput.hide'); WidgetsBinding.instance.addObserver(this); }
@override void dispose() { WidgetsBinding.instance.removeObserver(this); super.dispose(); }
@override void didChangeMetrics() { super.didChangeMetrics(); WidgetsBinding.instance.addPostFrameCallback((_) { double newBottomMargin = MediaQuery.of(context).viewInsets.bottom; if (newBottomMargin > 0 && bottomMargin == 0) { showBegin(newBottomMargin); } else if (newBottomMargin == bottomMargin && isKbShowing && isKbShowEnd) { showEnd(newBottomMargin); } else if (newBottomMargin < bottomMargin && !isKbHiding) { hideBegin(newBottomMargin); } else if (newBottomMargin == 0 && bottomMargin > 0) { hideEnd(newBottomMargin); } if (isKbHiding && bottomMargin > newBottomMargin) { if (widget.onKbSliding!=null) { widget.onKbSliding!(newBottomMargin>20 ? newBottomMargin-20 : 0); } if (widget.onKbHiding!=null) { widget.onKbHiding!(newBottomMargin>20 ? newBottomMargin-20 : 0); } } else if (isKbShowing && bottomMargin < newBottomMargin) { if (widget.onKbShowing!=null) { widget.onKbShowing!(newBottomMargin); } if (widget.onKbSliding!=null) { widget.onKbSliding!(newBottomMargin); } } bottomMargin = newBottomMargin; }); }
void showBegin(double newBottomMargin) { isKbShowing = true; if (widget.onKbShowBegin!=null) { widget.onKbShowBegin!(0); } Future.delayed(checkEndDelay, (){ isKbShowEnd = true; Future.delayed(const Duration(milliseconds: 100), (){ if (newBottomMargin == bottomMargin && isKbShowing && isKbShowEnd) { showEnd(newBottomMargin); } }); }); }
void showEnd(double newBottomMargin) { isKbShowing = false; isKbShowEnd = false; if (widget.onKbShowing!=null) { widget.onKbShowing!(newBottomMargin); } if (widget.onKbSliding!=null) { widget.onKbSliding!(newBottomMargin); } if (widget.onKbShowEnd!=null) { widget.onKbShowEnd!(newBottomMargin); } }
void hideBegin(double newBottomMargin) { isKbHiding = true; if (widget.onKbHideBegin!=null) { widget.onKbHideBegin!(newBottomMargin); } }
void hideEnd(double newBottomMargin) { isKbHiding = false; if (widget.onKbSliding!=null) { widget.onKbSliding!(0); } if (widget.onKbHiding!=null) { widget.onKbHiding!(0); } if (widget.onKbHideEnd!=null) { widget.onKbHideEnd!(0); } } }
|
如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !