How to center one element and place the second element next to it

291 Views Asked by At

In my layout I have two Widgets in a row, a text and a button.

How can I achieve something like below, where only the Text is centered, and the icon is simply next to it?

---------------------------
           Text *          
---------------------------

Using Row would center all the contents and would output something like

---------------------------
          Text *           
---------------------------

Tried: Row(children:[widget1, widget2], mainAxisAlignment: MainAxisAlignment.center); But this centers both items, causing the text to look off-centered.

2

There are 2 best solutions below

5
Md. Yeasin Sheikh On

You can use CompositedTransformTarget and CompositedTransformFollower as mentioned on comment section by pskink.

class AppPT extends StatelessWidget {
  const AppPT({super.key});

  @override
  Widget build(BuildContext context) {
    final LayerLink _layerLink = LayerLink();

    return Scaffold(
      body: Column(
        children: [
          Stack(
            children: [
              Align(
                child: CompositedTransformTarget(
                  link: _layerLink,
                  child: Container(
                    color: Colors.red,
                    width: 100,
                    height: 60,
                    alignment: Alignment.center,
                    child: Text("btn"),
                  ),
                ),
              ),
              CompositedTransformFollower(
                link: _layerLink,
                targetAnchor: Alignment.centerRight,
                followerAnchor: Alignment.centerLeft,
                child: Container(
                  color: Colors.cyanAccent,
                  width: 12,
                  height: 12,
                  alignment: Alignment.center,
                  child: Text("*"),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }
}

There are few tricks I can think of,

  • You can use Stack widget with Position.

  • including another widget on right by applying opacity(invisible) on current snippet.

  • using transform will be handle if you know the width of the widget.

Transform.translate(
  offset: Offset(20 / 2, 0), //20 is the * box size
  child: Row(
    mainAxisAlignment: MainAxisAlignment.center,
    children: [
      Container(
        color: Colors.red,
        width: 100,
        height: 60,
        alignment: Alignment.center,
        child: Text("btn"),
      ),
      Container(
        color: Colors.green,
        width: 20,
        height: 20,
        child: Center(child: Text("*")),
      ),
    ],
  ),
),

enter image description here

1
Fuad All On

Place text and second widget inside row then put the row inside container with alignment center and use SizedBox for spacing between widget instead of Padding Widget.

Container(
      color: Colors.green,
      alignment: Alignment.center,
      height: 100,
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: const [
          Text("Text"),
          SizedBox(width: 10),
          Text("*"),
        ],
      ),
    );

enter image description here