AsyncButton class abstract

A builder that wraps a button providing disabled, loading, success and error states while retaining almost full access to the original Button's API. This is useful for any long running operations and helps better improve UX.

{@tool dartpad --template=stateful_widget_material}


@override
Widget build(BuildContext context) {
  return AsyncButtonBuilder(
    child: Text('Click Me'),
    loadingWidget: Text('Loading...'),
    onPressed: () async {
      await Future.delayed(Duration(seconds: 1));

      throw 'shucks';
    },
    builder: (context, child, callback, buttonState) {
      final buttonColor = buttonState.when(
        idle: () => Colors.yellow[200],
        loading: () => Colors.grey,
        success: () => Colors.orangeAccent,
        error: () => Colors.orange,
      );

      return OutlinedButton(
        child: child,
        onPressed: callback,
        style: OutlinedButton.styleFrom(
          primary: Colors.black,
          backgroundColor: buttonColor,
        ),
      );
    },
  ),
}

{@end-tool}

Constructors

AsyncButton()

Properties

animateSize bool
Whether to animate the Size of the widget implicitly.
no setter
builder AsyncButtonBuilderCallback
This builder provides the widget's BuildContext, the variable child based on button state as well as the callback that should be passed to the button and the current ButtonState
no setter
buttonState ButtonState
This is used to manually drive the state of the loading button thus initiating the corresponding animation and showing the correct button child.
no setter
child Widget
The child of the button. In the case of an IconButton, this can be a an Icon. For a TextButton, a Text.
no setter
disabled bool
This is used to manually drive the disabled state of the button.
no setter
duration Duration
The animation's duration between child, loadingWidget, successWidget and errorWidget. This same value is used for both the internal AnimatedSize and TransitionBuilder.
no setter
errorDuration Duration
The amount of idle time the errorWidget shows
no setter
errorPadding EdgeInsets?
Optional EdgeInsets that will wrap around the errorWidget. This is a convenience field that can be replaced by defining your own errorWidget and wrapping it in a Padding.
no setter
errorSwitchInCurve Curve
Defines a curve for the custom transition. This used in in an AnimatedSwitcher and only takes effect when animating to error
no setter
errorSwitchOutCurve Curve
Defines a curve for the custom transition. This used in in an AnimatedSwitcher and only takes effect when animating out of error
no setter
errorTransitionBuilder AnimatedSwitcherTransitionBuilder
Defines a custom transition when animating between any state and error
no setter
errorWidget Widget?
The widget used to replace the child when the button is in a error state. If this is null the default widget is:
no setter
hashCode int
The hash code for this object.
no setterinherited
idleSwitchInCurve Curve
Defines a curve for the custom transition. This used in in an AnimatedSwitcher and only takes effect when animating to idle
no setter
idleSwitchOutCurve Curve
Defines a curve for the custom transition. This used in in an AnimatedSwitcher and only takes effect when animating out of idle
no setter
idleTransitionBuilder AnimatedSwitcherTransitionBuilder
Defines a custom transition when animating between any state and idle
no setter
loadingSwitchInCurve Curve
Defines a curve for the custom transition. This used in in an AnimatedSwitcher and only takes effect when animating to loading
no setter
loadingSwitchOutCurve Curve
Defines a curve for the custom transition. This used in in an AnimatedSwitcher and only takes effect when animating out of loading
no setter
loadingTransitionBuilder AnimatedSwitcherTransitionBuilder
Defines a custom transition when animating between any state and loading
no setter
loadingWidget Widget?
The widget replaces the child when the button is in the loading state. If this is null the default widget is:
no setter
onPressed AsyncCallback?
A callback that runs the async task. This is wrapped in order to begin the button's internal isLoading before and after the operation completes.
no setter
reverseDuration Duration
The animation's reverse duration between child, loadingWidget, successWidget and errorWidget. This same value is used for both the internal AnimatedSize and TransitionBuilder.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
showError bool
Whether to show the errorWidget on error.
no setter
showSuccess bool
Whether to show the successWidget on success.
no setter
sizeAlignment Alignment
Defines the Alignment for the internal AnimatedSize
no setter
sizeClipBehavior Clip
Defines the Clip for the internal AnimatedSize
no setter
sizeCurve Curve
Defines a curve for the internal AnimatedSize
no setter
successDuration Duration
The amount of idle time the successWidget shows
no setter
successPadding EdgeInsets?
Optional EdgeInsets that will wrap around the successWidget. This is a convenience field that can be replaced by defining your own successWidget and wrapping it in a Padding.
no setter
successSwitchInCurve Curve
Defines a curve for the custom transition. This used in in an AnimatedSwitcher and only takes effect when animating to success
no setter
successSwitchOutCurve Curve
Defines a curve for the custom transition. This used in in an AnimatedSwitcher and only takes effect when animating out of success
no setter
successTransitionBuilder AnimatedSwitcherTransitionBuilder
Defines a custom transition when animating between any state and success
no setter
successWidget Widget?
The widget used to replace the child when the button is in a success state. If this is null the default widget is:
no setter

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited