WindowCompat.getInsetsController() で取得した WindowInsetsControllerCompat の setAppearanceLightStatusBars() と setAppearanceLightNavigationBars() を使うことで、status bar と navigation bar の light mode(light mode だとアイコンがグレーになり、dark だと白になる)をコードから切り替えることができます。
このようにアプリ用の MaterialTheme のところで SideEffect を使って切り替え処理をすると、Theme の xml で頑張らなくて良くなるので便利です。
- @Composable
- fun MyAppTheme(
- darkTheme: Boolean = isSystemInDarkTheme(),
- content: @Composable () -> Unit
- ) {
- val view = LocalView.current
- val context = LocalContext.current
- SideEffect {
- val controller = WindowCompat.getInsetsController(context.findActivity().window, view)
- controller?.isAppearanceLightStatusBars = !darkTheme
- controller?.isAppearanceLightNavigationBars = !darkTheme
- }
- MaterialTheme(
- colors = if (!darkTheme) LightColorPalette else DarkColorPalette,,
- typography = Typography,
- shapes = Shapes,
- content = content
- )
- }
- private tailrec fun Context.findActivity(): Activity =
- when (this) {
- is Activity -> this
- is ContextWrapper -> this.baseContext.findActivity()
- else -> throw IllegalArgumentException("Could not find activity!")
- }
0 件のコメント:
コメントを投稿