【发布时间】:2022-02-07 08:44:07
【问题描述】:
我尝试使用 Timer 类,但这只从固定持续时间 AFAIK 开始倒计时。我有一个按钮,用户可以单击它或长按并按住它 X 秒,最多 10 秒。我已经弄清楚了单击代码,但是如果用户按住按钮,我想记录他按住按钮的时间并执行其他操作。
Timer startRecordingTimer() => Timer(Duration(milliseconds: 10 * 1000), handleTimeout);
void handleTimeout() {
//when startRecordingTimer() reaches 0, do the following
stopRecording();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Align(
alignment: Alignment.bottomLeft,
child: Column(
children: [
Row(
children: [
GestureDetector(
onTap: () async {
startRecording();
},
onLongPressDown: (details) {
startRecordingTimer();
startRecording();
},
onLongPressUp: () {
//Need to measure how long user held down button
//Do other stuff using the aforementioned duration
},
),
],
),
],
),
)
);
}
【问题讨论】: