6
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】画面全体に背景色を設定する

Posted at

やりたいこと

画面全体に背景色を設定したい。

実装

最初にやったのはrootのWidgetにContainerを設定してcolorプロパティに設定しました。
画面に表示するWidgetはContainerのchildプロパティに設定すれば良いと思いましたが、結果はこうなりました。

sample.dart
class ScreenView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Container(
        color: Colors.orange,
        child: Column(
          children: [
            Text('あいうえお'),
          ],
        ),
      ),
    );
  }
}

ezgif.com-gif-maker.png

これだとchildのWidgetによって親のContainerのサイズが決まってしまうので、画面全体に背景色を設定できません。

Containerにwidthとheightを設定して画面全体の大きさにすれば解決できそうですが、クールではありません。

結論ScaffoldのbackgroundColorを使えば画面全体に背景色を設定することができました。

sample.dart
class ScreenView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.orange, // 背景色設定
      appBar: AppBar(),
      body: Container(
        child: Column(
          children: [
            Text('あいうえお'),
          ],
        ),
      ),
    );
  }
}

ezgif.com-gif-maker (1).png

以上です。

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