[英]Flutter. A way for TextField to detect tap outside
Is there any way to detect a tap outside of TextTield?有没有办法检测 TextTield 之外的点击? I wanna make a self sufficient search field component which should expend when focused and shrink when a focus is lost.
我想制作一个自给自足的搜索字段组件,它应该在聚焦时消耗并在失去焦点时缩小。 The TextField is using FocusNode to unfocus itself when the focus is not needed but the problem is that I can't get the way to detect a tap outside of it.
当不需要焦点时,TextField 使用 FocusNode 来取消焦点,但问题是我无法检测到它之外的点击。 Wrapping the whole app in GestureDetector and requesting a new focus on tap is not an option at all because, first this tap can be easily intercepted by any component containing their own gesture detectors, second it will make my component not self sufficient and I'll have to write some extra code outside of it, which is not preferable
将整个应用程序包装在 GestureDetector 中并请求重新关注点击根本不是一种选择,因为首先这个点击可以被任何包含自己的手势检测器的组件轻松拦截,其次会使我的组件无法自给自足,我会必须在它之外编写一些额外的代码,这是不可取的
here is the code of my search field for the moment这是我目前搜索字段的代码
@override
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(_padding),
child: AnimatedContainer(
decoration: BoxDecoration(
border: Border.all(color: Colors.black12, width: 2),
borderRadius: BorderRadius.all(Radius.circular(6)),
color: pizzaWhite,
),
height: 40,
width: !_isExpanded
? MediaQuery.of(context).size.width / 2 - (_padding * 2)
: MediaQuery.of(context).size.width - (_padding * 2),
child: Padding(
padding: const EdgeInsets.only(left: _padding, right: _padding),
child: Row(
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Expanded(
child: RawKeyboardListener(
focusNode: _keyboardFocusNode,
onKey: (RawKeyEvent e) {
},
child: TextField(
keyboardType: TextInputType.text,
onEditingComplete: _sendSearch,
controller: _controller,
focusNode: _textFocusNode,
cursorColor: pizzaBottomBarColor,
onTap: () => _switchExpanded(true),
decoration: InputDecoration(
alignLabelWithHint: true,
contentPadding: EdgeInsets.only(top: 1),
border: InputBorder.none,
hintText: 'Search...'
),
style: TextStyle(
fontSize: 18,
fontFamily: 'OpenSans',
fontWeight: FontWeight.w100,
),
),
),
),
GestureDetector(
onTap: () => _switchExpanded(false),
child: Icon(
Icons.search
),
),
],
),
),
duration: Duration(milliseconds: 250),
curve: Curves.fastOutSlowIn,
),
)
],
);
}
https://pub.dev/packages/flutter_focus_watcher https://pub.dev/packages/flutter_focus_watcher
You can also filnd it on my github你也可以在我的github上找到
https://github.com/caseyryan/flutter_focus_watcher https://github.com/caseyryan/flutter_focus_watcher
you need to separate from the other widgets and wrap them in GestureDetector
and call it's onTap | onTapDown
您需要与其他小部件分开并将它们包装在
GestureDetector
并调用它的onTap | onTapDown
onTap | onTapDown
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.