Skip to content

Commit 5afab59

Browse files
feat: ✨ Add dynamic onStart callback registration with addOnStartCallback and removeOnStartCallback methods (#624)
1 parent 65c1929 commit 5afab59

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- Feature [#600](https://github.com/SimformSolutionsPvtLtd/showcaseview/issues/600) - Added dynamic onComplete callback registration with `addOnCompleteCallback` and `removeOnCompleteCallback` methods
77
- Fixed [#577](https://github.com/SimformSolutionsPvtLtd/showcaseview/issues/577) - Resolve issue where long tooltip text was rendered outside the screen bounds
88
- Feature [#586](https://github.com/SimformSolutionsPvtLtd/showcaseview/issues/586): Enhance tooltip accessibility using Semantics live region
9+
- Feature [#624](https://github.com/SimformSolutionsPvtLtd/showcaseview/pull/624): Added dynamic onStart callback registration with `addOnStartCallback` and `removeOnStartCallback` methods
910

1011
## [5.0.1]
1112

doc/documentation.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,29 @@ ShowcaseView.register(
575575

576576
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.
577577

578+
#### OnStart Callback
579+
580+
Add or remove callbacks that trigger when each showcase step starts:
581+
582+
```dart
583+
// Add a callback for when each showcase step completes
584+
ShowcaseView.get().addOnstartCallback((index, key) {
585+
print('Showcase step $index started with key: $key');
586+
// Perform custom actions here
587+
});
588+
589+
// Remove a previously added callback
590+
ShowcaseView.get().removeOnstartCallback(callbackFunction);
591+
```
592+
593+
**Example with named scope:**
594+
595+
```dart
596+
ShowcaseView.getNamed('profile').addOnstartCallback((index, key) {
597+
print('Profile showcase started at step $index');
598+
});
599+
```
600+
578601
#### OnComplete Callback
579602

580603
Add or remove callbacks that trigger when each showcase step completes:

lib/src/showcase/showcase_view.dart

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ class ShowcaseView {
192192
/// Stores functions to call when a onComplete event occurs.
193193
final List<OnShowcaseCallback> _onCompleteCallbacks = [];
194194

195+
/// Stores functions to call when a onStart event occurs.
196+
final List<OnShowcaseCallback> _onStartCallbacks = [];
197+
195198
/// Returns whether showcase is completed or not.
196199
bool get isShowCaseCompleted => _ids == null && _activeWidgetId == null;
197200

@@ -301,6 +304,7 @@ class ShowcaseView {
301304
_onFinishCallbacks.clear();
302305
_onDismissCallbacks.clear();
303306
_onCompleteCallbacks.clear();
307+
_onStartCallbacks.clear();
304308
}
305309

306310
/// Updates the overlay to reflect current showcase state.
@@ -359,6 +363,16 @@ class ShowcaseView {
359363
_onCompleteCallbacks.remove(listener);
360364
}
361365

366+
/// Adds a listener that will be called when the showcase tour is started.
367+
void addOnStartCallback(OnShowcaseCallback listener) {
368+
_onStartCallbacks.add(listener);
369+
}
370+
371+
/// Removes a listener that was previously added via [addOnStartCallback].
372+
void removeOnStartCallback(OnShowcaseCallback listener) {
373+
_onStartCallbacks.remove(listener);
374+
}
375+
362376
void _startShowcase(
363377
Duration delay,
364378
List<GlobalKey<State<StatefulWidget>>> widgetIds,
@@ -467,6 +481,11 @@ class ShowcaseView {
467481
_activeWidgetId ??= 0;
468482
if (_activeWidgetId! < _ids!.length) {
469483
onStart?.call(_activeWidgetId, _ids![_activeWidgetId!]);
484+
// Call all registered onStart callbacks
485+
for (final callback in _onStartCallbacks) {
486+
callback.call(_activeWidgetId, _ids![_activeWidgetId!]);
487+
}
488+
470489
final controllers = _getCurrentActiveControllers;
471490
final controllerLength = controllers.length;
472491
final firstController = controllers.firstOrNull;
@@ -486,7 +505,6 @@ class ShowcaseView {
486505
}
487506

488507
// Cancel any existing timer before setting up a new one
489-
490508
if (autoPlay) {
491509
_cancelTimer();
492510
final config = _getCurrentActiveControllers.firstOrNull?.config;

0 commit comments

Comments
 (0)