Created
December 20, 2023 11:37
-
-
Save dipendra-sharma/0c1bed73bdf9a0e4d99f6f33319143b5 to your computer and use it in GitHub Desktop.
Implementing rotation functionality within InteractiveViewer would be highly beneficial. For those looking to incorporate similar features.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InteractiveViewerWithRotation extends StatefulWidget { | |
final Widget child; | |
const InteractiveViewerWithRotation({super.key, required this.child}); | |
@override | |
State<InteractiveViewerWithRotation> createState() => | |
_InteractiveViewerWithRotationState(); | |
} | |
class _InteractiveViewerWithRotationState | |
extends State<InteractiveViewerWithRotation> { | |
final RotationPositionNotifier _rotationPositionNotifier = | |
RotationPositionNotifier(); | |
@override | |
Widget build(BuildContext context) { | |
return InteractiveViewer( | |
onInteractionStart: (ScaleStartDetails details) { | |
_rotationPositionNotifier | |
.updatePreviousAngle(_rotationPositionNotifier.angle); | |
}, | |
onInteractionUpdate: (ScaleUpdateDetails details) { | |
print("Scale: ${details.scale}"); | |
// Update the angle and position using the notifier | |
_rotationPositionNotifier.updateRotation( | |
_rotationPositionNotifier.previousAngle + details.rotation); | |
_rotationPositionNotifier.updatePosition(details.focalPointDelta); | |
}, | |
child: AnimatedBuilder( | |
animation: _rotationPositionNotifier, | |
builder: (context, child) { | |
return Transform.rotate( | |
angle: _rotationPositionNotifier.angle, | |
child: Transform.translate( | |
offset: _rotationPositionNotifier.position, | |
child: widget.child, | |
), | |
); | |
}, | |
), | |
); | |
} | |
} | |
// ChangeNotifier class to manage the state of the rotation and position | |
class RotationPositionNotifier with ChangeNotifier { | |
double _previousAngle = 0.0; | |
double _angle = 0.0; | |
Offset _position = Offset.zero; | |
double get previousAngle => _previousAngle; | |
double get angle => _angle; | |
Offset get position => _position; | |
// Method to update the angle and notify listeners | |
void updateRotation(double newAngle) { | |
_angle = newAngle; | |
notifyListeners(); | |
} | |
void updatePreviousAngle(double newAngle) { | |
_previousAngle = newAngle; | |
notifyListeners(); | |
} | |
// Method to update the position and notify listeners | |
void updatePosition(Offset newPositionDelta) { | |
_position += newPositionDelta; | |
notifyListeners(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment