Skip to main content

Command Palette

Search for a command to run...

Transform widget and Attributes

Published
2 min read
Transform widget and Attributes
V

"Hello World, I'm Vinit Mepani, a coding virtuoso driven by passion, fueled by curiosity, and always poised to conquer challenges. Picture me as a digital explorer, navigating through the vast realms of code, forever in pursuit of innovation.

In the enchanting kingdom of algorithms and syntax, I wield my keyboard as a magical wand, casting spells of logic and crafting solutions to digital enigmas. With each line of code, I embark on an odyssey of learning, embracing the ever-evolving landscape of technology.

Eager to decode the secrets of the programming universe, I see challenges not as obstacles but as thrilling quests, opportunities to push boundaries and uncover new dimensions in the realm of possibilities.

In this symphony of zeros and ones, I am Vinit Mepani, a coder by passion, an adventurer in the digital wilderness, and a seeker of knowledge in the enchanting world of code. Join me on this quest, and let's create digital wonders together!"

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.

More from this blog

Vinit Mepani (Flutter Developer)

270 posts

"Vinit Mepani, passionate coder! Dive into my Dart and Flutter journey on the blog. Let's master these tech wonders together. Happy coding! 🚀"