4
2

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 Containerに背景色をつける方法

Last updated at Posted at 2021-04-25

Containerに背景色をつけるには?

動画のように、背景色がテキストの色と被ってる場合は非常に読みにくくなる。。。

// 省略
 final routeArgs =
        ModalRoute.of(context).settings.arguments as Map<String, Object>;
    final selectedMovie = routeArgs;
    final backdropPath = selectedMovie['backdropPath'];

// 省略

 SliverAppBar(
              expandedHeight: 250,
              flexibleSpace: FlexibleSpaceBar(
                title: Container(
                  padding: EdgeInsets.symmetric(horizontal: 20, vertical: 2),
                  child: Text(
                    selectedMovie['title'],
                  ),
                ),
                background: Image.network(
                  'https://image.tmdb.org/t/p/w780/${backdropPath}',
                  fit: BoxFit.fitHeight,
                ),
              ),
            ),

なのでContainerに背景色をつけたい。
でもContainerbackgroundColorのプロパティ?を持っていない!

さてどうしようか。。

BoxDecorationを使う

// 省略
 final routeArgs =
        ModalRoute.of(context).settings.arguments as Map<String, Object>;
    final selectedMovie = routeArgs;
    final backdropPath = selectedMovie['backdropPath'];

// 省略

 SliverAppBar(
              expandedHeight: 250,
              flexibleSpace: FlexibleSpaceBar(
                title: Container(
                  decoration: BoxDecoration(color: Colors.black), // 追加
                  padding: EdgeInsets.symmetric(horizontal: 20, vertical: 2),
                  child: Text(
                    selectedMovie['title'],
                  ),
                ),
                background: Image.network(
                  'https://image.tmdb.org/t/p/w780/${backdropPath}',
                  fit: BoxFit.fitHeight,
                ),
              ),
            ),

Containerにdecorationを指定してBoxDecorationに色をつけましょう。
それでこんな感じに色がつきます!

スクリーンショット 2021-04-25 18.29.47.jpg

 BoxDecoration(color: Colors.black.withOpacity(0.4)),

Opacityをつけて、背景を透明にさせてもいいですね!

スクリーンショット 2021-04-25 18.35.53.jpg

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?