以下代码段只是我实际情况的最小化版本。在我的实际情况中,这些GestureDetectors
位于不同的小部件中。我的问题是onVerticalDragUpdate
事件仅由内部GestureDetector接收。我什至将内部behavior
的GestureDetector
设置为HitTestBehavior.translucent
,这意味着该事件应该冒泡到父窗口小部件。还是我出了点问题?
void main() {
debugPaintPointersEnabled = true;
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return GestureDetector(
onVerticalDragUpdate: (details) {
var test = "test";
},
child: GestureDetector(
behavior: HitTestBehavior.translucent,
onVerticalDragUpdate: (details) {
var test = "test";
},
child: Container(height: 100, width: 100, color: Colors.red),
));
}
}
mmm这是您需要的吗?
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return GestureDetector(
behavior: HitTestBehavior.deferToChild,
onVerticalDragUpdate: (details) {
var test1 = "test1";
print(test1);
},
child: GestureDetector(
onHorizontalDragUpdate: (details) {
var test2 = "test2";
print(test2);
},
child: Container(height: 100, width: 100, color: Colors.red),
));
}
}
[顶部小部件是垂直的'test1',内部小部件是draghoritzontal'test2'。
希望对尼克拉斯有帮助;-)
对于所有感兴趣的人,这就是我解决的方法:
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return RawGestureDetector(
gestures: {
AllowMultipleVerticalDragGestureRecognizer: GestureRecognizerFactoryWithHandlers<
AllowMultipleVerticalDragGestureRecognizer>(
() => AllowMultipleVerticalDragGestureRecognizer(),
(AllowMultipleVerticalDragGestureRecognizer instance) {
instance..onEnd = (_) => print("test1");
},
)
},
child: RawGestureDetector(
gestures: {
AllowMultipleVerticalDragGestureRecognizer: GestureRecognizerFactoryWithHandlers<
AllowMultipleVerticalDragGestureRecognizer>(
() => AllowMultipleVerticalDragGestureRecognizer(),
(AllowMultipleVerticalDragGestureRecognizer instance) {
instance..onEnd = (_) => print("test2");
},
)
},
child: Container(color: Colors.red),
));
}
}
class AllowMultipleVerticalDragGestureRecognizer extends VerticalDragGestureRecognizer{
@override
void rejectGesture(int pointer) {
acceptGesture(pointer);
}
}
信用:https://gist.github.com/Nash0x7E2/08acca529096d93f3df0f60f9c034056