I created custom TextField widget :
class ActionTextField extends StatefulWidget {
final String label;
final String errorText;
final FocusNode focusNode;...
const ActionTextField(
{Key key,
this.label,
this.focusNode,...
child: Column(
children: [
Material(
elevation: 1.5,
shadowColor: Colors.grey,
child: Stack(
children: [
TextField(
textAlignVertical: TextAlignVertical.center,
obscureText: widget.isPassword,
focusNode: widget.focusNode,
controller: widget.controller ?? widget.controller,
Now I am using my widget into my project.Now i need to detect delete soft keyboard so in order to i use RawKeyboardListener
according it's official page.
final FocusNode _focusNode = FocusNode();
void _handleKeyEvent(RawKeyEvent event) {
print("0000000000");
if (event.logicalKey == LogicalKeyboardKey.delete) {
//do whatever you have to do
print("0000000000");
}
}
Widget build(BuildContext context) {...
RawKeyboardListener(
focusNode: _focusNode,
onKey: _handleKeyEvent,
child: ActionTextField(
label: "Test",
focusNode: _focusNode,
But when i passed _focusNode
into my custom view i got error:
════════ Exception caught by widgets library ═══════════════════════════════════
Tried to make a child into a parent of itself.
'package:flutter/src/widgets/focus_manager.dart':
Failed assertion: line 959 pos 12: 'child != this'
The relevant error-causing widget was
TextField
And when i comment _focusNode from my custom view, I can not detect my key from softkeyboard!!! How can i detect delete soft keyboard ?
The problem is that you use the same FocusNode object in parent and child widgets. You need to exclude TextField from RawKeyboardListener widget, for example:
@override
Widget build(BuildContext context) {
final focusNode = FocusNode();
return Scaffold(
body: SafeArea(
child: Row(
children: [
RawKeyboardListener(
focusNode: focusNode,
child: const SizedBox.shrink(),
onKey: (e) => print(e),
),
Expanded(
child: TextField(
focusNode: focusNode,
),
)
],
),
),
);
}
In this case you will able to handle keyboard events in RawKeyboardListener and TextField will handle same events by self.
In some cases after handling event in RawKeyboardListener TextField loses focus. I use the following workaround to return focus to TextField:
onKey: (e) {
// Some code for event handling
// W/A for TextField focus loss
textFieldFocusNode.previousFocus();
SchedulerBinding.instance.scheduleFrameCallback((_) {
textFieldFocusNode.nextFocus();
});
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.