Penyesuaian Warna ketika Tema Terang dan Penambahan Fitur Validasi Input

This commit is contained in:
202310715297 RAIHAN ARIQ MUZAKKI 2025-11-07 14:12:33 +07:00
parent 010dee7e6b
commit 637c6089b6
2 changed files with 356 additions and 286 deletions

View File

@ -95,6 +95,7 @@ fun BMICalculatorLayout() {
var heightInput by remember { mutableStateOf("") } var heightInput by remember { mutableStateOf("") }
var weightInput by remember { mutableStateOf("") } var weightInput by remember { mutableStateOf("") }
var unitUSC by remember { mutableStateOf(false) } var unitUSC by remember { mutableStateOf(false) }
var errorMessage by remember { mutableStateOf("") }
// State baru untuk mengontrol kapan output ditampilkan // State baru untuk mengontrol kapan output ditampilkan
var showResult by remember { mutableStateOf(false) } var showResult by remember { mutableStateOf(false) }
@ -104,6 +105,7 @@ fun BMICalculatorLayout() {
heightInput = "" heightInput = ""
weightInput = "" weightInput = ""
showResult = false showResult = false
errorMessage = ""
} }
val bmiHeight = heightInput.toDoubleOrNull() ?: 0.0 val bmiHeight = heightInput.toDoubleOrNull() ?: 0.0
@ -179,6 +181,57 @@ fun BMICalculatorLayout() {
Button( Button(
onClick = { onClick = {
// Validasi angka
if (bmiHeight == 0.0 || bmiWeight == 0.0) {
errorMessage = "Input harus berupa angka yang valid."
showResult = false
return@Button
}
// Validasi untuk SI Units
if (!unitUSC) {
if (bmiHeight !in 50.0..300.0 && bmiWeight !in 20.0..500.0) {
errorMessage =
"Masukkan Tinggi Badan pada rentang 50-300 cm \ndan\n Berat Badan pada rentang 20-500 kg"
showResult = false
return@Button
}
else if (bmiHeight !in 50.0..300.0) {
errorMessage = "Tinggi harus berada pada rentang 50300 cm."
showResult = false
return@Button
}
else if (bmiWeight !in 20.0..500.0) {
errorMessage = "Berat harus berada pada rentang 20500 kg."
showResult = false
return@Button
}
}
// Validasi untuk USC Units
if (unitUSC) {
if (bmiHeight !in 20.0..120.0 && bmiWeight !in 44.0..1100.0){
errorMessage =
"Masukkan Tinggi Badan pada rentang 20-120 in dan\n Berat Badan pada rentang 44-110 kg"
showResult = false
return@Button
}
else if (bmiHeight !in 20.0..120.0) {
errorMessage = "Tinggi harus berada pada rentang 20120 in."
showResult = false
return@Button
}
else if (bmiWeight !in 44.0..1100.0) {
errorMessage = "Berat harus berada pada rentang 441100 lbs."
showResult = false
return@Button
}
}
// Jika valid
errorMessage = ""
showResult = true showResult = true
}, },
modifier = Modifier.weight(1f) modifier = Modifier.weight(1f)
@ -193,6 +246,7 @@ fun BMICalculatorLayout() {
heightInput = "" heightInput = ""
weightInput = "" weightInput = ""
showResult = false showResult = false
errorMessage = ""
}, },
modifier = Modifier.weight(1f) modifier = Modifier.weight(1f)
) { ) {
@ -202,6 +256,22 @@ fun BMICalculatorLayout() {
Spacer(modifier = Modifier.height(32.dp)) Spacer(modifier = Modifier.height(32.dp))
// Pesan Error
if (errorMessage.isNotEmpty()) {
Text(
text = errorMessage,
color = MaterialTheme.colorScheme.error,
fontWeight = FontWeight.SemiBold,
modifier = Modifier
.padding(top = 16.dp)
.fillMaxWidth(),
textAlign = TextAlign.Center
)
}
Spacer(modifier = Modifier.height(32.dp))
// Output BMI
if (showResult) { if (showResult) {
Text( Text(
text = "BMI: $bmi", text = "BMI: $bmi",

View File

@ -17,8 +17,8 @@ package com.example.bmicalculator.ui.theme
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
val md_theme_light_primary = Color(0xFFE3E2DE) val md_theme_light_primary = Color(0xFF1351AA) // Button
val md_theme_light_onPrimary = Color(0xFF1351AA) val md_theme_light_onPrimary = Color(0xFFE3E2DE)
val md_theme_light_primaryContainer = Color(0xFFE3E2DE) val md_theme_light_primaryContainer = Color(0xFFE3E2DE)
val md_theme_light_onPrimaryContainer = Color(0xFF1351AA) val md_theme_light_onPrimaryContainer = Color(0xFF1351AA)
val md_theme_light_secondary = Color(0xFFE3E2DE) val md_theme_light_secondary = Color(0xFFE3E2DE)