1
1

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 1 year has passed since last update.

【Flutter】Rowで片方だけを真ん中にする

1
Posted at

はじめに

FlutterのRowを使って、左側のWidgetだけとか、右側のWidgetだけを真ん中に配置するという方法に意外に苦戦したので、実装方法を記事にしたいと思います。
デザインとしては下記のように、Centerというテキストが真ん中に配置されて、その右にlableというテキストが配置されるというデザインです。

image.png

配置方法

下記のような形で、両側に同じ幅の領域を取り、真ん中の部分に真ん中へ配置したいWidgetを並べます。
スクリーンショット 2023-12-23 12.35.59.png

コードにすると下記のようになります。

Row(
  children: [
    Expanded(
      child: SizedBox()
    ),
    Center(
      child: Text(
        "Center",
      ),
    ),
    Expanded(
      child: Padding(
        padding: EdgeInsets.only(left: 8.0),
        child: Text("Right"),
      ),
    ),
  ],
),

image.png

注意点

冒頭で見せたデザインを実装したい場合は、ExpandedではなくFlexibleを使いましょう。
下記のようにExpandedを使うと、横幅いっぱいに広がってしまいます。

Row(
  children: [
    Expanded(
      child: SizedBox()
    ),
    Center(
      child: Text(
        "Center",
      ),
    ),
    Expanded(
      child: Padding(
        padding: const EdgeInsets.only(left: 8.0),
        child: Container(
          decoration: BoxDecoration(
            color: Colors.blue,
            borderRadius: BorderRadius.circular(16),
          ),
          // heightとwidthはExpanded内だと効かない
          height: 32,
          width: 80,
          child: const Center(child: Text("Right")),
        ),
      ),
    ),
  ],
),

image.png

下記のようにFlexibleを使えば解決します。

Row(
  children: [
    Expanded(
      child: SizedBox()
    ),
    Center(
      child: Text(
        "Center",
      ),
    ),
    Flexible(
      child: Padding(
        padding: const EdgeInsets.only(left: 8.0),
        child: Container(
          decoration: BoxDecoration(
            color: Colors.blue,
            borderRadius: BorderRadius.circular(16),
          ),
          // heightとwidthはExpanded内だと効かない
          height: 32,
          width: 80,
          child: const Center(child: Text("Right")),
        ),
      ),
    ),
  ],
),

image.png

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?