gpt4 book ai didi

flutter - 在 flutter 中按下按钮时自动增加一个值

转载 作者:行者123 更新时间:2023-12-03 03:01:10 25 4
gpt4 key购买 nike

我正在开发一个计算应用程序,其中一个变量通过加号按钮和减号按钮增加/减少。我想在长按(按住)加号或减号按钮时实现连续递增/递减。
如何用 Dart/flutter 来做到这一点?

最佳答案

只是一个快速的示例代码。

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();
},
),
),
);
}
}

关于flutter - 在 flutter 中按下按钮时自动增加一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59417182/

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