Skip to content
This repository was archived by the owner on Jun 16, 2024. It is now read-only.

Added null safety and outlined button support #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
65 changes: 44 additions & 21 deletions lib/button_stagger_animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,23 @@ class ButtonStaggerAnimation extends StatelessWidget {
final double progressIndicatorSize;
final BorderRadius borderRadius;
final double strokeWidth;
final Function(AnimationController) onPressed;
final Function(AnimationController)? onPressed;
final Widget child;
final bool outlined;
final double height;

ButtonStaggerAnimation({
Key key,
this.controller,
this.color,
this.progressIndicatorColor,
this.progressIndicatorSize,
this.borderRadius,
this.onPressed,
this.strokeWidth,
this.child,
Key? key,
required this.controller,
required this.color,
required this.progressIndicatorColor,
required this.progressIndicatorSize,
required this.borderRadius,
required this.onPressed,
required this.strokeWidth,
required this.child,
required this.outlined,
required this.height,
}) : super(key: key);

@override
Expand Down Expand Up @@ -50,7 +54,7 @@ class ButtonStaggerAnimation extends StatelessWidget {
BuildContext context, BoxConstraints constraints) {
var buttonHeight = (constraints.maxHeight != double.infinity)
? constraints.maxHeight
: 60.0;
: height;

var widthAnimation = Tween<double>(
begin: constraints.maxWidth,
Expand All @@ -76,16 +80,35 @@ class ButtonStaggerAnimation extends StatelessWidget {
return SizedBox(
height: buttonHeight,
width: widthAnimation.value,
child: RaisedButton(
shape: RoundedRectangleBorder(
borderRadius: borderRadiusAnimation.value,
),
color: color,
child: _buttonChild(),
onPressed: () {
this.onPressed(controller);
},
),
child: outlined
? OutlinedButton(
style: OutlinedButton.styleFrom(
primary: color,
shape: RoundedRectangleBorder(
borderRadius: borderRadiusAnimation.value,
),
),
onPressed: onPressed != null
? () {
onPressed!(controller);
}
: null,
child: _buttonChild(),
)
: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: color,
shape: RoundedRectangleBorder(
borderRadius: borderRadiusAnimation.value,
),
),
onPressed: onPressed != null
? () {
onPressed!(controller);
}
: null,
child: _buttonChild(),
),
);
},
);
Expand Down
29 changes: 23 additions & 6 deletions lib/progress_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,49 @@ import 'package:progress_indicator_button/button_stagger_animation.dart';
class ProgressButton extends StatefulWidget {
/// The background color of the button.
final Color color;

/// The progress indicator color.
final Color progressIndicatorColor;

/// The size of the progress indicator.
final double progressIndicatorSize;

/// The border radius while NOT animating.
final BorderRadius borderRadius;

/// The duration of the animation.
final Duration animationDuration;

/// The stroke width of progress indicator.
final double strokeWidth;

/// Changes to an outlined button.
final bool outlined;

/// The height of the button unless it is constrained.
final double height;

/// Function that will be called at the on pressed event.
///
/// This will grant access to its [AnimationController] so
/// that the animation can be controlled based on the need.
final Function(AnimationController) onPressed;
/// If it is null, the button will be disabled.
final Function(AnimationController)? onPressed;

/// The child to display on the button.
final Widget child;

ProgressButton({
@required this.child,
@required this.onPressed,
required this.child,
required this.onPressed,
this.color = Colors.blue,
this.strokeWidth = 2,
this.progressIndicatorColor = Colors.white,
this.progressIndicatorSize = 30,
this.borderRadius = const BorderRadius.all(Radius.circular(0)),
this.animationDuration = const Duration(milliseconds: 300),
this.outlined = false,
this.height = 60.0,
});

@override
Expand All @@ -42,7 +57,7 @@ class ProgressButton extends StatefulWidget {

class _ProgressButtonState extends State<ProgressButton>
with TickerProviderStateMixin {
AnimationController _controller;
AnimationController? _controller;

@override
void initState() {
Expand All @@ -56,21 +71,23 @@ class _ProgressButtonState extends State<ProgressButton>

@override
void dispose() {
_controller.dispose();
_controller?.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Center(
child: ButtonStaggerAnimation(
controller: _controller.view,
controller: _controller!,
color: widget.color,
strokeWidth: widget.strokeWidth,
progressIndicatorColor: widget.progressIndicatorColor,
progressIndicatorSize: widget.progressIndicatorSize,
borderRadius: widget.borderRadius,
onPressed: widget.onPressed,
outlined: widget.outlined,
height: widget.height,
child: widget.child,
),
);
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: progress_indicator_button
description: A simple button which can transform and show a progress indicator.
version: 0.0.3
version: 0.0.4
author: Pascal Brosinski<[email protected]>
homepage: https://github.com/PascalAC/progress_button

environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
sdk: ">=2.12.0 <3.0.0"

dependencies:
flutter:
Expand Down
8 changes: 5 additions & 3 deletions test/progress_button_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import 'package:progress_indicator_button/progress_button.dart';
import '../example/lib/src/app.dart';

void main() {
testWidgets('ProgressButton - Start animation on click', (WidgetTester tester) async {
testWidgets('ProgressButton - Start animation on click',
(WidgetTester tester) async {
await tester.pumpWidget(App());

expect(find.byType(ProgressButton), findsOneWidget);

await tester.tap(find.byType(RaisedButton));
await tester.tap(find.byType(ElevatedButton));

final widget = tester.widget<ButtonStaggerAnimation>(find.byType(ButtonStaggerAnimation));
final widget = tester
.widget<ButtonStaggerAnimation>(find.byType(ButtonStaggerAnimation));
expect(widget.controller.isAnimating, true);
});
}