IgnorePointer 在 Flutter 的 GridView 中不起作用
the IgnorePointer is not working in the GridView in Flutter
class _B extends State<B>{
...
// a 2*2 grid layout
Widget _a() {
var c = Container(
child: GridView.count(
crossAxisCount: 2,
children: [_b(), _c(), _c(), _b()],
key: ObjectKey("1"),
),
width: 300,
height: 300);
var l = Listener(
child: c,
onPointerDown: (event) => print("hello"),
);
return l;
}
// normal widget, it's red
Widget _b() {
var l = Listener(
// behavior: HitTestBehavior.deferToChild,
onPointerDown: (event) => print(0),
child: Listener(
onPointerDown: (event) => print(1),
child: Listener(
onPointerDown: (event) => print(2),
child: Container(
color: Colors.red,
height: 300,
width: 300,
),
),
),
);
return l;
}
// should be ignored, it's blue
Widget _c() {
var l = Listener(
// behavior: HitTestBehavior.deferToChild,
onPointerDown: (event) => print(0),
child: Listener(
onPointerDown: (event) => print(1),
child: Listener(
onPointerDown: (event) => print(2),
child: Container(
color: Colors.blue,
height: 300,
width: 300,
),
),
),
);
var i = IgnorePointer(
child: l,
);
return Listener(child: i, onPointerDown: (event) => print("3!"));
}
}
- 当我点击红色的时,这是输出。刚好符合我的预期。
[+1052 ms] 2
[ +1 ms] 1
[ ] 0
[ +1 ms] hello
- 但是当我点击蓝色的时,这是输出。不是应该忽略吗?事件是怎么分发到上层widget的?
[+2612 ms] hello
试了很多方法还是不行
这是图片,抱歉。
image
这里比较棘手。
它实际上忽略了蓝色方块上的事件,但你在整个 GridView
布局上也有一个监听器,即使你创建一个没有监听器的小部件并将其添加到你的 GridView
。
我的建议是从您的顶级布局中删除 Listener
并处理您的小部件或组件中的所有内容。
Widget _a() {
var c = Container(
child: GridView.count(
crossAxisCount: 2,
children: [_b(), _c(), _c(), _b()],
key: ObjectKey("1"),
),
width: 300,
height: 300);
return c;
}
class _B extends State<B>{
...
// a 2*2 grid layout
Widget _a() {
var c = Container(
child: GridView.count(
crossAxisCount: 2,
children: [_b(), _c(), _c(), _b()],
key: ObjectKey("1"),
),
width: 300,
height: 300);
var l = Listener(
child: c,
onPointerDown: (event) => print("hello"),
);
return l;
}
// normal widget, it's red
Widget _b() {
var l = Listener(
// behavior: HitTestBehavior.deferToChild,
onPointerDown: (event) => print(0),
child: Listener(
onPointerDown: (event) => print(1),
child: Listener(
onPointerDown: (event) => print(2),
child: Container(
color: Colors.red,
height: 300,
width: 300,
),
),
),
);
return l;
}
// should be ignored, it's blue
Widget _c() {
var l = Listener(
// behavior: HitTestBehavior.deferToChild,
onPointerDown: (event) => print(0),
child: Listener(
onPointerDown: (event) => print(1),
child: Listener(
onPointerDown: (event) => print(2),
child: Container(
color: Colors.blue,
height: 300,
width: 300,
),
),
),
);
var i = IgnorePointer(
child: l,
);
return Listener(child: i, onPointerDown: (event) => print("3!"));
}
}
- 当我点击红色的时,这是输出。刚好符合我的预期。
[+1052 ms] 2
[ +1 ms] 1
[ ] 0
[ +1 ms] hello
- 但是当我点击蓝色的时,这是输出。不是应该忽略吗?事件是怎么分发到上层widget的?
[+2612 ms] hello
试了很多方法还是不行
这是图片,抱歉。 image
这里比较棘手。
它实际上忽略了蓝色方块上的事件,但你在整个 GridView
布局上也有一个监听器,即使你创建一个没有监听器的小部件并将其添加到你的 GridView
。
我的建议是从您的顶级布局中删除 Listener
并处理您的小部件或组件中的所有内容。
Widget _a() {
var c = Container(
child: GridView.count(
crossAxisCount: 2,
children: [_b(), _c(), _c(), _b()],
key: ObjectKey("1"),
),
width: 300,
height: 300);
return c;
}