scratcher

repository·master·Indexed 20 days ago

https://github.com/vintage/scratcher

A Flutter widget for creating scratch card effects that temporarily hide content under a color or image layer. It features progress tracking via onChange, a configurable reveal threshold via onThreshold, and programmatic control through ScratcherState methods like reset() and reveal() using a GlobalKey.

Tokens
2.1K
Snippets
7
Records
10
Agent score
67%

What's inside scratcher

  1. Customize Launch Screen Assets

    master

    To customize the app's launch screen, replace the existing image files within the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory with your own assets.

    Alternatively, you can manage these assets using Xcode:

    1. Open the iOS workspace using open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, select Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog.
    open ios/Runner.xcworkspace
  2. Implement the Scratcher widget

    master

    To use the scratch card, import package:scratcher/scratcher.dart and wrap the widget you want to hide inside a Scratcher widget. You can cover the child with a solid color or a custom image.

    Key features include tracking scratch progress via onChange and triggering an action when a specific percentage of the area is revealed via onThreshold.

    import 'package:scratcher/scratcher.dart';
    
    Scratcher(
      brushSize: 30,
      threshold: 50,
      color: Colors.red,
      onChange: (value) => print("Scratch progress: $value%"),
      onThreshold: () => print("Threshold reached, you won!"),
      child: Container(
        height: 300,
        width: 300,
        color: Colors.blue,
      ),
    )
  3. Configure ScratchAccuracy for performance

    master

    The accuracy property determines how precisely the progress is tracked. Higher accuracy provides a smoother progress value but requires more computational resources.

    Use the ScratchAccuracy enum to set the level:

    • ScratchAccuracy.low: High performance, lower accuracy.
    • ScratchAccuracy.medium: Balanced performance and accuracy.
    • ScratchAccuracy.high: High accuracy, lower performance.
    Scratcher(
      accuracy: ScratchAccuracy.medium,
      child: MyWidget(),
    )
  4. Use the Scratcher widget

    master

    The Scratcher widget covers a given child with a scratchable overlay. You can use a solid color or an image to create the scratchable surface. The widget provides callbacks to track progress, detect when a threshold is reached, and monitor the lifecycle of the scratching interaction.

    Key Properties

    • child: The widget that will be revealed underneath the scratch layer.
    • threshold: A double (0.0 to 100.0) representing the percentage of the area that must be scratched to trigger onThreshold.
    • brushSize: The size of the scratch brush.
    • accuracy: Controls the trade-off between progress tracking precision and performance using ScratchAccuracy.
    • color: The color used for the scratchable overlay (if no image is provided).
    • image: An Image widget to use as the scratchable overlay.

    Callbacks

    • onChange: Called when the revealed area changes (minimum 0.1% difference).
    • onThreshold: Called when the threshold percentage is reached.
    • onScratchStart, onScratchUpdate, onScratchEnd: Lifecycle callbacks for the user's touch/drag interaction.
    Scratcher(
      brushSize: 30, 
      threshold: 50, // Trigger onThreshold at 50%
      onChange: (progress) => print('Progress: $progress%'),
      onThreshold: () => print('Threshold reached!'),
      child: Text('Hidden Content'),
    )
  5. Control Scratcher programmatically using GlobalKey

    master

    To trigger actions like resetting the scratch card or revealing the content entirely from your code, assign a GlobalKey<ScratcherState> to the Scratcher widget. This allows you to access the ScratcherState methods.

    final scratchKey = GlobalKey<ScratcherState>();
    
    // In your build method
    Scratcher(
      key: scratchKey,
      // ... other properties
    )
    
    // To reset the scratcher
    scratchKey.currentState?.reset(duration: Duration(milliseconds: 2000));
    
    // To reveal the whole scratcher
    scratchKey.currentState?.reveal();
  6. Scratcher widget properties reference

    master

    The Scratcher widget accepts the following properties to configure the scratch card behavior and appearance:

    PropertyTypeDescription
    childWidgetWidget rendered under the scratch area.
    enabledboolWhether new scratches can be applied.
    thresholddoublePercentage level of scratch area which should be revealed to complete.
    brushSizedoubleSize of the brush. The bigger it is the faster user can scratch the card.
    accuracyScratchAccuracyDetermines how accurate the progress should be reported. Lower accuracy means higher performance.
    colorColorColor used to cover the child widget.
    imageImageImage widget used to cover the child widget.
    rebuildOnResizeboolDetermines if the scratcher should rebuild itself when space constraints change (resize).
    onChangeFunctionCallback called when new part of area is revealed (min 0.1% difference).
    onThresholdFunctionCallback called when threshold is reached (only when defined).
    onScratchStartFunctionCallback called when scratching starts.
    onScratchUpdateFunctionCallback called during scratching.
    onScratchEndFunctionCallback called when scratching ends.
  7. ScratcherState API Reference

    master

    Use ScratcherState to programmatically control the widget's state. You can obtain the state using a GlobalKey<ScratcherState>.

    Methods

    • reset({Duration? duration}): Resets the scratcher to its initial state. If a duration is provided, scratching is disabled during the transition.
    • reveal({Duration? duration}): Immediately reveals the entire child widget. If a duration is provided, the transition is animated.
    final GlobalKey<ScratcherState> _key = GlobalKey<ScratcherState>();
    
    // In your build method
    Scratcher(
      key: _key,
      child: MyWidget(),
    )
    
    // To reset programmatically
    _key.currentState?.reset(duration: Duration(milliseconds: 500));
    
    // To reveal programmatically
    _key.currentState?.reveal(duration: Duration(milliseconds: 500));
  8. Scratcher configuration properties

    master

    The following properties are available on the Scratcher widget:

    PropertyTypeDescription
    childWidgetRequired. The widget rendered under the scratch area.
    enabledboolWhether new scratches can be applied. Defaults to true.
    thresholddouble?Percentage level (0-100) to complete.
    brushSizedoubleSize of the brush. Defaults to 25.
    accuracyScratchAccuracyDetermines progress tracking precision. Defaults to ScratchAccuracy.high.
    colorColorColor used to cover the child. Defaults to Colors.black.
    imageImage?Image widget used to cover the child.
    rebuildOnResizeboolIf true, rebuilds when space constraints change. Defaults to true.
    onChangeFunction(double value)?Called when new part of area is revealed.
    onThresholdVoidCallback?Called when threshold is reached.
    onScratchStartVoidCallback?Called when scratching starts.
    onScratchUpdateVoidCallback?Called during scratching.
    onScratchEndVoidCallback?Called when scratching ends.
  9. ScratcherState methods

    master

    When using a GlobalKey<ScratcherState>, you can call the following methods on the currentState:

    • reset({Duration? duration}): Resets the scratcher state to the initial values.
    • reveal(): Reveals the whole scratcher, so that only the original child is displayed.