Skip to content

Commit a51fd75

Browse files
Merge pull request #334 from sarithavarrier/master
added drag option for circular progress bar in gf_progress_bar
2 parents 426db80 + 1cd1583 commit a51fd75

File tree

1 file changed

+57
-33
lines changed

1 file changed

+57
-33
lines changed

lib/components/progress_bar/gf_progress_bar.dart

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:math';
2+
13
import 'package:flutter/material.dart';
24
import 'package:getwidget/getwidget.dart';
35

@@ -140,6 +142,7 @@ class _GFProgressBarState extends State<GFProgressBar>
140142
Animation? _animation, circularAnimation;
141143
double _progressPercent = 0;
142144
double _percentage = 0;
145+
double _progress = 0.5;
143146

144147
@override
145148
void initState() {
@@ -210,7 +213,7 @@ class _GFProgressBarState extends State<GFProgressBar>
210213
? oldWidget.percentage
211214
: 0.0,
212215
end: widget.percentage)
213-
.animate(_animationController!);
216+
.animate(_animationController ?? AnimationController(vsync: this));
214217
_animationController?.forward(from: 0);
215218
} else {
216219
_updateprogressPercent();
@@ -226,7 +229,8 @@ class _GFProgressBarState extends State<GFProgressBar>
226229
? oldWidget.percentage
227230
: 0.0,
228231
end: widget.percentage)
229-
.animate(circularAnimationController!);
232+
.animate(circularAnimationController ??
233+
AnimationController(vsync: this));
230234
circularAnimationController?.forward(from: 0);
231235
} else {
232236
_updateProgress();
@@ -250,6 +254,17 @@ class _GFProgressBarState extends State<GFProgressBar>
250254
}
251255
}
252256

257+
void _onDragUpdateCircular(Offset position, Size size) {
258+
final dx = position.dx - size.width / 2;
259+
final dy = position.dy - size.height / 2;
260+
final angle = atan2(dy, dx);
261+
final progress = angle / (2 * pi) + 0.5;
262+
263+
setState(() {
264+
_progress = progress.clamp(0.0, 1.0);
265+
});
266+
}
267+
253268
@override
254269
Widget build(BuildContext context) {
255270
super.build(context);
@@ -268,7 +283,7 @@ class _GFProgressBarState extends State<GFProgressBar>
268283
child: widget.type == GFProgressType.linear
269284
? GestureDetector(
270285
onHorizontalDragUpdate: (details) {
271-
if (widget.isDragable!) {
286+
if (widget.isDragable == true) {
272287
_onDragUpdate(context, details);
273288
}
274289
},
@@ -288,21 +303,29 @@ class _GFProgressBarState extends State<GFProgressBar>
288303
child: (widget.child != null) ? widget.child : Container(),
289304
),
290305
)
291-
: CustomPaint(
292-
painter: CirclePainter(
293-
progressHeadType: widget.progressHeadType,
294-
progress: _percentage * 360,
295-
progressBarColor: widget.progressBarColor,
296-
backgroundColor: widget.backgroundColor,
297-
circleStartAngle: widget.circleStartAngle,
298-
radius: (widget.radius! / 2) - widget.circleWidth / 2,
299-
circleWidth: widget.circleWidth,
300-
reverse: widget.reverse,
301-
linearGradient: widget.linearGradient,
302-
mask: widget.mask),
303-
child: (widget.child != null)
304-
? Center(child: widget.child)
305-
: Container(),
306+
: GestureDetector(
307+
onPanUpdate: (details) {
308+
if (widget.isDragable == true) {
309+
final size = context.size ?? const Size(0, 0);
310+
_onDragUpdateCircular(details.localPosition, size);
311+
}
312+
},
313+
child: CustomPaint(
314+
painter: CirclePainter(
315+
progressHeadType: widget.progressHeadType,
316+
progress: _progress * 360,
317+
progressBarColor: widget.progressBarColor,
318+
backgroundColor: widget.backgroundColor,
319+
circleStartAngle: widget.circleStartAngle,
320+
radius: (widget.radius! / 2) - widget.circleWidth / 2,
321+
circleWidth: widget.circleWidth,
322+
reverse: widget.reverse,
323+
linearGradient: widget.linearGradient,
324+
mask: widget.mask),
325+
child: (widget.child != null)
326+
? Center(child: widget.child)
327+
: Container(),
328+
),
306329
));
307330

308331
if (hasSetWidth) {
@@ -355,15 +378,15 @@ class LinearPainter extends CustomPainter {
355378
this.mask,
356379
this.clipLinearGradient,
357380
}) {
358-
_paintBackground.color = backgroundColor!;
381+
_paintBackground.color = backgroundColor ?? Colors.transparent;
359382
_paintBackground.style = PaintingStyle.stroke;
360-
_paintBackground.strokeWidth = circleWidth!;
383+
_paintBackground.strokeWidth = circleWidth ?? 0.0;
361384

362385
_paintLine.color = progress.toString() == '0.0' && progressBarColor != null
363-
? progressBarColor!.withOpacity(0)
364-
: progressBarColor!;
386+
? progressBarColor ?? Colors.transparent.withOpacity(0)
387+
: progressBarColor ?? Colors.transparent;
365388
_paintLine.style = PaintingStyle.stroke;
366-
_paintLine.strokeWidth = circleWidth!;
389+
_paintLine.strokeWidth = circleWidth ?? 0.0;
367390

368391
if (progressHeadType == GFProgressHeadType.square) {
369392
_paintLine.strokeCap = StrokeCap.butt;
@@ -394,13 +417,13 @@ class LinearPainter extends CustomPainter {
394417
_paintLine.maskFilter = mask;
395418
}
396419
if (fromRightToLeft!) {
397-
final xProgress = size.width - size.width * progress!;
420+
final xProgress = size.width - size.width * (progress ?? 0.0);
398421
if (linearGradient != null) {
399422
_paintLine.shader = _createGradientShaderRightToLeft(size, xProgress);
400423
}
401424
canvas.drawLine(end, Offset(xProgress, size.height / 2), _paintLine);
402425
} else {
403-
final xProgress = size.width * progress!;
426+
final xProgress = size.width * (progress ?? 0.0);
404427
if (linearGradient != null) {
405428
_paintLine.shader = _createGradientShaderLeftToRight(size, xProgress);
406429
}
@@ -409,8 +432,9 @@ class LinearPainter extends CustomPainter {
409432
}
410433

411434
Shader? _createGradientShaderRightToLeft(Size size, double xProgress) {
412-
final Offset shaderEndPoint =
413-
clipLinearGradient! ? Offset.zero : Offset(xProgress, size.height);
435+
final Offset shaderEndPoint = clipLinearGradient ?? false
436+
? Offset.zero
437+
: Offset(xProgress, size.height);
414438
return linearGradient?.createShader(
415439
Rect.fromPoints(
416440
Offset(size.width, size.height),
@@ -420,7 +444,7 @@ class LinearPainter extends CustomPainter {
420444
}
421445

422446
Shader? _createGradientShaderLeftToRight(Size size, double xProgress) {
423-
final Offset shaderEndPoint = clipLinearGradient!
447+
final Offset shaderEndPoint = clipLinearGradient ?? false
424448
? Offset(size.width, size.height)
425449
: Offset(xProgress, size.height);
426450
return linearGradient?.createShader(
@@ -448,12 +472,12 @@ class CirclePainter extends CustomPainter {
448472
this.reverse,
449473
this.arcBackgroundColor,
450474
this.mask}) {
451-
_paintBackground.color = backgroundColor!;
475+
_paintBackground.color = backgroundColor ?? Colors.transparent;
452476
_paintBackground.style = PaintingStyle.stroke;
453-
_paintBackground.strokeWidth = circleWidth!;
454-
_paintLine.color = progressBarColor!;
477+
_paintBackground.strokeWidth = circleWidth ?? 0.0;
478+
_paintLine.color = progressBarColor ?? Colors.transparent;
455479
_paintLine.style = PaintingStyle.stroke;
456-
_paintLine.strokeWidth = circleWidth!;
480+
_paintLine.strokeWidth = circleWidth ?? 0.0;
457481
if (progressHeadType == GFProgressHeadType.circular) {
458482
_paintLine.strokeCap = StrokeCap.round;
459483
} else if (progressHeadType == GFProgressHeadType.square) {
@@ -507,7 +531,7 @@ class CirclePainter extends CustomPainter {
507531
);
508532
} else {
509533
final start = radians(-90.0 + fixedStartAngle);
510-
final end = radians(progress! * circleStartAngleFixedMargin);
534+
final end = radians((progress ?? 0.0) * circleStartAngleFixedMargin);
511535
canvas.drawArc(
512536
Rect.fromCircle(
513537
center: child,

0 commit comments

Comments
 (0)