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

【Flutter】Columnに余白(padding)を付ける

FlutterでColumnに余白(padding)を付ける方法を紹介します。

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

目次

Columnに余白(padding)を付ける方法

Columnに余白(padding)を付けるにはPaddingを使用します。

使用方法としてはPaddingの子ウィジェットとしてColumnを指定し、paddingEdgeInsetsを渡して余白を調整します。

Padding(
  padding: const EdgeInsets.all(50),
  child: Column(
    children: [
      ...
    ],
  ),
)

指定した余白以外の隙間を埋める方法

横幅に対して指定した余白以外の隙間を埋めるにはSizedBoxを使用します。

SizedBoxの子ウィジェットとして先ほど使用したPaddingを指定しwidthdouble.infinityを渡します。

SizedBox(
    width: double.infinity,
    child: Padding(
      padding: const EdgeInsets.all(50),
      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: Padding(
            padding: const EdgeInsets.all(50),
            child: Column(
              children: [
                Container(
                  height: 150,
                  color: Colors.red,
                ),
                Container(
                  height: 150,
                  color: Colors.yellow,
                ),
                Container(
                  height: 150,
                  color: Colors.blue,
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

合わせて読みたい

参考サイト

目次