[Flutter]Containerを並べて表示するには?

Flutter

どうも、ちょげ(@chogetarou)です。

Containerを並べて表示する方法を紹介します。

スポンサーリンク

方法

インターフェース, インターネット, プログラム, ブラウザ, Www

Containerを縦に並べて表示するには、Columnを使います。

Columnの引数「children」に[]を指定し、[]内に並べたいContainerをカンマ(,)区切りで指定します。

Column(
    children : [
          Container(),
          Container(),
          Container(),
          ・・・
    ]
)

childrenに指定されたコンテナは、上から順に表示されます。

使用例
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              color: Colors.blue,
              height: 200,
              width: 200,
            ),
            Container(
              color: Colors.green,
              height: 200,
              width: 200,
            ),
            Container(
              color: Colors.yellow,
              height: 200,
              width: 200,
            ),
          ],
        ),
      ),
    );
  }

横に並べる

プログラマー, プログラミング, コード, 仕事, コンピューター

Containerを横に並べるには、 Rowウェジェットを使います。

Rowの引数「children」に[]を指定し、[]内に並べたいContainerをカンマ(,)区切りで指定します。

Row(
    children : [
          Container(),
          Container(),
          Container(),
          ・・・
    ]
)

childrenに指定されたコンテナは、左から順に表示されます。

使用例
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              color: Colors.blue,
              height: 100,
              width: 100,
            ),
            Container(
              color: Colors.green,
              height: 100,
              width: 100,
            ),
            Container(
              color: Colors.yellow,
              height: 100,
              width: 100,
            ),
          ],
        ),
      ),
    );
  }

まとめ

Containerを縦に並べて表示するならColumn、横に並べて表示するにはRowを使います。

コメント

タイトルとURLをコピーしました