简体   繁体   中英

How to align a widget to the right of a dynamic centered widget in Flutter?

I have a text widget in the center that has a dynamic width. However, I want to add in a widget to the right of this text widget.

Because this center text wiget has a dynamic width, I cannot use absolute positioning widgets such as Align or the Position Widget. I am looking for something similar to RelativeLayout in Android.

在此处输入图像描述

  Widget body() {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text('Center text'),
          FlatButton(
            child: Text('next'),
            onPressed: () {},
          ),
        ],
      ),
    );
  }

With @Igors method I can end with a center widget with a button on each end evenly spaced. I am aiming for the button to be to the right of.

  Widget body() {
    return Container(
      child: Row(
        children: <Widget>[
          Expanded(
            child: FlatButton(
              child: Text('next'),
              onPressed: () {},
            ),
          ),
          Container(color: Colors.red, child: Text('test')),
          Expanded(
            child: FlatButton(
              child: Text('next'),
              onPressed: () {},
            ),
          ),
        ],
      ),
    );
  }

Edit: Solved through the use of Sized Box on each side of the centered widget 1.

Row(
  children: <Widget>[
    Expanded(
      child: Container(
        color: Colors.green
      ),
    ),
    Container(
      color: Colors.red,
      child: Text('test')
    ),
    Expanded(
      child: Container(
        color: Colors.blue
      ),
    ),
  ],
)

Use the Widget Row and add the text widget in a Container Widget.

And add margin for the Container

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM