mhannani
mhannani

Reputation: 502

Flutter - How to change the rotation point of the Container widget?

I'm using the transform property of the Container widget to rotate it around the Z-axis , just like that :

transform: Matrix4.rotationZ(10 * pi/180)

in the following snippet:

Scaffold(
  resizeToAvoidBottomInset: false,
  body: Stack(
    children: <Widget>[
      Container(

//            margin: EdgeInsets.symmetric(vertical: 20.0,horizontal: 10.0),
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage(
              "assets/img/women.png",
            ),
            fit: BoxFit.scaleDown,
          ),
        ),
      ),
      SingleChildScrollView(
        child: Container(
          width: deviceSize.width,
          height: deviceSize.height,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              Flexible(
                child: Container(
                  margin: EdgeInsets.only(bottom: 90.0),
                  padding:
                      EdgeInsets.symmetric(vertical: 10.0, horizontal: 15),
                  transform: Matrix4.rotationZ(10 * pi / 180)
                    ..translate(10.0),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(25),
                    color: Colors.orange,
                    boxShadow: [
                      BoxShadow(
                        blurRadius: 20.0,
                        color: Colors.orange,
                        offset: Offset(5, 7),
                      )
                    ],
                  ),
                  child: Text(
                    'Shopping Universe',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 60.0,
                      fontFamily: 'Piedra',
                      fontWeight: FontWeight.w200,
                    ),
                  ),
                ),
              ),
              Flexible(
                child: AuthCard(),
              ),
            ],
          ),
        ),
      ),
    ],
  ),
);

How to change the rotation point and set it to the center of gravity of the container ?

I could reposition it after that using translate ,something like that Matrix4.rotationZ(15 * pi/180)..translate(10.0)

But i'm not happy with that method ,

Could someone help me please ?

Any suggestions or advice would be appreciated. Thanks.

Upvotes: 1

Views: 4978

Answers (1)

MahMoos
MahMoos

Reputation: 1296

You can wrap the Container with a Transform widget and set origin property to whatever you wish.

If you want the origin to be in the center of the Container, you have to do something like origin: Offset(containerWidth/2, containerHeight/2).

And as your Container doesn't have a specific height(only the width is known_same as its parent), you have to find its size. You can use RenderBox for that. Here you can find a tutorial on how to achive this.

import 'package:flutter/material.dart';
import 'auth_card.dart';
import 'dart:math' as Math;

class Test extends StatefulWidget {
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  GlobalKey _containerKey = GlobalKey();
  Size _containerSize = Size(0, 0);

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback(_afterLayout);
    super.initState();
  }

  Size _getSize() {
    final RenderBox renderBoxRed =
    _containerKey.currentContext.findRenderObject();
    return renderBoxRed.size;
  }

  _afterLayout(_) {
    _containerSize = _getSize();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    Size deviceSize = MediaQuery.of(context).size;
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Stack(
        children: <Widget>[
          Container(

//            margin: EdgeInsets.symmetric(vertical: 20.0,horizontal: 10.0),
            decoration: BoxDecoration(
              image: DecorationImage(
                image: AssetImage(
                  "assets/img/women.png",
                ),
                fit: BoxFit.scaleDown,
              ),
            ),
          ),
          SingleChildScrollView(
            child: Container(
              width: deviceSize.width,
              height: deviceSize.height,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  Flexible(
                    child: Transform(
                      transform: Matrix4.rotationZ(10 * Math.pi / 180),
                      origin: Offset(
                          _containerSize.width / 2, _containerSize.height / 2),
                      child: Container(
                        key: _containerKey,
                        margin: EdgeInsets.only(bottom: 20.0),
                        padding: EdgeInsets.symmetric(
                            vertical: 20.0, horizontal: 15),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(25),
                          color: Colors.orange,
                          boxShadow: [
                            BoxShadow(
                              blurRadius: 20.0,
                              color: Colors.orange,
                              offset: Offset(5, 7),
                            )
                          ],
                        ),
                        child: Text(
                          'Shopping Universe',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 60.0,
                            fontFamily: 'Piedra',
                            fontWeight: FontWeight.w200,
                          ),
                        ),
                      ),
                    ),
                  ),
                  Flexible(
                    child: AuthCard()
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

But note that:

Setting an origin is equivalent to conjugating the transform matrix by a translation. This property is provided just for convenience.

Upvotes: 2

Related Questions