From a5c778a62901af3e868db140f68f49bb093a52bd Mon Sep 17 00:00:00 2001 From: WadihPersonal Date: Thu, 15 Dec 2022 13:48:38 +0200 Subject: [PATCH] Update rounded_loading_button.dart Add the ability to show the progress inside the idicator --- lib/rounded_loading_button.dart | 47 ++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/lib/rounded_loading_button.dart b/lib/rounded_loading_button.dart index 54c39c0..b428e87 100644 --- a/lib/rounded_loading_button.dart +++ b/lib/rounded_loading_button.dart @@ -33,6 +33,9 @@ class RoundedLoadingButton extends StatefulWidget { /// The size of the CircularProgressIndicator final double loaderSize; + /// The loader text is to update the text inside the loader + String? loaderText; + /// The stroke width of the CircularProgressIndicator final double loaderStrokeWidth; @@ -87,7 +90,7 @@ class RoundedLoadingButton extends StatefulWidget { } /// initalize constructor - const RoundedLoadingButton({ + RoundedLoadingButton({ Key? key, required this.controller, required this.onPressed, @@ -95,7 +98,8 @@ class RoundedLoadingButton extends StatefulWidget { this.color = Colors.lightBlue, this.height = 50, this.width = 300, - this.loaderSize = 24.0, + this.loaderText, + this.loaderSize = 35.0, this.loaderStrokeWidth = 2.0, this.animateOnTap = true, this.valueColor = Colors.white, @@ -172,9 +176,22 @@ class RoundedLoadingButtonState extends State Widget _loader = SizedBox( height: widget.loaderSize, width: widget.loaderSize, - child: CircularProgressIndicator( - valueColor: AlwaysStoppedAnimation(widget.valueColor), - strokeWidth: widget.loaderStrokeWidth, + child: Stack( + alignment: Alignment.center, + children: [ + SizedBox( + height: widget.loaderSize, + width: widget.loaderSize, + child: CircularProgressIndicator( + valueColor: AlwaysStoppedAnimation(widget.valueColor), + strokeWidth: widget.loaderStrokeWidth, + ), + ), + Text( + widget.loaderText ?? "", + style: TextStyle(color: widget.valueColor, fontSize: 10), + ) + ], ), ); @@ -275,7 +292,8 @@ class RoundedLoadingButtonState extends State widget.controller._state.sink.add(event); }); - widget.controller._addListeners(_start, _stop, _success, _error, _reset); + widget.controller + ._addListeners(_start, _stop, _success, _error, _reset, _progress); } @override @@ -305,6 +323,14 @@ class RoundedLoadingButtonState extends State if (widget.resetAfterDuration) _reset(); } + void _progress(int progress) { + if (!mounted) return; + widget.loaderText = "$progress%"; + setState(() {}); + _state.sink.add(ButtonState.loading); + if (widget.resetAfterDuration) _reset(); + } + void _stop() { if (!mounted) return; _state.sink.add(ButtonState.idle); @@ -342,6 +368,7 @@ class RoundedLoadingButtonController { VoidCallback? _successListener; VoidCallback? _errorListener; VoidCallback? _resetListener; + UpdateProgressDef? _updateProgress; void _addListeners( VoidCallback startListener, @@ -349,12 +376,14 @@ class RoundedLoadingButtonController { VoidCallback successListener, VoidCallback errorListener, VoidCallback resetListener, + UpdateProgressDef? updateProgress, ) { _startListener = startListener; _stopListener = stopListener; _successListener = successListener; _errorListener = errorListener; _resetListener = resetListener; + _updateProgress = updateProgress; } final BehaviorSubject _state = @@ -371,6 +400,10 @@ class RoundedLoadingButtonController { if (_startListener != null) _startListener!(); } + void update(int progress) { + if (_updateProgress != null) _updateProgress!(progress); + } + /// Notify listeners to start the stop animation void stop() { if (_stopListener != null) _stopListener!(); @@ -391,3 +424,5 @@ class RoundedLoadingButtonController { if (_resetListener != null) _resetListener!(); } } + +typedef UpdateProgressDef(int value);