Difference between Positioned and Transform.translate in Flutter?

Advertisements

Both can offset a widget. What should be used?

I used these inside a stack:

Transform.translate(
   offset: Offset(0.0,29),
   child: ElevatedButton(
             onPressed: (){},
             child: Text("Click me"),
          ),
)
Positioned(
   child: ElevatedButton(
             onPressed: (){},
             child: Text("Click me"),
   ),
   bottom: -29,
)

>Solution :

Positioned Widget

  • child widget can be positioned in relation to the stack.
  • you need to utilise the top, bottom, left, and right properties to specify the widget’s position when utilising the position property.

Transform.translate

  • child widget can be translated by a certain amount using the offset attribute which is used to define the amount of translation.

Depending on your use case, you may use one of these widgets in your sample code. Use Positioned if you wish to place the widget in relation to the stack. Use Transform.translate if you wish to translate the widget by a specific amount.

Further refer:

Leave a ReplyCancel reply