skip to Main Content

I want to create Container and on its border I want to show a Ring Progress Bar how can I implement it can anybody help me

Like given below Image:

The yellow color circle is a container and I want to show the progress bar on the container border and in the middle I want to show progress percentage

enter image description here

2

Answers


  1. Here’s a minimal code that achieves something very close to what you want using CustomPaint and TweenAnimationBuilder.

    Screen from dartpad:

    enter image description here

    Code:

    import 'package:flutter/material.dart';
    import 'dart:math';
    
    const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData.dark().copyWith(
            scaffoldBackgroundColor: darkBlue,
          ),
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            body: Center(
              child: MyWidget(),
            ),
          ),
        );
      }
    }
    
    class BorderPainter extends CustomPainter {
      final double currentState;
    
      BorderPainter({required this.currentState});
    
      @override
      void paint(Canvas canvas, Size size) {
    
        double strokeWidth = 5;
        Rect rect = const Offset(5,5) & Size(size.width - 10, size.height - 10);
    
        var paint = Paint()
          ..color = Colors.white
          ..strokeWidth = strokeWidth
          ..strokeCap = StrokeCap.round
          ..style = PaintingStyle.stroke;
    
          double startAngle = -pi / 2;
          double sweepAmount = currentState * pi;
    
          canvas.drawArc(rect, startAngle, sweepAmount, false, paint);
      }
    
    
      @override
      bool shouldRepaint(covariant CustomPainter oldDelegate) {
        return true;
      }
    }
    
    class MyWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Center(
          child: TweenAnimationBuilder(
            tween: Tween(begin: 0.0, end: 2.0),
            duration: const Duration(seconds: 10),
            builder: (context, value, child) {
              return Container(
                width: 100,
                height: 100,
                decoration: const BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.orange
                ),
                child: CustomPaint(
                  painter: BorderPainter(currentState: value),
                  child: Center(
                    child: Text(
                      '${value * 100 ~/ 2.0}%',
                    ),
                  ),
                ),
              );
            }
          ),
        );
      }
    }
    

    Credits to this great article: https://medium.com/@callumdixon941/creating-awesome-animated-custom-borders-in-flutter-with-canvas-90cf755cc29f

    Login or Signup to reply.
  2. You can make use of the flutter circularProgressIndicator class as given in the example here : https://api.flutter.dev/flutter/material/CircularProgressIndicator-class.html

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search