Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Implement Multiple Countdown Progress Bars in Flutter?

Tags:

flutter

dart

How can I implement instagram's story progress bar on the top. Multiple bars if multiple stories exist, and if there's only one then only one bar. A timer of 10 seconds. What I have so far kinda worked, but it doesn't work when there are 2 or more snaps.

Code

Container(
    height: 30,
    margin: EdgeInsets.only(top: 50.0),
    child: CustomPaint(
        foregroundPainter: ProgressPainter(value: (this._totalTime) / 10.0 / widget.posts.length),
    ),
)

I tried putting it into a listview but it kinda doesn't work.

Update:

I figured a "Row.builder" hack but how can I animate the progress? I'm using countdown as a timer solution and I can get the snaps to change at the end of the timer but the progress bar updating properly is the issue.

Row(
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: List.generate(widget.posts.length, (index) {
    if (this._index == index) {
      return Expanded(
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.0),
          height: 30,
          child: CustomPaint(
            foregroundPainter: ProgressPainter(value: this._countDown.remainingTime.inSeconds / 10.0),
          ),
        ),
      );
    } else {
      return Expanded(
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.0),
          height: 30,
          child: CustomPaint(
            foregroundPainter: ProgressPainter(value: 0.0),
          ),
        ),
      );
    }
  }).reversed.toList(),
)

Notes: I have the effect of the progress bar working but only for one. I have 2 posts, but only one progres bar works, the other remaains 0.

like image 532
Mohamed Mohamed Avatar asked Apr 15 '26 12:04

Mohamed Mohamed


1 Answers

Solved it.

The Countdown:

int _index = 0;
CountDown _countDown;
StreamSubscription _countDownSubscription;

@override
void initState() {
  super.initState();
  this._initTimer();
}

_initTimer() {
  if (mounted)
    setState(() {
      this._countDown = CountDown(Duration(seconds: widget.posts[this._index].timer));
      this._countDown.remainingTime = Duration(seconds: widget.posts[this._index].timer);
      this._countDownSubscription = this._countDown.stream.listen(null);
    });

  this._countDownSubscription.onData((d) {
    setState(() {}); // Updates the UI to animate the progress bar
  });

  this._countDownSubscription.onDone(() {
    if (widget.posts.length == this._index + 1) {
      Navigator.of(context).pop();
    } else {
      if (mounted)
        setState(() {
          this._index++;
          this._initTimer();
        });
    }
  });
}

The Progress Bar:

class ProgressPainter extends CustomPainter {
  final double value;

  ProgressPainter({this.value});

  Paint backgroundPaint = Paint()
    ..color = Colors.white
    ..style = PaintingStyle.stroke
    ..strokeCap = StrokeCap.round
    ..strokeWidth = 10;

  Paint valuePaint = Paint()
    ..color = Colors.deepPurple
    ..style = PaintingStyle.stroke
    ..strokeCap = StrokeCap.round
    ..strokeWidth = 10;

  @override
  void paint(Canvas canvas, Size size) {
    Path backgroundPath = Path();
    Path valuePath = Path();

    backgroundPath.moveTo(0, size.height / 2);
    backgroundPath.lineTo(size.width, size.height / 2);

    valuePath.moveTo(0, size.height / 2);
    valuePath.lineTo(size.width * this.value, size.height / 2);

    canvas.drawPath(backgroundPath, backgroundPaint);
    canvas.drawPath(valuePath, valuePaint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}

Dynamic Number of Progress Bars:

Row(
  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
  children: List.generate(widget.posts.length, (index) {
    if (this._index == index) { // Current active post
      return Expanded(
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.0),
          child: CustomPaint(
            foregroundPainter: ProgressPainter(value: this._countDown.remainingTime.inSeconds / 10.0),
          ),
        ),
      );
    } else if (this._index > index) { // when it's done
      return Expanded(
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.0),
          child: CustomPaint(
            foregroundPainter: ProgressPainter(value: 0.0),
          ),
        ),
      );
    } else { // When it's next
      return Expanded(
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: 10.0),
          child: CustomPaint(
            foregroundPainter: ProgressPainter(value: 1.0),
          ),
        ),
      );
    }
  }).reversed.toList(),
)

If y'all got a better and simpler solution I'm all for it.

like image 94
Mohamed Mohamed Avatar answered Apr 20 '26 04:04

Mohamed Mohamed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!