Transform Widget
The Transform widget applies geometric transformations to its child widget using a 4x4 transformation matrix.
Common Transformations
-
Rotation:
Transform({ transform: Matrix4.rotationZ(angle), child: /* widget */ })
-
Scale:
Transform({ transform: Matrix4.diagonal3Values(scaleX, scaleY, 1), child: /* widget */ })
-
Translation:
Transform({ transform: Matrix4.translationValues(x, y, 0), child: /* widget */ })
-
Skew:
Transform({ transform: Matrix4.skewX(value), child: /* widget */ })
Matrix4 Operations
-
Basic Operations:
Matrix4.identity()
: Creates identity matrixMatrix4.rotationZ()
: Rotation around Z axisMatrix4.translationValues()
: Translation in X, Y, ZMatrix4.diagonal3Values()
: Scale in X, Y, Z
-
Combining Transformations:
const matrix = Matrix4.identity() .multiply(Matrix4.rotationZ(angle)) .multiply(Matrix4.translationValues(x, y, 0));
Common Use Cases
-
Card Flip Effect:
Transform({ transform: Matrix4.rotationY(isFlipped ? Math.PI : 0), child: Card() })
-
Hover Scale:
Transform({ transform: Matrix4.diagonal3Values( isHovered ? 1.1 : 1, isHovered ? 1.1 : 1, 1 ), child: Button() })
-
3D Rotation:
Transform({ transform: Matrix4.rotationX(angleX) .multiply(Matrix4.rotationY(angleY)), child: Container() })
Best Practices
-
Performance:
- Use hardware acceleration when possible
- Avoid unnecessary transformations
- Consider using Transform.translate for simple translations
-
Animation:
- Use smooth transitions
- Consider performance impact
- Test on different devices
-
Layout Considerations:
- Be aware of transformed bounds
- Handle touch/click events correctly
- Consider parent constraints
Tips and Tricks
-
Origin Point:
- Set transform origin for rotations
- Consider widget center vs corner
- Use alignment property
-
3D Effects:
- Use perspective for depth
- Combine multiple transformations
- Test on different viewports
-
Debugging:
- Use debug paint to visualize
- Check transformed bounds
- Verify touch areas