### Install Scratcher Package Source: https://context7.com/vintage/scratcher/llms.txt Instructions for adding the Scratcher package to your Flutter project's dependencies via pubspec.yaml and running the installation command. ```yaml dependencies: scratcher: "^2.5.0" ``` ```bash flutter pub get ``` -------------------------------- ### Scratcher Widget with Image Cover Source: https://context7.com/vintage/scratcher/llms.txt Shows how to use an image as the scratchable cover instead of a solid color. This example also demonstrates updating UI based on scratch progress and triggering a snackbar upon reaching the reveal threshold. ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class ImageScratchCard extends StatefulWidget { @override _ImageScratchCardState createState() => _ImageScratchCardState(); } class _ImageScratchCardState extends State { double progress = 0; @override Widget build(BuildContext context) { return Column( children: [ Text('Progress: ${progress.floor()}%'), Scratcher( brushSize: 40, threshold: 70, image: Image.asset('assets/scratch_cover.png'), onChange: (value) => setState(() => progress = value), onThreshold: () { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Congratulations! You revealed the prize!')) ); }, child: Container( height: 300, width: 300, decoration: BoxDecoration( image: DecorationImage( image: AssetImage('assets/prize.png'), fit: BoxFit.cover, ), ), ), ), ], ); } } ``` -------------------------------- ### Scratcher reveal() Method Example Source: https://context7.com/vintage/scratcher/llms.txt Demonstrates how to use the reveal() method to programmatically show the entire content of the Scratcher widget. This method is useful for implementing features like 'skip' buttons or automatically revealing the prize after a certain condition is met. It supports animated transitions for a smoother user experience. ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class RevealExample extends StatefulWidget { @override _RevealExampleState createState() => _RevealExampleState(); } class _RevealExampleState extends State { final scratchKey = GlobalKey(); void revealContent() { // Reveal with smooth 1.5-second animation scratchKey.currentState?.reveal( duration: Duration(milliseconds: 1500), ); } @override Widget build(BuildContext context) { return Column( children: [ TextButton( onPressed: revealContent, child: Text('Skip - Reveal Now'), ), Scratcher( key: scratchKey, brushSize: 30, threshold: 80, color: Colors.indigo, onThreshold: () => print("User scratched enough!"), child: Container( height: 200, width: 200, color: Colors.teal, alignment: Alignment.center, child: Text('Secret Message', style: TextStyle(color: Colors.white)), ), ), ], ); } } ``` -------------------------------- ### Programmatic Control of Scratcher Widget with GlobalKey Source: https://github.com/vintage/scratcher/blob/master/README.md Shows how to control the Scratcher widget programmatically by assigning a GlobalKey. This allows you to call methods like `reset` or `reveal` on the scratcher instance, for example, triggered by a button press. ```dart final scratchKey = GlobalKey(); Scratcher( key: scratchKey, // remaining properties ) RaisedButton( child: const Text('Reset'), onPressed: () { scratchKey.currentState.reset(duration: Duration(milliseconds: 2000)); }, ); ``` -------------------------------- ### Add Scratcher Dependency to pubspec.yaml Source: https://github.com/vintage/scratcher/blob/master/README.md This snippet shows how to add the scratcher package as a project dependency in your pubspec.yaml file. After adding, run `flutter pub get` to install it. ```yaml dependencies: scratcher: "^2.5.0" ``` -------------------------------- ### Scratcher reset() Method Example Source: https://context7.com/vintage/scratcher/llms.txt Illustrates the usage of the reset() method on the Scratcher widget. This method can be called to cover the content again, either instantly or with a specified animation duration. It's useful for resetting the scratcher's state after user interaction or for replaying the scratch experience. ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class ResetExample extends StatefulWidget { @override _ResetExampleState createState() => _ResetExampleState(); } class _ResetExampleState extends State { final scratchKey = GlobalKey(); void resetWithAnimation() { // Reset with 2-second animated transition scratchKey.currentState?.reset( duration: Duration(milliseconds: 2000), ); } void resetInstantly() { // Reset immediately without animation scratchKey.currentState?.reset(); } @override Widget build(BuildContext context) { return Column( children: [ ElevatedButton(onPressed: resetWithAnimation, child: Text('Animated Reset')), ElevatedButton(onPressed: resetInstantly, child: Text('Instant Reset')), Scratcher( key: scratchKey, brushSize: 25, color: Colors.black, child: Container( height: 150, width: 150, color: Colors.yellow, child: Center(child: Text('Hidden Content')), ), ), ], ); } } ``` -------------------------------- ### Toggle Scratching Interaction with Enabled Property in Dart Source: https://context7.com/vintage/scratcher/llms.txt Control user interaction with the scratcher widget using the 'enabled' property. This is useful for disabling scratching after a certain condition is met, such as revealing the content or during loading states. The example demonstrates toggling the 'enabled' state via a checkbox. ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class ToggleableScratcher extends StatefulWidget { @override _ToggleableScratcherState createState() => _ToggleableScratcherState(); } class _ToggleableScratcherState extends State { bool scratchEnabled = true; bool isRevealed = false; @override Widget build(BuildContext context) { return Column( children: [ CheckboxListTile( value: scratchEnabled, title: Text('Scratching Enabled'), onChanged: (value) => setState(() => scratchEnabled = value ?? false), ), Scratcher( enabled: scratchEnabled, // Toggle scratching on/off brushSize: 30, threshold: 60, color: Colors.grey[700]!, onThreshold: () { setState(() { isRevealed = true; scratchEnabled = false; // Disable after reveal }); }, child: Container( height: 200, width: 200, color: Colors.blue, alignment: Alignment.center, child: Text( isRevealed ? 'Revealed!' : 'Keep Scratching', style: TextStyle(color: Colors.white, fontSize: 18), ), ), ), ], ); } } ``` -------------------------------- ### Responsive Resizing with RebuildOnResize Property in Dart Source: https://context7.com/vintage/scratcher/llms.txt Manage how the scratcher widget behaves when its parent's size constraints change using the 'rebuildOnResize' property. Setting this to 'true' ensures the scratcher redraws itself correctly in responsive layouts that might resize dynamically. The example shows toggling the container size to demonstrate the effect. ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class ResponsiveScratcher extends StatefulWidget { @override _ResponsiveScratchers createState() => _ResponsiveScratchers(); } class _ResponsiveScratchers extends State { double? containerSize; @override Widget build(BuildContext context) { return Column( children: [ ElevatedButton( child: Text('Toggle Size'), onPressed: () { setState(() { containerSize = containerSize == null ? 150 : null; }); }, ), SizedBox( height: containerSize, width: containerSize, child: Scratcher( rebuildOnResize: true, // Reset when container size changes brushSize: 25, color: Colors.red, child: Container( color: Colors.cyan, alignment: Alignment.center, child: Text('Resizable'), ), ), ), ], ); } } ``` -------------------------------- ### Programmatic Control with GlobalKey Source: https://context7.com/vintage/scratcher/llms.txt Demonstrates how to use a GlobalKey to access ScratcherState methods for programmatic control, enabling reset and reveal functionality triggered by external actions. ```APIDOC ## Programmatic Control with GlobalKey Use a GlobalKey to access ScratcherState methods for programmatic control, enabling reset and reveal functionality triggered by external actions. ### Method This example shows how to integrate the Scratcher widget with buttons that trigger its `reset()` and `reveal()` methods via a `GlobalKey`. ### Endpoint N/A (This is a client-side Flutter widget example) ### Parameters N/A ### Request Example ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class ControllableScratchCard extends StatefulWidget { @override _ControllableScratchCardState createState() => _ControllableScratchCardState(); } class _ControllableScratchCardState extends State { final scratchKey = GlobalKey(); double progress = 0; bool thresholdReached = false; @override Widget build(BuildContext context) { return Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ ElevatedButton( child: Text('Reset'), onPressed: () { scratchKey.currentState?.reset( duration: Duration(milliseconds: 2000), ); setState(() => thresholdReached = false); }, ), ElevatedButton( child: Text('Reveal'), onPressed: () { scratchKey.currentState?.reveal( duration: Duration(milliseconds: 1500), ); }, ), ], ), Text('Progress: ${progress.floor()}% - ${thresholdReached ? "Complete" : "Scratching..."}'), Scratcher( key: scratchKey, brushSize: 30, threshold: 50, color: Colors.purple, onChange: (value) => setState(() => progress = value), onThreshold: () => setState(() => thresholdReached = true), child: Container( height: 200, width: 200, color: Colors.orange, alignment: Alignment.center, child: Text('Prize!', style: TextStyle(fontSize: 32)), ), ), ], ); } } ``` ### Response N/A ``` -------------------------------- ### Basic Scratcher Widget Implementation Source: https://context7.com/vintage/scratcher/llms.txt Demonstrates the basic usage of the Scratcher widget, wrapping a colored container with a scratchable overlay. It includes configuration for brush size, threshold, cover color, and callbacks for progress and threshold events. ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class SimpleScratchCard extends StatelessWidget { @override Widget build(BuildContext context) { return Scratcher( brushSize: 30, threshold: 50, color: Colors.grey, onChange: (value) => print("Scratch progress: $value%"), onThreshold: () => print("Threshold reached, content revealed!"), child: Container( height: 200, width: 200, alignment: Alignment.center, color: Colors.amber, child: Text( 'You Won!', style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), ), ), ); } } ``` -------------------------------- ### Import and Use Scratcher Widget in Dart Source: https://github.com/vintage/scratcher/blob/master/README.md Demonstrates how to import the scratcher library and use the Scratcher widget to cover another widget. It includes basic properties like brush size, threshold, and color, along with callback functions for scratch progress and threshold events. ```dart 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, ), ) ``` -------------------------------- ### Programmatic Control of Scratcher with GlobalKey Source: https://context7.com/vintage/scratcher/llms.txt Demonstrates how to use a GlobalKey to access ScratcherState methods for programmatic control. This allows external actions, like button presses, to trigger reset or reveal functionality on the scratcher widget. It includes event handlers for scratch progress and threshold completion. ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class ControllableScratchCard extends StatefulWidget { @override _ControllableScratchCardState createState() => _ControllableScratchCardState(); } class _ControllableScratchCardState extends State { final scratchKey = GlobalKey(); double progress = 0; bool thresholdReached = false; @override Widget build(BuildContext context) { return Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ ElevatedButton( child: Text('Reset'), onPressed: () { scratchKey.currentState?.reset( duration: Duration(milliseconds: 2000), ); setState(() => thresholdReached = false); }, ), ElevatedButton( child: Text('Reveal'), onPressed: () { scratchKey.currentState?.reveal( duration: Duration(milliseconds: 1500), ); }, ), ], ), Text('Progress: ${progress.floor()}% - ${thresholdReached ? "Complete" : "Scratching..."}'), Scratcher( key: scratchKey, brushSize: 30, threshold: 50, color: Colors.purple, onChange: (value) => setState(() => progress = value), onThreshold: () => setState(() => thresholdReached = true), child: Container( height: 200, width: 200, color: Colors.orange, alignment: Alignment.center, child: Text('Prize!', style: TextStyle(fontSize: 32)), ), ), ], ); } } ``` -------------------------------- ### reveal() Method Source: https://context7.com/vintage/scratcher/llms.txt Programmatically reveals the entire scratch area, showing only the original child widget. Useful for skip buttons or automatic reveals. ```APIDOC ## reveal() Method Programmatically reveals the entire scratch area, showing only the original child widget. Useful for skip buttons or automatic reveals. ### Method ```dart scratchKey.currentState?.reveal({Duration duration}); ``` ### Endpoint N/A (Client-side method) ### Parameters #### Optional Parameters - **duration** (Duration) - The duration for the reveal animation. If not provided, the reveal is instant. ### Request Example ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class RevealExample extends StatefulWidget { @override _RevealExampleState createState() => _RevealExampleState(); } class _RevealExampleState extends State { final scratchKey = GlobalKey(); void revealContent() { // Reveal with smooth 1.5-second animation scratchKey.currentState?.reveal( duration: Duration(milliseconds: 1500), ); } @override Widget build(BuildContext context) { return Column( children: [ TextButton( onPressed: revealContent, child: Text('Skip - Reveal Now'), ), Scratcher( key: scratchKey, brushSize: 30, threshold: 80, color: Colors.indigo, onThreshold: () => print("User scratched enough!"), child: Container( height: 200, width: 200, color: Colors.teal, alignment: Alignment.center, child: Text('Secret Message', style: TextStyle(color: Colors.white)), ), ), ], ); } } ``` ### Response N/A ``` -------------------------------- ### Scratch Event Callbacks with Dart Source: https://context7.com/vintage/scratcher/llms.txt Implement onScratchStart, onScratchUpdate, and onScratchEnd callbacks to track user scratching behavior. These callbacks are useful for triggering analytics, sounds, or haptic feedback based on user interaction with the scratcher widget. The 'onChange' and 'onThreshold' callbacks provide additional progress and event tracking. ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class ScratchEventTracker extends StatelessWidget { @override Widget build(BuildContext context) { return Scratcher( brushSize: 25, threshold: 50, color: Colors.brown, onScratchStart: () { print("User started scratching"); // Trigger haptic feedback or play scratch sound }, onScratchUpdate: () { print("User is scratching..."); // Update UI or play continuous sound }, onScratchEnd: () { print("User stopped scratching"); // Stop sounds or log analytics }, onChange: (value) => print("Progress: $value%"), onThreshold: () => print("Threshold reached!"), child: Container( height: 200, width: 200, color: Colors.lightGreen, child: Center(child: Text('Scratch Me!')) ), ); } } ``` -------------------------------- ### Scratcher with Performance Optimized Accuracy Source: https://context7.com/vintage/scratcher/llms.txt Illustrates how to configure the Scratcher widget for performance optimization by setting the `accuracy` property to `ScratchAccuracy.low`. This is beneficial for smaller scratch areas or performance-critical games. ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; // ScratchAccuracy.low - 10x10 checkpoint grid, highest performance // ScratchAccuracy.medium - 30x30 checkpoint grid, balanced // ScratchAccuracy.high - 100x100 checkpoint grid, most accurate (default) class PerformanceOptimizedScratcher extends StatelessWidget { @override Widget build(BuildContext context) { return Scratcher( accuracy: ScratchAccuracy.low, // Best for small scratch areas or games brushSize: 15, color: Colors.blueGrey, threshold: 60, onThreshold: () => print("Done!"), child: Container( width: 80, height: 80, color: Colors.green, child: Icon(Icons.check, color: Colors.white), ), ); } } ``` -------------------------------- ### reset() Method Source: https://context7.com/vintage/scratcher/llms.txt Resets the scratcher to its initial state, covering the content again. Optionally accepts a duration parameter for animated transition. ```APIDOC ## reset() Method Resets the scratcher to its initial state, covering the content again. Optionally accepts a duration parameter for animated transition. ### Method ```dart scratchKey.currentState?.reset({Duration duration}); ``` ### Endpoint N/A (Client-side method) ### Parameters #### Optional Parameters - **duration** (Duration) - The duration for the reset animation. If not provided, the reset is instant. ### Request Example ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class ResetExample extends StatefulWidget { @override _ResetExampleState createState() => _ResetExampleState(); } class _ResetExampleState extends State { final scratchKey = GlobalKey(); void resetWithAnimation() { // Reset with 2-second animated transition scratchKey.currentState?.reset( duration: Duration(milliseconds: 2000), ); } void resetInstantly() { // Reset immediately without animation scratchKey.currentState?.reset(); } @override Widget build(BuildContext context) { return Column( children: [ ElevatedButton(onPressed: resetWithAnimation, child: Text('Animated Reset')), ElevatedButton(onPressed: resetInstantly, child: Text('Instant Reset')), Scratcher( key: scratchKey, brushSize: 25, color: Colors.black, child: Container( height: 150, width: 150, color: Colors.yellow, child: Center(child: Text('Hidden Content')), ), ), ], ); } } ``` ### Response N/A ``` -------------------------------- ### Flutter Lottery Game with Scratcher Source: https://context7.com/vintage/scratcher/llms.txt Implements a lottery game with three scratchable cards. Each card reveals a prize amount when scratched. The game detects a win if at least two '100' prizes are revealed. It uses the Scratcher widget for the interactive scratching effect and Flutter's state management to update the UI. ```dart import 'package:flutter/material.dart'; import 'package:scratcher/scratcher.dart'; class LotteryGame extends StatefulWidget { @override _LotteryGameState createState() => _LotteryGameState(); } class _LotteryGameState extends State { int matchCount = 0; List revealed = [false, false, false]; List prizes = ['100', '100', '500']; // Two matching = win void checkCard(int index) { setState(() { revealed[index] = true; // Count how many '100' prizes are revealed matchCount = revealed.asMap().entries .where((e) => e.value && prizes[e.key] == '100') .length; }); if (matchCount >= 2) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Congratulations! You matched 2 cards!'), backgroundColor: Colors.green, ), ); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Scratch & Win')), body: Center( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: List.generate(3, (index) { return Padding( padding: EdgeInsets.all(8), child: Scratcher( accuracy: ScratchAccuracy.low, brushSize: 20, threshold: 50, color: Colors.grey[800]!, onThreshold: () => checkCard(index), child: Container( width: 80, height: 100, decoration: BoxDecoration( color: Colors.amber, borderRadius: BorderRadius.circular(8), ), alignment: Alignment.center, child: Text( ' ${prizes[index]}', style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, color: revealed[index] ? Colors.black : Colors.black54, ), ), ), ), ); }), ), ), ); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.