【问题标题】:Prevent touch event from reaching to parent widget防止触摸事件到达父小部件
【发布时间】:2020-03-09 14:20:57
【问题描述】:
在下面的例子中,当点击FlatButton 时,触摸事件也被传播到InkWell,它会显示波纹。有没有办法禁用这种行为?那么如果触摸被子小部件消耗,它不会到达小部件的父级?
InkWell(
onTap: () {},
child: Row(
children: <Widget>[
Text("Dummy text"),
FlatButton(onPressed: () {}, child: Text("Button"))
],
),
);
【问题讨论】:
标签:
flutter
flutter-layout
flutter-widget
【解决方案1】:
事件未到达父窗口小部件。默认情况下,扁平按钮具有那种涟漪效应。您可以禁用它,将 FlatButton 的 splashColor 设置为透明。 InkWell 也可以这样做。
这样
InkWell(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () {},
child: Row(
children: <Widget>[
Text("Dummy text"),
FlatButton(
splashColor: Colors.transparent,
onPressed: () {}, child: Text("Button"))
],
),
);
【解决方案2】:
我知道这有点太晚了,但如果有人正在寻找正确的答案。在这里。
InkWell(
onTap: () {},
child: Row(
children: <Widget>[
Text("Dummy text"),
FlatButton(onPressed: () {
FocusScope.of(context).requestFocus();
}, child: Text("Button")),
],
),
);
FocusScope.of(context).requestFocus() 将使按钮请求焦点,因此 InkWell 不会显示波纹。