在我的页面中,我有textfield
,并且在点击textfield
打开键盘时,现在我想知道完成键盘打开动画的时间。当我使用任何有关键盘的软件包时,启动动画时键盘可见属性将变为true。
答案 0 :(得分:0)
您可以将侦听器添加到要观察的类中。例如
class KeyboardTogglePage extends StatefulWidget {
@override
_KeyboardTogglePageState createState() => _KeyboardTogglePageState();
}
class _KeyboardTogglePageState extends State<KeyboardTogglePage>
with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
var isKeyboardOpen = false;
///
/// This routine is invoked when the window metrics have changed.
///
@override
void didChangeMetrics() {
final value = MediaQuery.of(context).viewInsets.bottom;
if (value > 0) {
if (isKeyboardOpen) {
_onKeyboardChanged(false);
}
isKeyboardOpen = false;
} else {
isKeyboardOpen = true;
_onKeyboardChanged(true);
}
}
_onKeyboardChanged(bool isVisible) {
if (isVisible) {
print("KEYBOARD VISIBLE");
} else {
print("KEYBOARD HIDDEN");
}
}
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: TextField(),
),
);
}
}