7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Flutter: RowのWidgetを端に配置したい

Posted at

やりたいこと

以下のように、Rowの中のText「World」を右端に配置したい。

修正前

// 修正前コード
Container(
  color: Colors.grey.withAlpha(50),
  child: Row(
    children: <Widget>[
      Text(
        "Hello",
        style: TextStyle(
          fontSize: 24,
          fontWeight: FontWeight.bold,
        ),
      ),
      Text( // これを右端に寄せたい
        "World",
        style: TextStyle(fontSize: 16),
      ),
    ],
  ),
)

方法1 Expanded

Expandedを使うと実現できるが、階層が深くなるのでベストな方法ではない。

Container(
  color: Colors.grey.withAlpha(50),
  child: Row(
    children: <Widget>[
      Expanded(         // <= 追加
        child: Text(
          "Hello",
          style: TextStyle(
            fontSize: 24,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      Text(
        "World",
        style: TextStyle(fontSize: 16),
      ),
    ],
  ),
)

方法2 MainAxisAlignment.spaceBetween

Rowに MainAxisAlignment.spaceBetweenを指定する。
これが一番ベストかな

Container(
  color: Colors.grey.withAlpha(50),
  child: Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,  // <= 追加
    children: <Widget>[
      Text(
        "Hello",
        style: TextStyle(
          fontSize: 24,
          fontWeight: FontWeight.bold,
        ),
      ),
      Text(
        "World",
        style: TextStyle(fontSize: 16),
      ),
    ],
  ),
)

方法3 Spacer

TextとTextの間にSpacerを插入する。
これもシンプルな方法で良い

Container(
  color: Colors.grey.withAlpha(50),
  child: Row(
    children: <Widget>[
      Text(
        "Hello",
        style: TextStyle(
          fontSize: 24,
          fontWeight: FontWeight.bold,
        ),
      ),
      Spacer(), // <= 追加
      Text(
        "World",
        style: TextStyle(fontSize: 16),
      ),
    ],
  ),
)

使用コード

7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?