[英]Circular Progress Indicator - Maths Questions
好的,我正在使用circularProgressIndicator
來顯示計時器何時完成,但是我的大腦已經停止了 function 並且我無法得到數學運算。
- 計時器值為 90 秒
- 進度指示器在 1 時為 100%,在 0.5 時為 50%,依此類推。
- 每秒我將 90 秒計時器減少 1 秒,所以顯示倒計時
當 90 秒的計時器用完時,我還想逐步添加進度指示器值以達到 100%。
90是一個例子,這個會定期改變
我試過的
1. _intervalProgress += (( 100 / intervalValueSeconds) / 60;
2. _intervalProgress += (( 100 / intervalValueSeconds) * ( 100 / 60 )) / 100;
問題:如何找到一個值的小數,然后除以 60 以便每秒迭代 <--- 等等,我剛剛意識到這個值不是 60 秒,它是計時器的值,因此我一直算錯。
使用以下邏輯
int value = ((yourValue/ 90) * 100).toInt(); // here 90 is higest value which you have
print(value);
例子:
int value = ((45/ 90) * 100).toInt();
print(value); // Output will be 50
您可以計算並顯示如下所示的進度。 根據您的問題,您似乎正在尋找變量progressFraction
的計算。
class _MyWidgetState extends State<MyWidget> {
int totalSeconds = 90;
int secondsRemaining = 90;
double progressFraction = 0.0;
int percentage = 0;
Timer timer;
@override
void initState() {
timer = Timer.periodic(Duration(seconds: 1), (_) {
if(secondsRemaining == 0){
return;
}
setState(() {
secondsRemaining -= 1;
progressFraction = (totalSeconds - secondsRemaining) / totalSeconds;
percentage = (progressFraction*100).floor();
});
});
super.initState();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
SizedBox(height: 20),
Text('$secondsRemaining seconds remaining'),
SizedBox(height: 20),
CircularProgressIndicator(
value: progressFraction,
),
SizedBox(height: 20),
Text('$percentage% complete'),
],
);
}
@override
void dispose(){
timer.cancel();
super.dispose();
}
}
dartpad 上的演示 - https://dartpad.dev/4fae5a168d5e9421562d0e813b907a01
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.