flutter - 在小部件后面传播点击
问题描述
我有一个堆栈,里面有两个小部件。我正在尝试检测对堆栈底部小部件的点击,它位于顶部小部件的后面。
我正在使用HitTestBehavior.translucent
,但只有在GestureDetector
没有孩子的情况下才有效。
这是我在我的应用程序中需要的简化版本。我有一个堆栈,其中包含屏幕上的许多小卡片,并且所有这些卡片上都有一个画布。尽管有画布,但我需要能够点击卡片来更改其内容。我认为使用半透明行为可以解决我的问题,但事实并非如此。
编辑:另外,第一个GestureDetector
总是在一个Positioned
小部件中。
class TestPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
height: 800,
width: 400,
child: Stack(
children: [
/// 2. ... and trigger the onTap function of this widget
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
print('TAP BOTTOM');
},
child: Container(
height: 500,
width: 400,
color: Colors.deepOrange,
),
),
/// 1. I'm Trying to clic here...
GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: null,
child: Container(
height: 300,
width: 400,
color: Colors.deepPurple,
),
),
],
),
),
// ),
),
);
}
}
解决方案
我有一个示例代码,您可以使用它来实现此目的:
class TestPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
height: 800,
width: 400,
child: Stack(
children: [
/// 2. ... and trigger the onTap function of this widget (WIDGET_2)
GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
print('TAP BOTTOM');
},
child: Container(
height: 500,
width: 400,
color: Colors.deepOrange,
),
),
/// Able to tap bottom
IgnorePointer(
ignoring: true,
child: Container(
height: 300,
width: 400,
color: Colors.deepPurple,
),
),
],
),
),
),
);
}
}
也很抱歉在这里发迟了。
推荐阅读
- node.js - 如何阻止黑客让我的 Express 服务器离线?
- java - 无法打开某些活动 java 文件
- laravel - 不会在多态关系 laravel 中显示“提交”表数据,但如果使用 dd () 会出现数据
- html - Bootstrap 4移动导航未打开
- swift - 哪些类型可以用作 Swift 中的错误?
- php - 如何使用任务计划程序在每天运行时按需将文件上传到数据库
- ios - iOS 12 中的相机在没有相机权限的情况下工作对话框
- javascript - 如何在 Hover 上制作 React 动画?
- css - 为什么在 css 中使用 :hover 时这个导航栏悬停不起作用?
- python - 需要帮助使用 python list 为以下输入生成输出