TutorialCoachMark

repository·master·Indexed 20 days ago

https://github.com/rafaelbarbosatec/tutorial_coach_mark

A Flutter plugin for creating step-by-step tutorials by highlighting specific widgets using TargetFocus and ContentTarget. It provides programmatic control over the tutorial flow via methods like next(), previous(), and goTo(), and supports customizable overlays, pulse animations, and skip button behavior.

Tokens
2.4K
Snippets
7
Records
11
Agent score
69%

What's inside tutorial_coach_mark

  1. Customize iOS Launch Screen Assets

    master

    To change the launch screen image for the iOS version of your Flutter application, you can either replace the image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a visual approach.

    Using Xcode:

    1. Open the iOS project in Xcode by running open ios/Runner.xcworkspace from your terminal.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing launch screen assets.
    open ios/Runner.xcworkspace
  2. Initialize and show a TutorialCoachMark

    master

    To create an interactive tutorial, instantiate TutorialCoachMark with a list of TargetFocus objects and then call .show(context: context). You can customize the appearance (shadow color, opacity), animations (pulse, focus/unfocus duration), and skip button behavior via the constructor.

    Key configuration options:

    • targets: A required list of TargetFocus defining the sequence of highlights.
    • onSkip: A callback that returns true to close the tutorial or false to proceed to the next target.
    • onFinish: A callback triggered when the tutorial sequence completes.
    • colorShadow & opacityShadow: Controls the background overlay appearance.
    • pulseEnable: Enables/disables the pulse animation on the focused target.
    TutorialCoachMark(
      targets: targets, // List<TargetFocus>
      colorShadow: Colors.red,
      onSkip: () {
        return true; // returning true closes the tutorial
      },
    )..show(context: context);
  3. Use TutorialCoachMark to show a tutorial

    master

    To display a tutorial, instantiate TutorialCoachMark with a list of TargetFocus objects and call the .show(context: context) method. You can configure callbacks for when the tutorial finishes, when a target is clicked, or when the user skips the tutorial.

    import 'package:flutter/material.dart';
    import 'package:tutorial_coach_mark/tutorial_coach_mark.dart';
    
    void showTutorial() {
        TutorialCoachMark tutorial = TutorialCoachMark(
          targets: targets, // List<TargetFocus>
          colorShadow: Colors.red,
          onFinish: () {
            print("finish");
          },
          onClickTargetWithTapPosition: (target, tapDetails) {
            print("target: $target");
            print("clicked at position local: ${tapDetails.localPosition} - global: ${tapDetails.globalPosition}");
          },
          onClickTarget: (target) {
            print(target);
          },
          onSkip: () {
            print("skip");
            return true;
          }
        )..show(context: context);
    }
  4. Control the tutorial programmatically

    master

    The TutorialCoachMark instance provides methods to control the tutorial flow manually:

    • tutorial.skip(): Skips the tutorial.
    • tutorial.finish(): Finishes the tutorial.
    • tutorial.next(): Moves to the next target.
    • tutorial.previous(): Moves to the previous target.
    • tutorial.goTo(index): Jumps to a specific target by its index.
  5. Configure Skip Button appearance and behavior

    master

    You can customize the skip button via the TutorialCoachMark constructor:

    • textSkip: The label for the button (default: "SKIP").
    • textStyleSkip: The TextStyle for the button text.
    • alignSkip: The AlignmentGeometry on screen (default: Alignment.bottomRight).
    • hideSkip: If true, the skip button is hidden.
    • showSkipInLastTarget: Whether to show the skip button on the final target (default: true).
    • skipWidget: A custom Widget to replace the default skip button.
  6. Show tutorial using NavigatorStateKey or OverlayState

    master

    If you do not have direct access to a BuildContext or need more granular control over where the tutorial is injected, use these alternative methods:

    • showWithNavigatorStateKey({required GlobalKey<NavigatorState> navigatorKey, bool rootOverlay = false}): Uses a specific NavigatorState global key to find the overlay.
    • showWithOverlayState({required OverlayState overlay, bool rootOverlay = false}): Directly uses a provided OverlayState to insert the tutorial.
    // Using a Navigator Key
    coachMark.showWithNavigatorStateKey(navigatorKey: myNavigatorKey);
    
    // Using an Overlay State
    coachMark.showWithOverlayState(overlay: myOverlayState);
  7. Control the tutorial sequence programmatically

    master

    Once a TutorialCoachMark is active, you can control its progression using the following methods:

    • show({required BuildContext context, bool rootOverlay = false}): Displays the tutorial using the provided context.
    • next(): Advances to the next target in the sequence.
    • previous(): Returns to the previous target.
    • goTo(int index): Jumps directly to a specific target by its zero-based index.
    • skip(): Skips the tutorial (respects the onSkip callback logic).
    • finish(): Ends the tutorial immediately and removes the overlay.
    • isShowing: A getter that returns true if the tutorial is currently visible.
    // Example of programmatic control
    final coachMark = TutorialCoachMark(targets: myTargets);
    coachMark.show(context: context);
    
    // Later in your code...
    coachMark.next();
    coachMark.finish();
  8. Configure TutorialCoachMark callbacks

    master

    The TutorialCoachMark class provides several lifecycle and interaction callbacks:

    CallbackDescription
    beforeFocusExecuted before a target is focused.
    onClickTargetExecuted when the target area is tapped.
    onClickTargetWithTapPositionExecuted when the target is tapped, providing TapDownDetails.
    onClickOverlayExecuted when the area outside the target (the overlay) is tapped.
    onFinishExecuted when the tutorial sequence is completed.
    onSkipExecuted when the skip button is pressed. Return true to close, false to go to next().
  9. Configure ContentTarget

    master

    The ContentTarget (referred to as TargetContent in code examples) class determines what is displayed and how it appears relative to the focused widget.

    Attributes:

    AttributeTypeDescription
    alignAlignContentPosition of content relative to the widget (top, bottom, left, right, or custom)
    paddingEdgeInsetsPadding of the content
    childWidgetThe widget to display
    builderWidgetAlternative way to provide content via a builder
    customPositionCustomTargetContentPositionUsed when align is AlignContent.custom
    TargetContent(
      align: ContentAlign.bottom,
      child: Container(
        child: Text("Tutorial Content"),
      ),
    )
  10. Configure TargetFocus

    master

    The TargetFocus class represents the specific widget that will be highlighted during the tutorial. Use it to define which widget to focus on and what content to display around it.

    Attributes:

    AttributeTypeDescription
    identifydynamicFree for identification use
    keyTargetGlobalKeyGlobalKey of the widget that wants to be focused
    targetPositionTargetPositionUse this instead of keyTarget to determine focus location manually
    contentsContentTarget[]List of TargetContent to display after focusing the widget
    shapeShapeLightFocusShapeLightFocus.Circle or ShapeLightFocus.RRect
    radiusdoubleUsed when shape is ShapeLightFocus.RRect
    borderSideBorderSideBorder styling for the focus area
    colorColorCustom color for the target
    enableOverlayTabboolIf true, clicking anywhere on the screen calls the next step
    enableTargetTabboolIf true, clicking on the target calls the next step
    alignSkipAlignmentAlignment of the skip button within the target
    paddingFocusAlignmentPadding settings for the focus area
    focusAnimationDurationDurationOverride global focus animation duration
    unFocusAnimationDurationDurationOverride global unfocus animation duration
    pulseVariationTweenOverride the interval pulse animation
    TargetFocus(
      identify: "Target 1",
      keyTarget: myGlobalKey,
      contents: [
        TargetContent(
          align: ContentAlign.bottom,
          child: Text("Hello World"),
        ),
      ],
    )