startTutorialStep method

void startTutorialStep(
  1. int step
)

Starts the tutorial from a specific step index.

Automatically navigates to the tab associated with each tutorial target.

Implementation

void startTutorialStep(int step) async {
  if (_isShowingTutorial) return;
  _isShowingTutorial = true;

  if (step >= targets.length || step < 0) return;

  tutorialCoachMark?.finish();

  final user = await _profileController.getUserData();

  setState(() {
    tutorialStep = step;

    final currentTarget = targets[step];
    final key = currentTarget.keyTarget;

    if (key == _progressTabKey) {
      selectedIndex = 0;
    } else if (key == _libraryTabKey) {
      selectedIndex = 1;
    } else if (key == _statisticsTabKey) {
      selectedIndex = 2;
    } else if (key == _profileTabKey) {
      selectedIndex = 3;
    }
  });

  Future.delayed(const Duration(milliseconds: 300), () {
    if (mounted) {
      tutorialCoachMark = TutorialCoachMark(
        initialFocus: step,
        targets: targets,
        pulseEnable: false,
        alignSkip: Alignment.topRight,
        colorShadow: Colors.black,
        opacityShadow: 0.8,
        skipWidget: Padding(
          padding: const EdgeInsets.only(top: 40.0, right: 16.0),
          child: Align(
            alignment: Alignment.topRight,
            child: GestureDetector(
              onTap: () {
                tutorialCoachMark?.skip();
              },
              child: Text(
                AppLocalizations.of(context)!.tSkip,
                style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                ),
              ),
            ),
          ),
        ),
        onFinish: () {
          SharedPreferences.getInstance().then((prefs) {
            prefs.setBool('seenTutorial_${user.email}', true);
          });
          _isShowingTutorial = false;
        },
        onClickTarget: (target) async {
          setState(() {
            switch (counter) {
              case 0:
                selectedIndex = 0;
              case 1:
                selectedIndex = 1;
              case 2:
                selectedIndex = 2;
              case 3:
                selectedIndex = 3;
              case 4:
                selectedIndex = 3;
              default:
                selectedIndex = 0;
            }
            counter++;
          });

          if (target.identify == "full_screen_step_profile_2") {
            SharedPreferences.getInstance().then((prefs) {
              prefs.setBool('seenTutorial_${user.email}', true);
            });
          }
        },
        onSkip: () {
          SharedPreferences.getInstance().then((prefs) {
            prefs.setBool('seenTutorial_${user.email}', true);
          });
          _isShowingTutorial = false;
          return true;
        },
        onClickOverlay: (_) {},
      );
    }

    if (mounted) {
      tutorialCoachMark!.show(context: context);
    }
  });
}