Let's try the below solution, I tested it and it works quite well in your case.
下面是一个自定义的GestureDetector
小部件,它允许我们设置onLongPressStart
和onLongPressEnd
的持续时间
import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';
class CustomGestureLongPress extends StatelessWidget {
final Widget? child;
final Duration duration;
final GestureLongPressStartCallback onLongPressStart;
final GestureLongPressEndCallback onLongPressEnd;
final VoidCallback? onLongPress;
const CustomGestureLongPress({
super.key,
this.child,
required this.duration,
required this.onLongPressStart,
required this.onLongPressEnd,
this.onLongPress,
});
@override
Widget build(BuildContext context) {
return RawGestureDetector(
behavior: HitTestBehavior.translucent,
gestures: <Type, GestureRecognizerFactory>{
LongPressGestureRecognizer: GestureRecognizerFactoryWithHandlers<LongPressGestureRecognizer>(
() => LongPressGestureRecognizer(duration: duration), (instance) {
instance.onLongPress = () {
print("Parent onLongPress");
onLongPress?.call();
};
instance.onLongPressStart = (details) {
print("Parent onLongPressStart");
onLongPressStart.call(details);
};
instance.onLongPressEnd = (details) {
print("Parent onLongPressEnd");
onLongPressEnd.call(details);
};
},
),
},
child: child,
);
}
}`
以及自定义小部件的使用
CustomGestureLongPress(
duration: const Duration(milliseconds: 200),
onLongPressStart: (_) {
//TODO Pause the loader
},
onLongPressEnd: (_) {
//TODO Resume the loader
},
child: Stack(
children: [
GestureDetector(
onTap: () {
//TODO Go to previous story
},
child: Align(
alignment: Alignment.centerLeft,
child: Container(
width: 50.w,
color: Colors.transparent,
),
)
),
GestureDetector(
onTap: () {
//TODO Go to next story
},
child: Align(
alignment: Alignment.centerRight,
child: Container(
width: 50.w,
color: Colors.transparent,
),
)
)
]
),
)