圆形进度指示器 - 数学问题

3

好的,我正在使用来显示计时器完成时的进度,但是我的大脑已经停止工作,我无法让它正常工作。

  1. 计时器值为90秒
  2. 进度指示器在1时为100%,在0.5时为50%等等。
  3. 每秒钟我减少90秒计时器1秒钟,以显示倒计时。

我还希望逐步增加进度指示器的值,以在90秒的计时器运行完毕时达到100%。

90只是一个示例,这将经常更改。

我尝试过的方法:

1. _intervalProgress += (( 100 / intervalValueSeconds) / 60;
2. _intervalProgress += (( 100 / intervalValueSeconds) * ( 100 / 60 )) / 100; 

问题:如何找到一个值的小数部分,然后除以60以便每秒迭代<-等等,我刚意识到这个值不是60秒,而是计时器的值,因此一直以来我都算错了。

看起来我把它复杂化了。_intervalProgress += (1 / intervalValueSeconds); - Yonkee
2个回答

1
使用以下逻辑。
 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

1
您可以像下面这样计算并显示进度。根据您的问题,似乎您正在寻找变量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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接