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
8 changes: 8 additions & 0 deletions lib/src/step.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,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 @@ -135,6 +136,8 @@ class OnboardingStep {
/// is required
final FocusNode focusNode;

final void Function(VoidCallback nextStep)? onShowStep;

/// By default, the value used is `TextAlign.start`
final TextAlign textAlign;

Expand Down Expand Up @@ -292,6 +295,7 @@ class OnboardingStep {
Color? pulseInnerColor,
Color? pulseOuterColor,
TapCallback? onTapCallback,
Function(VoidCallback nextStep)? onShowStep,
Copy link
Owner

@talamaska talamaska Nov 19, 2024

Choose a reason for hiding this comment

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

I would prefer if you create a new typedef for this callback, named ShowStepCallback, I would prefer consistent naming of its argument, e.g. in TapCallback it is called "next", I would like this here to be also "next" and not "nextStep"

}) {
return OnboardingStep(
key: key ?? this.key,
Expand Down Expand Up @@ -320,6 +324,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 +357,7 @@ class OnboardingStep {
pulseInnerColor: $pulseInnerColor,
pulseOuterColor: $pulseOuterColor,
onTapCallback: $onTapCallback,
onShowStep: $onShowStep,
)''';
}

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

Expand Down Expand Up @@ -415,6 +422,7 @@ class OnboardingStep {
showPulseAnimation.hashCode ^
pulseInnerColor.hashCode ^
pulseOuterColor.hashCode ^
onShowStep.hashCode ^
Copy link
Owner

Choose a reason for hiding this comment

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

fix the indentation please.

onTapCallback.hashCode;
}
}
19 changes: 19 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,6 +272,11 @@ 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();
Expand Down