Transform widget and Attributes

Transform widget and Attributes

ยท

2 min read

The Transform widget in Flutter is used to apply transformations, such as rotation, scaling, and translation, to its child widget. It's commonly used to manipulate the appearance and position of UI elements.

Attributes:

  1. transform (Matrix4):

    • The transformation matrix to apply to the child widget. You can use methods like Matrix4.rotation, Matrix4.translationValues, and Matrix4.scale to create the transformation matrix.
  2. origin (Offset):

    • The origin point for the transformation. It specifies the point around which the widget will be transformed. The default value is the center of the widget.
  3. alignment (AlignmentGeometry):

    • The alignment of the origin point within the child widget's bounds. It's specified as an Alignment or FractionalOffset object. The default value is Alignment.center.
  4. child (Widget):

    • The widget to which the transformation should be applied.

Example:

import 'package:flutter/material.dart';

class TransformExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Transform Widget Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('Original Widget'),
            SizedBox(height: 20),
            Transform.rotate(
              angle: 0.5, // Rotate by 0.5 radians (approximately 29 degrees)
              child: Container(
                width: 100,
                height: 100,
                color: Colors.blue,
                child: Center(
                  child: Text(
                    'Transformed Widget',
                    style: TextStyle(color: Colors.white),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: TransformExample(),
  ));
}

Explanation:

  • In this example, a Transform widget is used to rotate a container by 0.5 radians (approximately 29 degrees).

  • The Transform.rotate constructor is used to create the transformation with an angle of 0.5 radians.

  • Inside the Transform.rotate, a container is provided as the child widget, which will be transformed.

  • The text "Transformed Widget" inside the container will be rotated along with the container.

  • When you run this code, you'll see the original widget followed by the transformed widget, which is rotated by 0.5 radians.

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย