Skip to content

Commit 89a7f15

Browse files
feat: ✨ Added dynamic onComplete callback registration with addOnCompleteCallback and removeOnCompleteCallback methods (#601)
1 parent 71c271d commit 89a7f15

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Fixed [#590](https://github.com/SimformSolutionsPvtLtd/showcaseview/issues/590) - Fixed Rendering issues with scrollable showcase views inside withWidget
44
- Fixed [#587](https://github.com/SimformSolutionsPvtLtd/showcaseview/issues/587) - Fixed ShowcaseView.withWidget container positioning issues on web
55
- Fixed [#588](https://github.com/SimformSolutionsPvtLtd/showcaseview/issues/588) & [#593](https://github.com/SimformSolutionsPvtLtd/showcaseview/issues/593) - Fixed incorrect ShowcaseView callback scope by adding optional scope parameter
6+
- Feature [#600](https://github.com/SimformSolutionsPvtLtd/showcaseview/issues/600) - Added dynamic onComplete callback registration with `addOnCompleteCallback` and `removeOnCompleteCallback` methods
67

78
## [5.0.1]
89

doc/documentation.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,79 @@ ShowcaseView.register(
570570
)
571571
```
572572

573+
### Dynamic Callbacks
574+
575+
You can also add and remove event callbacks dynamically at runtime. This is particularly useful when you need to register callbacks from widgets deeper in the tree or when using named scopes.
576+
577+
#### OnComplete Callback
578+
579+
Add or remove callbacks that trigger when each showcase step completes:
580+
581+
```dart
582+
// Add a callback for when each showcase step completes
583+
ShowcaseView.get().addOnCompleteCallback((index, key) {
584+
print('Showcase step $index completed with key: $key');
585+
// Perform custom actions here
586+
});
587+
588+
// Remove a previously added callback
589+
ShowcaseView.get().removeOnCompleteCallback(callbackFunction);
590+
```
591+
592+
**Example with named scope:**
593+
594+
```dart
595+
ShowcaseView.getNamed('profile').addOnCompleteCallback((index, key) {
596+
print('Profile showcase completed at step $index');
597+
});
598+
```
599+
600+
#### OnFinish Callback
601+
602+
Add or remove callbacks that trigger when the entire showcase tour is finished:
603+
604+
```dart
605+
// Add a callback for when the showcase tour is finished
606+
ShowcaseView.get().addOnFinishCallback(() {
607+
print('Showcase tour finished');
608+
// Perform custom actions here
609+
});
610+
611+
// Remove a previously added callback
612+
ShowcaseView.get().removeOnFinishCallback(callbackFunction);
613+
```
614+
615+
**Example with named scope:**
616+
617+
```dart
618+
ShowcaseView.getNamed('profile').addOnFinishCallback(() {
619+
print('Profile showcase tour finished');
620+
});
621+
```
622+
623+
#### OnDismiss Callback
624+
625+
Add or remove callbacks that trigger when the showcase is dismissed:
626+
627+
```dart
628+
// Add a callback for when the showcase tour is dismissed
629+
ShowcaseView.get().addOnDismissCallback((reason) {
630+
print('Showcase dismissed because: $reason');
631+
// Perform custom actions here
632+
});
633+
634+
// Remove a previously added callback
635+
ShowcaseView.get().removeOnDismissCallback(callbackFunction);
636+
```
637+
638+
**Example with named scope:**
639+
640+
```dart
641+
ShowcaseView.getNamed('profile').addOnDismissCallback((reason) {
642+
print('Profile showcase dismissed because: $reason');
643+
});
644+
```
645+
573646
## Overriding Showcase View Configuration
574647

575648
You can set global configurations for all showcases in your app by using ShowcaseView.register()

lib/src/showcase/showcase_view.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ class ShowcaseView {
179179
/// Stores functions to call when a onDismiss event occurs.
180180
final List<OnDismissCallback> _onDismissCallbacks = [];
181181

182+
/// Stores functions to call when a onComplete event occurs.
183+
final List<OnShowcaseCallback> _onCompleteCallbacks = [];
184+
182185
/// Returns whether showcase is completed or not.
183186
bool get isShowCaseCompleted => _ids == null && _activeWidgetId == null;
184187

@@ -287,6 +290,7 @@ class ShowcaseView {
287290

288291
_onFinishCallbacks.clear();
289292
_onDismissCallbacks.clear();
293+
_onCompleteCallbacks.clear();
290294
}
291295

292296
/// Updates the overlay to reflect current showcase state.
@@ -335,6 +339,16 @@ class ShowcaseView {
335339
_onDismissCallbacks.remove(listener);
336340
}
337341

342+
/// Adds a listener that will be called when each showcase step completes.
343+
void addOnCompleteCallback(OnShowcaseCallback listener) {
344+
_onCompleteCallbacks.add(listener);
345+
}
346+
347+
/// Removes a listener that was previously added via [addOnCompleteCallback].
348+
void removeOnCompleteCallback(OnShowcaseCallback listener) {
349+
_onCompleteCallbacks.remove(listener);
350+
}
351+
338352
void _startShowcase(
339353
Duration delay,
340354
List<GlobalKey<State<StatefulWidget>>> widgetIds,
@@ -491,6 +505,10 @@ class ShowcaseView {
491505
final activeId = _activeWidgetId ?? -1;
492506
if (activeId < (_ids?.length ?? activeId)) {
493507
onComplete?.call(activeId, _ids![activeId]);
508+
// Call all registered onComplete callbacks
509+
for (final callback in _onCompleteCallbacks) {
510+
callback.call(activeId, _ids![activeId]);
511+
}
494512
}
495513

496514
if (autoPlay) _cancelTimer();

0 commit comments

Comments
 (0)