世界最大級のオンライン学習サービス「Udemy」のセール状況はこちら

【Flutter】Columnの要素を横幅いっぱいに表示

FlutterでColumnの要素を横幅いっぱいに表示する方法を紹介します。

Column基本的な使い方、知っておきたい基本プロパティのまとめ記事はこちら↓

目次

Columnを横幅いっぱいに表示する方法

Columnの要素を横幅いっぱいに表示するにはSizedBoxを使用します。

使用方法としてはSizedBoxの子ウィジェットとしてColumnを指定し、widthdouble.infinityを渡します。

SizedBox(
  width: double.infinity,
  child: Column(
    children: [
      ...  
    ],
  ),
)

Columnの要素を横幅いっぱいに表示する方法

Columnの要素を横幅いっぱいに表示したい場合は、次のように要素のwidthを指定せずheightのみ指定します。

SizedBox(
  width: double.infinity,
  child: Column(
    children: [
      Container(
        height: 150,
        color: Colors.red,
      ),
      Container(
        height: 150,
        color: Colors.yellow,
      ),
      Container(
        height: 150,
        color: Colors.blue,
      ),
    ],
  ),
)

サンプルコード

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: const Text('Flutter')),
        body: SizedBox(
          width: double.infinity,
          child: Column(
            children: [
              Container(
                height: 150,
                color: Colors.red,
              ),
              Container(
                height: 150,
                color: Colors.yellow,
              ),
              Container(
                height: 150,
                color: Colors.blue,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

合わせて読みたい

参考サイト

目次