Flutter 在颤振中按下按钮时自动增加一个值
我正在开发一个计算应用程序,其中变量通过加号按钮和减号按钮进行增减。我想在长按(按住)加号或减号按钮时实现连续递增/递减。Flutter 在颤振中按下按钮时自动增加一个值,flutter,dart,Flutter,Dart,我正在开发一个计算应用程序,其中变量通过加号按钮和减号按钮进行增减。我想在长按(按住)加号或减号按钮时实现连续递增/递减。 如何使用“飞镖/颤振”完成此操作?只是一个简单的示例代码 class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: NewCode(), debugShowCheckedM
如何使用“飞镖/颤振”完成此操作?只是一个简单的示例代码
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();
},
),
),
);
}
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
主页:NewCode(),
debugShowCheckedModeBanner:false,
);
}
}
类NewCode扩展StatefulWidget{
@凌驾
_NewCodeState createState()=>\u NewCodeState();
}
类_NewCodeState扩展状态{
定时器;
var值=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:中(
儿童:手势检测器(
子:容器(
宽度:200,
身高:200,
颜色:颜色,蓝色,
儿童:中心(
子:文本(
“价值$value”,
样式:TextStyle(字体大小:40),
)),
),
onTapDown:(tappdown详细信息){
打印(“向下”);
计时器=计时器周期(持续时间(毫秒:500),(t){
设置状态(){
值++;
});
打印('value$value');
});
},
onTapUp:(TapUpDetails){
打印(‘向上’);
timer.cancel();
},
onTapCancel:(){
打印(“取消”);
timer.cancel();
},
),
),
);
}
}
这有帮助吗?不,不是真的,我需要一个值来增加/减少,而不是动画更改。因此,只需更新事件上的值即可。很抱歉,在该示例中,我看不到任何解决方案。当长按按钮时,您的示例中的哪一部分类似于变量的自动增量?也许我的问题不清楚。我可能需要重新表述我的问题。也许你可以使用手势检测器ontappown和ontappup。启动Timer.periodic/CountDownTimer,并在按下时增加该值。当ONTAPP启动时停止。