gpt4 book ai didi

flutter - flutter 设定事件的频率

转载 作者:行者123 更新时间:2023-12-03 04:42:22 24 4
gpt4 key购买 nike

假设我不断地更改Slider的值,并在onChanged回调函数中对服务器进行一些调用。是否可以有效地更改两次回调之间的最短时间?

  Slider showSoundBar(){
return Slider(
value: this.volume,
activeColor: Colors.blue,
onChanged: (vol){
// Don't send too often
send('volume', vol);
},
);
}

最佳答案

您可以使用Timer中的dart:async执行类似的操作。

class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {

Timer timer;
int timePassedInMilliseconds = 1500;

@override
void initState(){
super.initState();
timer = Timer.periodic(Duration(milliseconds: 100), (_){
timePassedInMilliseconds = timePassedInMilliseconds + 100;
});
}

@override
Widget build(BuildContext context) {
return Slider(
value: 10,
activeColor: Colors.blue,
onChanged: (vol){
// Don't send too often
if(timePassedInMilliseconds > 1500){
send('volume', vol);
timePassedInMilliseconds = 0;
}

},
);
}

void send(String sendWhat, double value){

}


@override
void dispose(){
timer.cancel();
super.dispose();
}

}

关于flutter - flutter 设定事件的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62980029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com