Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a border/corner radius to a LinearProgressIndicator in Flutter?

Tags:

I am trying to add a border radius to a LinearProgressIndicator in Flutter.

When I replace the LinearProgressIndicator with another widget (e.g. Text) in the code below, it works, as expected.

Container(   decoration: new BoxDecoration(       borderRadius:           new BorderRadius.all(const Radius.circular(20.0))),   child: LinearProgressIndicator(     value: _time,   ), )  

a busy cat

like image 390
oemera Avatar asked Aug 17 '19 07:08

oemera


2 Answers

1) Using Widget

 Container(           margin: EdgeInsets.symmetric(vertical: 20),           width: 300,           height: 20,           child: ClipRRect(             borderRadius: BorderRadius.all(Radius.circular(10)),             child: LinearProgressIndicator(               value: 0.7,               valueColor: AlwaysStoppedAnimation<Color>(Color(0xff00ff00)),               backgroundColor: Color(0xffD6D6D6),             ),           ),         ) 

enter image description here

2) Using dependency

List of different types indicator https://pub.dev/packages/percent_indicator

Try this template code

        child:  Padding(           padding: EdgeInsets.all(15.0),           child:  LinearPercentIndicator(             width: MediaQuery.of(context).size.width - 50,             animation: true,             lineHeight: 20.0,             animationDuration: 2000,             percent: 0.9,             linearStrokeCap: LinearStrokeCap.roundAll,             progressColor: Colors.greenAccent,           ),         ) 

enter image description here

like image 52
Amit Prajapati Avatar answered Sep 27 '22 22:09

Amit Prajapati


Edit: I just wanted to point out my answer doesn't account for rounding the end of the 'value' fill inside the bar, which the OP wanted. However it seems many people came to this question looking for the answer I provided, (as did I before I found the answer and came back to respond).

So if you need the inside of this indicator also to have a rounded edge, as of now I think the accepted answer from Amit is the best route. If the inside of the indicator can have a flat edge and you don't want to use a third party package, I think my answer below is still the best option.

Original:

You actually don't need to use a third party package, and can wrap your LinearProgressIndicator with the ClipRRect widget and give it a circular border radius. If you want to give it a specific thickness/height then you could use a container as an intermediary:

ClipRRect(   borderRadius: BorderRadius.circular(8),   child: Container(     height: 10,       child: LinearProgressIndicator(         value: 0.35, // percent filled         valueColor: AlwaysStoppedAnimation<Color>(Colors.orange),         backgroundColor: Color(0xFFFFDAB8),       ),     ),   ) 

would produce this, if placed in another widget with a defined width:

enter image description here

like image 38
Tristan Bennett Avatar answered Sep 27 '22 22:09

Tristan Bennett