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

Added onStart and onShowStep callbacks #63

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions lib/src/onboarding_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Onboarding extends StatefulWidget {
this.initialIndex = 0,
this.onChanged,
this.onEnd,
this.onStart,
this.autoSizeTexts = false,
required this.steps,
required this.child,
Expand All @@ -35,6 +36,8 @@ class Onboarding extends StatefulWidget {
/// A callback that signal when the `Onboarding` is finished or stopped
final ValueChanged<int>? onEnd;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add inline docs for the new callback

final VoidCallback? onStart;

/// By default, the value used is false
/// Sometimes the `titleText` and the `bodyText` might not fit well in the constrained label box,
/// because of the long texts, longer translations or smaller screens.
Expand Down Expand Up @@ -178,6 +181,7 @@ class OnboardingState extends State<Onboarding> {
debugBoundaries: widget.debugBoundaries,
scaleWidth: widget.scaleWidth,
scaleHeight: widget.scaleHeight,
onStart: widget.onStart,
onChanged: (int index) {
controller.setCurrentIndex(index);
widget.onChanged?.call(index);
Expand Down
11 changes: 11 additions & 0 deletions lib/src/step.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ typedef StepPainterBuilder = CustomPainter Function(
typedef TapCallback = void Function(
TapArea area, VoidCallback next, VoidCallback close);

typedef ShowStepCallback = void Function(
VoidCallback next);

@immutable
class OnboardingStep {
/// At least a [titleText] or a [bodyText] should be provided.
Expand Down Expand Up @@ -96,6 +99,7 @@ class OnboardingStep {
this.showPulseAnimation = false,
this.pulseInnerColor = defaultInnerPulseColor,
this.pulseOuterColor = defaultOuterPulseColor,
this.onShowStep,
this.onTapCallback,
}) : assert(() {
if (titleTextColor == null && titleTextStyle == null) {
Expand Down Expand Up @@ -265,6 +269,8 @@ class OnboardingStep {

final TapCallback? onTapCallback;

final ShowStepCallback? onShowStep;

OnboardingStep copyWith({
Key? key,
FocusNode? focusNode,
Expand Down Expand Up @@ -292,6 +298,7 @@ class OnboardingStep {
Color? pulseInnerColor,
Color? pulseOuterColor,
TapCallback? onTapCallback,
ShowStepCallback? onShowStep,
}) {
return OnboardingStep(
key: key ?? this.key,
Expand Down Expand Up @@ -320,6 +327,7 @@ class OnboardingStep {
pulseInnerColor: pulseInnerColor ?? this.pulseInnerColor,
pulseOuterColor: pulseOuterColor ?? this.pulseOuterColor,
onTapCallback: onTapCallback ?? this.onTapCallback,
onShowStep: onShowStep ?? this.onShowStep,
);
}

Expand Down Expand Up @@ -352,6 +360,7 @@ class OnboardingStep {
pulseInnerColor: $pulseInnerColor,
pulseOuterColor: $pulseOuterColor,
onTapCallback: $onTapCallback,
onShowStep: $onShowStep,
)''';
}

Expand Down Expand Up @@ -385,6 +394,7 @@ class OnboardingStep {
other.showPulseAnimation == showPulseAnimation &&
other.pulseInnerColor == pulseInnerColor &&
other.pulseOuterColor == pulseOuterColor &&
other.onShowStep == onShowStep &&
other.onTapCallback == onTapCallback;
}

Expand Down Expand Up @@ -415,6 +425,7 @@ class OnboardingStep {
showPulseAnimation.hashCode ^
pulseInnerColor.hashCode ^
pulseOuterColor.hashCode ^
onShowStep.hashCode ^
onTapCallback.hashCode;
}
}
20 changes: 20 additions & 0 deletions lib/src/stepper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class OnboardingStepper extends StatefulWidget {
this.pulseDuration = const Duration(milliseconds: 1000),
this.onChanged,
this.onEnd,
this.onStart,
this.autoSizeTexts = false,
this.stepIndexes = const <int>[],
this.debugBoundaries = false,
Expand Down Expand Up @@ -60,6 +61,8 @@ class OnboardingStepper extends StatefulWidget {
/// By default stepIndexes os an empty array
final List<int> stepIndexes;

final VoidCallback? onStart;

/// `onChanged` is called every time when the previous step has faded out,
///
/// before the next step is shown with a value of the step index on which the user was
Expand Down Expand Up @@ -200,6 +203,14 @@ class OnboardingStepperState extends State<OnboardingStepper>
await Future<void>.delayed(step.delay);
}

if (widget.onStart != null) {
widget.onStart!();
}

if (step.onShowStep != null) {
step.onShowStep!(_nextStep);
}

setTweensAndAnimate(step);
step.focusNode.requestFocus();
}
Expand Down Expand Up @@ -234,6 +245,9 @@ class OnboardingStepperState extends State<OnboardingStepper>
if (stepperIndex > 0) {
await Future<void>.delayed(step.delay);
}
if (step.onShowStep != null) {
step.onShowStep!(_nextStep);
}
if (stepperIndex < widget.steps.length && stepperIndex >= 0) {
setTweensAndAnimate(step);
step.focusNode.requestFocus();
Expand All @@ -258,11 +272,17 @@ class OnboardingStepperState extends State<OnboardingStepper>
final OnboardingStep step = widget.steps[stepperIndex];
await Future<void>.delayed(step.delay);

if (step.onShowStep != null) {
step.onShowStep!(_nextStep);
}


if (widget.stepIndexes.indexWhere((int el) => el == stepperIndex) != -1) {
setTweensAndAnimate(step);
step.focusNode.requestFocus();
}
}
if (!mounted) return;
setState(() {
calcWidgetRect(widget.steps[stepperIndex]);
});
Expand Down