Flutter:当 GestureDetector 中有一个 ListView 时,它显然没有收到 Drag 事件
Flutter: GestureDetector apparently doesn't receive Drag events when it has a ListView in it
我错过了什么吗?文档说事件从最里面的 child 冒泡到祖先,但下面的代码不会将 "dragged" 打印到控制台。它确实打印 "tapped" 虽然。
将 NeverScrollablePhyscs 应用于 ListView 确实有效,但我想在两个级别上收听事件。将 HitTestBehavior.translucent 应用于 GestureDetector 不会改变任何内容。
import "package:flutter/material.dart";
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onVerticalDragUpdate: (DragUpdateDetails details) {
print("dragged");
},
onTap: () {
print("tapped");
},
child: ListView.builder(
itemBuilder: (context, index) {
return Container(
padding: EdgeInsets.all(20.0),
child: Text(
"The GestureDetector above me does not react to drag events. Maybe my parent is at fault?"
)
);
},
)
)
);
}
}
您可以使用 Listener
监听原始指针事件
return Scaffold(
body: Listener(onPointerMove: (opm) {
print("onPointerMove .. ${opm.position}");
}, child: ListView.builder(
itemBuilder: (context, index) {
return Container(
padding: EdgeInsets.all(20.0),
child: Text(
"The GestureDetector above me does not react to drag events. Maybe my parent is at fault?"));
},
)));
我错过了什么吗?文档说事件从最里面的 child 冒泡到祖先,但下面的代码不会将 "dragged" 打印到控制台。它确实打印 "tapped" 虽然。 将 NeverScrollablePhyscs 应用于 ListView 确实有效,但我想在两个级别上收听事件。将 HitTestBehavior.translucent 应用于 GestureDetector 不会改变任何内容。
import "package:flutter/material.dart";
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: GestureDetector(
onVerticalDragUpdate: (DragUpdateDetails details) {
print("dragged");
},
onTap: () {
print("tapped");
},
child: ListView.builder(
itemBuilder: (context, index) {
return Container(
padding: EdgeInsets.all(20.0),
child: Text(
"The GestureDetector above me does not react to drag events. Maybe my parent is at fault?"
)
);
},
)
)
);
}
}
您可以使用 Listener
return Scaffold(
body: Listener(onPointerMove: (opm) {
print("onPointerMove .. ${opm.position}");
}, child: ListView.builder(
itemBuilder: (context, index) {
return Container(
padding: EdgeInsets.all(20.0),
child: Text(
"The GestureDetector above me does not react to drag events. Maybe my parent is at fault?"));
},
)));