'Flutter Transform translate a widget from current position to another position

If I want to translate any Widget in my flutter app, I will use below code.


Translate function required Offset (dx,dy). But I want to find dx or dy value of the current widget so I can slightly move widget to left and right or top and bottom from the current position on device.

Solution 1:[1]

That's how it works

If I do

Transform.translate(offset: Offset(-10, 20)

It's transform its position 10 to the left and 20 down from its normal render position.

Solution 2:[2]

It's not so easy to do. You have to work with render objects. You can copy-paste Transform and RenderTransform classes to begin with. RenderTransform has overridden method paint and its argument offset is widget's local position:

  void paint(PaintingContext context, Offset offset) {
    if (child != null) {
      final Matrix4 transform = _effectiveTransform;
      final Offset childOffset = MatrixUtils.getAsTranslation(transform);
      if (childOffset == null) {
        layer = context.pushTransform(needsCompositing, offset, transform, super.paint, oldLayer: layer);
      } else {
        super.paint(context, offset + childOffset);
        layer = null;

For example, I'll move the widget to 0 position on X, if its X position is less than 20:

    if (child != null) {
      final Matrix4 transform = _effectiveTransform;
      Offset childOffset;
      // Move it to the edge, if X position is less than 20
      if (offset.dx < 20) {
        childOffset = Offset(-offset.dx, 0);
      } else {
        childOffset = MatrixUtils.getAsTranslation(transform);
      if (childOffset == null) {


And then I'll add 10 pixels padding from left in parent's widget, but the widget will stick to the left side:

  padding: EdgeInsets.only(top: 20, left: 10),
  child: MyTransform.translate(
    offset: Offset.zero,
    child: Container(
      width: 100,
      height: 100,
      color: Colors.red



This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew
Solution 2 Igor Kharakhordin