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

【Flutter】Flatbutton内にwidget配置したらサイドスペースができてしまう

Last updated at Posted at 2020-05-02

はじめに

Flatbuttonを配置して、その中にTextとかImage.asset等のwidgetを配置すると、
配置したwidgetのサイドに謎のスペースが出来て少し困ったので、対処方法を載せます。
(他のボタンwidgetは試していませんので、自分で試してみてください!)

開発環境

言語/フレームワーク Version
Flutter 1.12.13+hotfix.9
Dart 2.3.1

サクッとデモ

失敗コード

例として、こんなコードを書いたとします。
300x100のFlatbuttonを作り、その中に300x100のTextを配置したものです。

main.dart
class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Container(
            width: 300,
            height: 100,
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red),
            ),
            child: FlatButton(
              onPressed: () {},
              child: Container(
                width: 300,
                height: 100,
                color: Colors.blue,
                child: Center(
                  child: Text(
                    "ボタン",
                    style: TextStyle(
                      fontSize: 20,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

↓実際に実機にbuildしたものがこちら↓ (写真の赤枠はFlatbutton、青の長方形がText部分です。)
IMG_0564.PNG

成功コード

サイドスペースが出来てしまう理由としては、Flatbuttonウィジェットがデフォルトでpaddingかけてしまうらしいので、

padding: EdgeInsets.all(0.0),

を指定してあげればおkです。

↓実機で見てみても、ちゃんとピッタリ重なりましたね。↓
IMG_0566.PNG

全体コード

これが完成コードです。

main.dart
class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Container(
            width: 300,
            height: 100,
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red),
            ),
            child: FlatButton(
              padding: EdgeInsets.all(0.0), //追加したところ
              onPressed: () {},
              child: Container(
                width: 300,
                height: 100,
                color: Colors.blue,
                child: Center(
                  child: Text(
                    "ボタン",
                    style: TextStyle(
                      fontSize: 20,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

結果

解決したらなんだこんなことかよ!って思うよね(全部とは言わない)

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