flutter
flutter

Reputation: 6786

increment progress for upload progress

I'm receiving progress from a callback and adding an event like this:

Data/Bloc

onSendProgress: (int sent, int total) {
                  var progress = sent / total;
                  this.add(ProgressUpdate(progress));
                },

The progress is too fast for the animation in my ui.

UI

child: CircularPercentIndicator(
                  radius: 130.0,
                  animation: true,
                  animationDuration: 1200,
                  lineWidth: 15.0,
                  percent:state.progress, //Percent value between 0.0 and 1.0
                  center: new Text(
                    "Loading",
                    style: new TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),
                  ),
                  circularStrokeCap: CircularStrokeCap.butt,
                  backgroundColor: AppTheme.backgroundColor,
                  progressColor: AppTheme.darkOrange,
                ),

wondering what's the best way to increment the response in intervals of 10 ?

Upvotes: 0

Views: 299

Answers (1)

eeqk
eeqk

Reputation: 3862

Assuming you have an access to the current value (as in case of ValueStream from rxdart) you could do something like this:

void main() {
  var currentIncrement = 0;
  for (var i = 0; i <= 100; i++) {
    final increment = i ~/ 10;
    if(increment > currentIncrement) {
      currentIncrement = increment;
      print('Progress updated! ${currentIncrement * 10}%');
    }
  }
}

Prints:

Progress updated! 10%
Progress updated! 20%
Progress updated! 30%
Progress updated! 40%
Progress updated! 50%
Progress updated! 60%
Progress updated! 70%
Progress updated! 80%
Progress updated! 90%
Progress updated! 100%

in your case it'd look similarly to this (I haven't compiled it, so it will need some tweaking):

onSendProgress: (sent, total) {
  final progress = (sent / total * 100) ~/ 10;

  if (progress > currentValue) {
    this.add(ProgressUpdate(progress));
  }
}

breakdown:

  • sent / total - fraction that represents a progress, e.g. 0.32 for 32%
  • sent / total * 100 - an actual percentage, e.g. 32%
  • ~/ 10 this one divides a number discarding any fractions, so if you do 32 ~/ 10 you will get 3

Upvotes: 1

Related Questions