自定义 Raw Material 按钮 - Flutter

Customized Raw Material Button - Flutter

我想将 属性 添加到 RawMaterialButton class,这样当我按住按钮时,它会不断增加变量的值。就像我们长时间按下手机相机的拍摄按钮时,它会点击多次拍摄,直到我们离开按钮。知道怎么做吗?

您可以将 GestureDetector 与计时器一起使用。当 onTapDown 事件触发时启动计时器。当 onTapUp 事件触发时停止计时器。使用 Timer.periodic() 计时器重复递增变量。有多个问题与此相关,例如:, 等。这是我为执行此操作而构建的小部件中的 onTapDown 和 onTapUp 方法(我希望在重复操作之前有一个初始延迟,因此要检查 timer.tick):

Timer _timer;
.
.
.
void _onTapDown() {
  <on pressed action>
  _timer = Timer.periodic(Duration(milliseconds: 200), (timer) {
    if (timer.tick < 3) {
      return;
    }
    if (widget == null) {
      timer.cancel();
      return;
    }
    <on pressed action>
  });
}

void _onTapUp() {
  if (_timer == null) {
    return;
  }
  _timer.cancel();
}