3
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 1 year has passed since last update.

Flutterでダークモード時に画像を切り替える【Dart】

Posted at

Xcode/AndroidStudioでそれぞれ開発するときは、ライトモード用とダークモード用の画像を用意しておくと勝手に切り替えてくれる機能がありますが、Flutterではそれを使うことができないようです。

以下のようにして対応します。

.dart
Image.asset(
    Theme.of(context).brightness == Brightness.dark
    ? 'assets/images/image_dark.png'
    : 'assets/images/image_light.png',
),

(bool変数) ? (trueのときの処理) : (falseのときの処理)の構文を利用して、
ダークモード(Theme.of(context).brightness == Brightness.dark)のときは、image_dark.png、
ライトモード(Theme.of(context).brightness == Brightness.light)のときはimage_light.png
を表示させます。

Twitter@ruemura3

3
2
1

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