Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to show text progress inside the loader #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions lib/rounded_loading_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -87,15 +90,16 @@ class RoundedLoadingButton extends StatefulWidget {
}

/// initalize constructor
const RoundedLoadingButton({
RoundedLoadingButton({
Key? key,
required this.controller,
required this.onPressed,
required this.child,
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,
Expand Down Expand Up @@ -172,9 +176,22 @@ class RoundedLoadingButtonState extends State<RoundedLoadingButton>
Widget _loader = SizedBox(
height: widget.loaderSize,
width: widget.loaderSize,
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(widget.valueColor),
strokeWidth: widget.loaderStrokeWidth,
child: Stack(
alignment: Alignment.center,
children: [
SizedBox(
height: widget.loaderSize,
width: widget.loaderSize,
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(widget.valueColor),
strokeWidth: widget.loaderStrokeWidth,
),
),
Text(
widget.loaderText ?? "",
style: TextStyle(color: widget.valueColor, fontSize: 10),
)
],
),
);

Expand Down Expand Up @@ -275,7 +292,8 @@ class RoundedLoadingButtonState extends State<RoundedLoadingButton>
widget.controller._state.sink.add(event);
});

widget.controller._addListeners(_start, _stop, _success, _error, _reset);
widget.controller
._addListeners(_start, _stop, _success, _error, _reset, _progress);
}

@override
Expand Down Expand Up @@ -305,6 +323,14 @@ class RoundedLoadingButtonState extends State<RoundedLoadingButton>
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);
Expand Down Expand Up @@ -342,19 +368,22 @@ class RoundedLoadingButtonController {
VoidCallback? _successListener;
VoidCallback? _errorListener;
VoidCallback? _resetListener;
UpdateProgressDef? _updateProgress;

void _addListeners(
VoidCallback startListener,
VoidCallback stopListener,
VoidCallback successListener,
VoidCallback errorListener,
VoidCallback resetListener,
UpdateProgressDef? updateProgress,
) {
_startListener = startListener;
_stopListener = stopListener;
_successListener = successListener;
_errorListener = errorListener;
_resetListener = resetListener;
_updateProgress = updateProgress;
}

final BehaviorSubject<ButtonState> _state =
Expand All @@ -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!();
Expand All @@ -391,3 +424,5 @@ class RoundedLoadingButtonController {
if (_resetListener != null) _resetListener!();
}
}

typedef UpdateProgressDef(int value);