woltapp / wolt_modal_sheet

This package provides a responsive modal with multiple pages, motion animation for page transitions, and scrollable content within each page.
MIT License
392 stars 50 forks source link

onCurrentPageChanged with ID and index #251

Open blingkonnerth opened 4 days ago

blingkonnerth commented 4 days ago

I would love have a onPageChanged - Callback.

I would expect it to look something like this:

onCurrentPageChanged: (newPageId, index) {
// ... do some stuff here
}

This came up when I wanted to build in tracking for the bottomsheet. I had this method before:

  static void _handleTracking({
    required BuildContext context,
    required List<UIBottomSheetPageModel> pages,
    required int currentIndex,
    required ValueNotifier<int> pageIndexNotifier,
  }) {
    // Since the views inside the bottom sheet are not actual routes,
    // we need to track the navigation manually.

    final firstPage = pages[0];

    // Track initial page
    context.appDelegate.trackNavigation(
      navigationType: UINavigationType.push,
      routeName: firstPage.trackingName,
      trackWithSegment: true,
    );

    // Track navigation inside the modal sheet
    pageIndexNotifier.addListener(() {
      final newIndex = pageIndexNotifier.value;
      final newPage = pages[newIndex];

      context.appDelegate.trackNavigation(
        navigationType: newIndex > currentIndex
            ? UINavigationType.push
            : UINavigationType.pop,
        routeName: newPage.trackingName,
        trackWithSegment: true,
      );
      currentIndex = newIndex;
    });
  }

But this will obviously not work when dealing with dynamic routes or even when pushing or popping. It will only work if all pages are constant.

I guess I could get hacky here and still manage to make it work, but everything would be solved, by a simply onCurrentPageChanged - callback (getting the id of the new page and index would be crucial here).

ulusoyca commented 3 days ago

Brilliant idea. Would you consider opening a PR?