我知道onLongPress会在一段时间后触发(比如 500 毫秒左右)。但我想做的是当用户按下按钮 3 秒左右时触发一些操作。实际上我想设置 的持续时间onLongPress。
ElevatedButton(
onPressed: () => print('ok I\'m just fine'),
onLongPress: () => print('Trigger me when user presses me for like 3 seconds'),
style: ElevatedButton.styleFrom(
primary: Colors.red,
elevation: 4,
),
Run Code Online (Sandbox Code Playgroud)
您可以这样解决您的问题,将 GestureDetector 的 onPanCancel 和 onPanDown 与计时器结合使用。
class _MyHomePageState extends State<MyHomePage> {
Timer _timer;
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: GestureDetector(
onPanCancel: () => _timer?.cancel(),
onPanDown: (_) => {
_timer = Timer(Duration(seconds: 3), () { // time duration
// your function here
})
},
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
让我知道它是否适合您。
| 归档时间: |
|
| 查看次数: |
6171 次 |
| 最近记录: |