dark mode-v10

This commit is contained in:
Eric85710 2026-04-24 16:38:47 +08:00
parent 57bc9b9895
commit 36233da198
3 changed files with 16 additions and 7 deletions

View file

@ -5,18 +5,20 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.compose.runtime.collectAsState
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import com.example.login_v3.auth.HealthCheckScreen import com.example.login_v3.auth.HealthCheckScreen
import com.example.login_v3.home.setting.setting_detail_page.viewmodel.Theme_ViewModel import com.example.login_v3.home.setting.setting_detail_page.viewmodel.Theme_ViewModel
import com.example.login_v3.ui.theme.AppTheme import com.example.login_v3.ui.theme.AppTheme
import com.example.login_v3.ui.theme.Login_V3Theme import com.example.login_v3.ui.theme.Login_V3Theme
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint @AndroidEntryPoint
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
//color mode viewmodel private val themeViewModel: Theme_ViewModel by viewModels()
private val themeViewModel by viewModels<Theme_ViewModel>()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -24,7 +26,9 @@ class MainActivity : ComponentActivity() {
installSplashScreen() installSplashScreen()
setContent { setContent {
Login_V3Theme(appTheme = themeViewModel.currentTheme) { val currentAppTheme by themeViewModel.currentTheme.collectAsState()
Login_V3Theme(appTheme = currentAppTheme) {
Technologia() Technologia()
} }
} }

View file

@ -11,6 +11,8 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -22,7 +24,7 @@ fun setting_theme_page(
viewModel: Theme_ViewModel viewModel: Theme_ViewModel
){ ){
// 直接讀取,不需要 .value // 直接讀取,不需要 .value
val selectedTheme = viewModel.currentTheme val selectedTheme by viewModel.currentTheme.collectAsState()
Column(modifier = Modifier.padding(16.dp)) { Column(modifier = Modifier.padding(16.dp)) {
Text( Text(

View file

@ -5,12 +5,15 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.example.login_v3.ui.theme.AppTheme import com.example.login_v3.ui.theme.AppTheme
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
class Theme_ViewModel : ViewModel() { class Theme_ViewModel : ViewModel() {
var currentTheme by mutableStateOf(AppTheme.SYSTEM) private val _currentTheme = MutableStateFlow(AppTheme.DARK)
private set val currentTheme: StateFlow<AppTheme> = _currentTheme.asStateFlow()
fun updateTheme(newTheme: AppTheme) { fun updateTheme(newTheme: AppTheme) {
currentTheme = newTheme _currentTheme.value = newTheme
} }
} }