0
0

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 3 years have passed since last update.

【Flutter】ColumnとRowのmainAxisAlignmentとcrossAxisAlignmentの関係性

Posted at

はじめに

FlutterのcrossAxisAlignmentとmainAxisAlignmentの違いについて、ColumnとRowで実際に変更しながら確認しまとめます。参考になれば幸いです。

使用するもの

  • Column
  • Row

Column

縦方向に並べる際に使用する。

基本となるコード

import 'package:flutter/material.dart';

class TopPage extends StatefulWidget {
  const TopPage({Key? key}) : super(key: key);

  @override
  _TopPageState createState() => _TopPageState();
}

class _TopPageState extends State<TopPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Column'),
      ),
      body: Column(
        // 以下の2つをこれから変更していく
        // mainAxisAlignment: MainAxisAlignment.start,
        // crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 200,
            height: 200,
            color: Colors.red,
          ),

          Container(
            width: 100,
            height: 100,
            color: Colors.yellow,
          ),

          Container(
            width: 50,
            height: 50,
            color: Colors.green,
          ),

          Container(
            width: 25,
            height: 25,
            color: Colors.blue,
          ),

        ],
      ),
    );
  }
}

これを元にして処理を加えていく

mainAxisAlignment

start

上に寄る。

Screenshot_1640749807.png

center

中央にくる。

Screenshot_1640749849.png

end

下に寄る。

Screenshot_1640749884.png

spaceAround

縦方向の余白を端にも同じ分設ける。

Screenshot_1640749914.png

spaceBetween

縦方向の余白を要素間のみ均等に設ける。

Screenshot_1640749953.png

spaceEvenly

縦方向の余白を全て同じ分設ける。

Screenshot_1640749990.png

crossAxisAlignment

start

左に寄る。

Screenshot_1640750406.png

center

中央にくる。

Screenshot_1640750429.png

end

右に寄る。

Screenshot_1640750456.png

stretch

要素が画面横方向に伸びる。

Screenshot_1640750485.png

Row

横方向に並べる際に使用する。

基本となるコード

class _TopPageState extends State<TopPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Row'),
      ),
      body: Row(
        // 以下の2つをこれから変更していく
        // mainAxisAlignment: MainAxisAlignment.start,
        // crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            width: 150,
            height: 150,
            color: Colors.red,
          ),

          Container(
            width: 100,
            height: 100,
            color: Colors.yellow,
          ),

          Container(
            width: 50,
            height: 50,
            color: Colors.green,
          ),

          Container(
            width: 25,
            height: 25,
            color: Colors.blue,
          ),

        ],
      ),
    );
  }
}

これを元にして処理を加えていく

mainAxisAlignment

start

左に寄る。
Screenshot_1640750943.png

center

中央にくる。
Screenshot_1640750969.png

end

右に寄る。
Screenshot_1640750991.png

spaceAround

横方向の余白を端にも同じ分設ける。
Screenshot_1640751017.png

spaceBetween

横方向の余白を要素間のみ均等に設ける。
Screenshot_1640751074.png

spaceEvenly

縦方向の余白を全て同じ分設ける。
Screenshot_1640751103.png

crossAxisAlignment

start

上に寄る。
Screenshot_1640751130.png

center

中央にくる。
Screenshot_1640751163.png

end

下に寄る。
Screenshot_1640751176.png

stretch

要素が画面縦方向に伸びる。
Screenshot_1640751197.png

まとめ

  • mainAxisAlignment
    • それぞれの方向に影響を与える
  • crossAxisAlignment
    • 縦横反対の方向に影響を与える

おわりに

きちんと認識出来ていなかった部分があったので良い確認になりました!

ここまで読んでいただき、ありがとうございました。
何かありましたらコメントをお願いします。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?