동적 색상 및 적응형 라이트·다크 테마
사용자의 배경화면에 맞춰 색상 팔레트를 조정하고 라이트 모드와 다크 모드 사이를 매끄럽게 전환합니다.
동적 색상 및 적응형 라이트·다크 테마은(는) CoddyKit의 무료 Flutter Mobile Development 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Flutter Mobile Development 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Flutter Mobile Development 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Why Dynamic Color Matters
Material 3 introduced dynamic color: an app's palette can be derived from the user's wallpaper (Android 12+) so your UI feels personal and native to the device.
- On supported devices, the system exposes a
ColorSchemesourced from the wallpaper. - On older devices or other platforms, you fall back to a brand seed color.
- The same logic must produce both a light and a dark scheme.
In this lesson you'll wire up dynamic color, build adaptive light/dark themes, and let the system decide which one to show.
Seed Color as the Foundation
Even with dynamic color, you always need a fallback. Material 3's ColorScheme.fromSeed generates a full, accessible palette from a single seed color.
- Pass
brightnessto get the matching light or dark variant. - The generated scheme guarantees correct contrast between roles like
primaryandonPrimary.
import 'package:flutter/material.dart';
final ColorScheme lightScheme = ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
brightness: Brightness.light,
);
final ColorScheme darkScheme = ColorScheme.fromSeed(
seedColor: const Color(0xFF6750A4),
brightness: Brightness.dark,
);Building Light and Dark ThemeData
A ThemeData wraps a ColorScheme plus typography and component styles. With Material 3 you set useMaterial3: true and feed in the scheme.
- Create one
ThemeDataper brightness. - Keep them consistent by deriving both from the same seed.
ThemeData buildTheme(ColorScheme scheme) {
return ThemeData(
useMaterial3: true,
colorScheme: scheme,
appBarTheme: AppBarTheme(
backgroundColor: scheme.surface,
foregroundColor: scheme.onSurface,
),
);
}Letting the System Pick the Mode
MaterialApp accepts both a theme (light) and a darkTheme. The themeMode property decides which is active.
ThemeMode.systemfollows the OS setting automatically.ThemeMode.light/ThemeMode.darkforce a mode (useful for an in-app toggle).
With ThemeMode.system, Flutter switches seamlessly when the user flips dark mode in their device settings.
MaterialApp(
theme: buildTheme(lightScheme),
darkTheme: buildTheme(darkScheme),
themeMode: ThemeMode.system,
home: const HomePage(),
);Reading Dynamic Color from the System
The dynamic_color package exposes the wallpaper-based palette via the DynamicColorBuilder widget. Its builder gives you optional light and dark ColorSchemes.
- If the device supports dynamic color, both schemes are non-null.
- If not, they are
nulland you fall back to your seed.
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
DynamicColorBuilder(
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
final light = lightDynamic ?? lightScheme;
final dark = darkDynamic ?? darkScheme;
return MaterialApp(
theme: buildTheme(light),
darkTheme: buildTheme(dark),
themeMode: ThemeMode.system,
home: const HomePage(),
);
},
);Harmonizing Brand Colors
When you blend dynamic wallpaper colors with your brand color, the two can clash. The harmonize helpers shift a custom color toward the dynamic primary so accents feel cohesive.
- Use
harmonizeWithon aColoragainst the scheme's primary. - This keeps custom semantic colors (success, warning) on-brand with the wallpaper.
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
ColorScheme harmonizeScheme(ColorScheme scheme) {
return scheme.harmonized();
}
Color harmonizeBrand(Color brand, ColorScheme scheme) {
return brand.harmonizeWith(scheme.primary);
}Color Roles, Not Hard-Coded Hex
For themes to adapt, widgets must read semantic roles from Theme.of(context).colorScheme instead of hard-coded colors.
scheme.primarywithscheme.onPrimaryfor prominent actions.scheme.surfacewithscheme.onSurfacefor backgrounds and text.scheme.surfaceContainerHighestfor subtle cards.
Because these roles flip with brightness, your widgets become light/dark agnostic automatically.
Widget buildCard(BuildContext context) {
final scheme = Theme.of(context).colorScheme;
return Container(
color: scheme.surfaceContainerHighest,
child: Text(
'Adaptive',
style: TextStyle(color: scheme.onSurface),
),
);
}Pure Dart: Picking a Scheme by Brightness
The decision logic for which scheme to use is plain Dart. Here is a tiny, runnable model of the fallback rule: prefer the dynamic scheme, otherwise use the seed-derived one.
- This mirrors the
lightDynamic ?? lightSchemepattern. - No Flutter widgets needed — just the selection rule.
enum Brightness { light, dark }
String chooseScheme({
required Brightness brightness,
required bool supportsDynamic,
}) {
final base = supportsDynamic ? 'dynamic' : 'seed';
return '$base-${brightness.name}';
}
void main() {
print(chooseScheme(brightness: Brightness.dark, supportsDynamic: true));
print(chooseScheme(brightness: Brightness.light, supportsDynamic: false));
}An In-App Theme Toggle
Users often want to override the system. Store a ThemeMode in state and pass it to MaterialApp. A ValueNotifier is a lightweight way to rebuild on change.
- Default to
ThemeMode.systemso wallpaper/dark-mode still apply. - Let the toggle pick light, dark, or back to system.
final themeModeNotifier = ValueNotifier<ThemeMode>(ThemeMode.system);
ValueListenableBuilder<ThemeMode>(
valueListenable: themeModeNotifier,
builder: (context, mode, _) {
return MaterialApp(
theme: buildTheme(lightScheme),
darkTheme: buildTheme(darkScheme),
themeMode: mode,
home: const HomePage(),
);
},
);Reacting to the Effective Brightness
Inside widgets you sometimes need to know which mode is actually rendering (for example, to pick a different illustration). Read it from the active theme, not from the device, because an in-app toggle may override the system.
Theme.of(context).brightnessreflects the resolved theme.- Avoid
MediaQuery.platformBrightnessOfwhen a toggle can override it.
Widget heroImage(BuildContext context) {
final isDark = Theme.of(context).brightness == Brightness.dark;
return Image.asset(
isDark ? 'assets/hero_dark.png' : 'assets/hero_light.png',
);
}Testing Both Modes Quickly
Always verify your UI in both schemes before shipping. Two practical habits:
- Toggle dark mode in the device/emulator settings to confirm
ThemeMode.systemreacts. - Temporarily force
themeMode: ThemeMode.darkto catch hard-coded colors and low-contrast text.
Common bugs: a hard-coded Colors.black text that vanishes on a dark surface, or a custom accent that never harmonized with the dynamic palette.
Quick Check: Choosing themeMode
You want your app to automatically match the user's wallpaper-based dynamic colors AND follow their OS light/dark setting, while still allowing an optional in-app override later. What is the right baseline configuration?
Recap: Adaptive Material 3 Theming
You built a palette that adapts to both wallpaper and brightness:
- Seed fallback:
ColorScheme.fromSeedfor light and dark when dynamic color is unavailable. - Dynamic color:
DynamicColorBuildersupplies wallpaper schemes, with?? seedSchemefallback. - Harmonization: blend brand and custom colors toward the dynamic primary.
- Semantic roles: read
colorSchemeroles instead of hard-coded hex so widgets flip automatically. - Mode control:
themeMode: ThemeMode.systemby default, with an optionalValueNotifiertoggle.
Adapt to the user, fall back gracefully, and never hard-code a color you could read from the scheme.
자주 묻는 질문
“동적 색상 및 적응형 라이트·다크 테마” 강의는 무료인가요?
네 — “동적 색상 및 적응형 라이트·다크 테마” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Flutter Mobile Development 강의 전체를 잠금 해제할 수 있습니다. Flutter Mobile Development 강의에는 총 4개의 강의가 포함되어 있습니다.
“동적 색상 및 적응형 라이트·다크 테마”에서 뭘 배우나요?
사용자의 배경화면에 맞춰 색상 팔레트를 조정하고 라이트 모드와 다크 모드 사이를 매끄럽게 전환합니다. 브라우저에서 직접 실행하는 실습 코드로 Flutter Mobile Development을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Flutter Mobile Development을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Flutter Mobile Development은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“동적 색상 및 적응형 라이트·다크 테마” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Flutter Mobile Development 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Flutter Mobile Development 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Material 3 색 구성표 및 시드 색상
- 동적 색상 및 적응형 라이트·다크 테마
- 브랜드 토큰을 위한 사용자 지정 ThemeExtension
- 반응형 타이포그래피 및 구성 요소 테마