Thursday, September 28, 2023

Circular progress bar in flutter

Flutter Animated Loader

A highly customizable circular slider/progress bar & spinner for Flutter.



sleek_circular_slider : ^lastest_version

to your pubspec.yaml, and run

flutter packages get

in your project’s root directory.

Basic Usage

Import it to your project file

import 'package:sleek_circular_slider/sleek_circular_slider.dart';

And add it in its most basic form like it:

final slider = SleekCircularSlider(
                      appearance: CircularSliderAppearance(),
                      onChange: (double value) {

There are additional optional parameters one can initialize the slider with.

final slider = SleekCircularSlider(
  min: 0,
  max: 1000,
  initialValue: 426,
  onChange: (double value) {
    // callback providing a value while its being changed (with a pan gesture)
  onChangeStart: (double startValue) {
    // callback providing a starting value (when a pan gesture starts)
  onChangeEnd: (double endValue) {
    // ucallback providing an ending value (when a pan gesture ends)
  innerWidget: (double value) {
    // use your custom widget inside the slider (gets a slider value from the callback)

Use as a progress bar

Slider user’s interaction will be disabled if there is either no [onChange] or [onChangeEnd] provided. That way one can use the widget not as a slider but as a progress bar.

final slider = SleekCircularSlider(
  appearance: CircularSliderAppearance(
    customWidths: CustomSliderWidths(progressBarWidth: 10)),
  min: 10,
  max: 28,
  initialValue: 14,

Use as a spinner

There is one more use case for the library. It’s a spinner which can be shown to users while loading etc. In this mode the widget is not interactive and ignores all angles so there is no need to provide them.

final slider = SleekCircularSlider(
  appearance: CircularSliderAppearance(
    spinnerMode: true,

SleekCircularSlider parameters

appearance CircularSliderAppearanceA set of objects describing the slider look and feel.
min double0The minimum value the user can select. Must be less than or equal to max.
max double100The maximum value the user can select. Must be greater than or equal to min.
initialValue double50The initial value for this slider.
onChange OnChange(double value)Called during a drag when the user is selecting a new value for the slider by dragging.
onChangeStart OnChange(double value)Called when the user starts selecting a new value for the slider.
onChangeEnd OnChange(double value)Called when the user is done selecting a new value for the slider.
innerWidget Widget InnerWidget(double value)A custom widget to replace the build in text labels which can capture a slider value from the callback.

CircularSliderAppearance parameters

size double150The width & height value for the slider.
startAngle double150The angle (in degrees) the slider begins with.
angleRange double240The angle range (in degrees) the slider reaches when maximum value set.
counterClockwise boolfalseThe setting indicating direction of the widget.
customWidths CustomSliderWidthsThe object with a set of widths for the track, bar, shadow etc.
customColors CustomSliderColorsThe object with a set of colors for the track, bar, shadow etc.
infoProperties InfoPropertiesThe object with a set of properties for internal labels displaying a current slider value.
animationEnabled booltrueThe setting indicating whether external changes of a slider value should be animated.
spinnerMode boolfalseThe setting turning the widget into a spinner.
spinnerDuration int1500The spinner animation duration in miliseconds
animDurationMultiplier double1.0The multiplier of duration for the animation when value changed

CustomSliderWidths parameters

trackWidth doubleprogressBarWidth / 4The width of the slider’s track.
progressBarWidth doubleslider’s size / 10The width of the slider’s progress bar.
shadowWidth doubleprogressBarWidth * 1.4The width of the slider’s shadow.
handlerSize doubleprogressBarWidth / 5The size of the slider’s handler.

CustomSliderColors parameters

trackColor Color#DCBEFBThe color of the slider’s track.
trackColors ListnullThe list of colors for the track’s gradient.
trackGradientStartAngle double0The start angle for the track’s gradient.
trackGradientEndAngle double180The end angle for the track’s gradient.
progressBarColor ColorThe color of the slider’s progress bar. Won’t be used if the slider uses gradient progressBarColors != null
progressBarColors List[#1E003B, #EC008A, #6285DA]The list of colors for the progress bar’s gradient.
gradientStartAngle double0The start angle for the progress bar’s gradient.
gradientEndAngle double180The end angle for the progress bar’s gradient.
dynamicGradient boolfalseThe gradient angles will change dynamically with value changing. If true it will ignore both the grandientStartAngle and gradientEndAngle
dotColor Color#FFFFFFThe color of the slider’s handle.
hideShadow boolfalseThe setting indicating whether the shadow should be showed.
shadowColor Color#2C57C0The color of the shadow.
shadowMaxOpacity double0.2The opacity of the shadow in its darker part.
shadowStep doubleThe shadow is being painted with a number of steps. This value determines how big is a width of each step. The more steps are painted the softer the shadow is. For a flat shadow use a difference between the shadowWidth and the progressWidth for the shadowStep.

InfoProperties parameters

mainLabelStyle TextStyleThe text style of the main text widget displaying a slider’s current value.
topLabelStyle TextStyleThe text style of the top text widget.
bottomLabelStyle TextStyleThe text style of the bottom text widget.
topLabelText StringThe text for the top text widget.
bottomLabelText StringThe text for the bottom text widget.
modifier String PercentageModifier(double percentage)closure adding the % characterThe closure allowing to modify how a current value of the slider is displayed.

Example of the modifier

String percentageModifier(double value) {
   final roundedValue = value.ceil().toInt().toString();
   return '$roundedValue %';

It will convert a current value to int and add the % sufix to it.

YouTube video

YouTube Video of the example in action