英文:
How to know exactly the moment the Back button is pressed to dismiss the Soft keyboard in flutter
问题
I don't want to wait until the Soft keyboard has been dismissed.
我不想等到软键盘被关闭才行。
The moment the user presses the back button to dismiss the keyboard I want to also at the same speed and time dismiss something else as well.
当用户按下返回按钮关闭键盘时,我想以相同的速度和时间关闭其他东西。
I attempted the following code to know when the user has pressed the back button to dismiss the soft keyboard
我尝试了以下代码来确定用户何时按下返回按钮以关闭软键盘:
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
print("Back button has been pressed"); //This seems not to be called when the soft keyboard is been dismissed
return true;
},
child: TextButton("Sample Button")),
);
}
I was surprised that the WillPopScope widgets' onWillPop
method wasn't called when the soft keyboard was dismissed.
我对WillPopScope小部件的onWillPop
方法在软键盘被关闭时没有被调用感到惊讶。
I don't want to use Keyboard Visibility package because that will only fire when the keyboard has been dismissed. Instead I want to know immediately the back button was pressed to dismiss the keyboard at that point in time.
我不想使用键盘可见性包,因为它只在键盘被关闭时触发。相反,我想立即知道返回按钮何时被按下以关闭键盘。
英文:
I don't want to wait until the Soft keyboard has been dismissed.
The moment the user presses the back button to dismiss the keyboard I want to also at the same speed and time dismiss something else as well.
I attempted the following code to know when the user has pressed the back button to dismiss the soft keyboard
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
print(
"Back button has been pressed"); //This seems not to be called when the soft keyboard is been dismissed
return true;
},
child: TextButton("Sample Button")),
);
}
I was surprised that the WillPopScope widgets' onWillPop
method wasn't called when the softkeyboard was dismissed.
I don't want to use Keyboard Visibility package because that will only fire when the keyboard has been dismissed. Instead I want to know immediately the back button was pressed to dismiss the keyboard at that point in time.
答案1
得分: 1
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State
final _focusNode = FocusNode();
@override
void initState() {
super.initState();
_focusNode.addListener(_onFocusChange);
}
@override
void dispose() {
_focusNode.removeListener(_onFocusChange);
super.dispose();
}
void _onFocusChange() {
if (!_focusNode.hasFocus) {
print('Back button was pressed to dismiss keyboard');
// Perform action you want here
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: ElevatedButton(
onPressed: () {},
child: Text('Sample Button'),
autofocus: true,
focusNode: _focusNode,
),
),
),
);
}
}
英文:
class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
final _focusNode = FocusNode();
@override
void initState() {
super.initState();
_focusNode.addListener(_onFocusChange);
}
@override
void dispose() {
_focusNode.removeListener(_onFocusChange);
super.dispose();
}
void _onFocusChange() {
if (!_focusNode.hasFocus) {
print('Back button was pressed to dismiss keyboard');
// Perform action you want here
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: ElevatedButton(
onPressed: () {},
child: Text('Sample Button'),
autofocus: true,
focusNode: _focusNode,
),
),
),
);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论