在颤动中按下按钮时自动增加一个值 [英] Auto increment a value as the button is pressed in flutter
本文介绍了在颤动中按下按钮时自动增加一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个计算应用程序,其中通过加号按钮和减号按钮来增加/减少变量。长按(按住)加号或减号按钮时,我想实现连续的递增/递减。
如何用dart / flutter来完成?
I am developing a calculation app where a variable is increased/decreased by a plus button and a minus button. I would like to implement a continuous increment/decrement when long-pressing (holding) the plus or minus button. How can this be done with dart/flutter?
推荐答案
只需一个简单的示例代码。
Just a quick sample code.
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: NewCode(),
debugShowCheckedModeBanner: false,
);
}
}
class NewCode extends StatefulWidget {
@override
_NewCodeState createState() => _NewCodeState();
}
class _NewCodeState extends State<NewCode> {
Timer timer;
var value = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: GestureDetector(
child: Container(
width: 200,
height: 200,
color: Colors.blue,
child: Center(
child: Text(
'value $value',
style: TextStyle(fontSize: 40),
)),
),
onTapDown: (TapDownDetails details) {
print('down');
timer = Timer.periodic(Duration(milliseconds: 500), (t) {
setState(() {
value++;
});
print('value $value');
});
},
onTapUp: (TapUpDetails details) {
print('up');
timer.cancel();
},
onTapCancel: () {
print('cancel');
timer.cancel();
},
),
),
);
}
}
这篇关于在颤动中按下按钮时自动增加一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文