サンプルコード

スポンサーリンク
data class Section1State(
    val selectedDropdownItem: String? = null,
    val textFieldValue: String = ""
)

data class Section2State(
    val textFieldValues: List<String> = listOf("", "", ""),
    val numberInputFields: List<String> = listOf("", "", "")
)

data class Section3State(
    val textFieldValues: List<String> = listOf("", ""),
    val numberInputFields: List<String> = listOf("", "", ""),
    val selectedDropdownItem: String? = null
)

data class Section4State(
    val selectedDropdownItem: String? = null,
    val radioButtonSelections: List<String?> = listOf(null, null, null) // 3つのラジオボタン
)

data class UiState(
    val section1: Section1State = Section1State(),
    val section2: Section2State = Section2State(),
    val section3: Section3State = Section3State(),
    val section4: Section4State = Section4State(),
    val isButtonEnabled: Boolean = false
)
class MainViewModel : ViewModel() {
    private val _section1State = MutableStateFlow(Section1State())
    val section1State: StateFlow<Section1State> = _section1State

    private val _section2State = MutableStateFlow(Section2State())
    val section2State: StateFlow<Section2State> = _section2State

    private val _section3State = MutableStateFlow(Section3State())
    val section3State: StateFlow<Section3State> = _section3State

    private val _section4State = MutableStateFlow(Section4State())
    val section4State: StateFlow<Section4State> = _section4State

    private val _isButtonEnabled = MutableStateFlow(false)
    val isButtonEnabled: StateFlow<Boolean> = _isButtonEnabled

    fun updateSection1(dropdownItem: String?, textValue: String) {
        val shouldShowSection2 = dropdownItem == "選択肢A"
        val shouldShowSection3 = dropdownItem == "選択肢B"

        _section1State.update {
            it.copy(
                selectedDropdownItem = dropdownItem,
                textFieldValue = textValue,
                shouldShowSection2 = shouldShowSection2,
                shouldShowSection3 = shouldShowSection3
            )
        }
        updateButtonState()
    }

    private fun updateButtonState() {
        val isValid = section1State.value.textFieldValue.isNotBlank() &&
                      section2State.value.textFieldValues.all { it.isNotBlank() } &&
                      section2State.value.numberInputFields.all { it.isNotBlank() } &&
                      section3State.value.textFieldValues.all { it.isNotBlank() } &&
                      section3State.value.numberInputFields.all { it.isNotBlank() } &&
                      section4State.value.radioButtonSelections.all { it != null }

        _isButtonEnabled.value = isValid
    }
}
@Composable
fun MainScreen(viewModel: MainViewModel = viewModel()) {
    val section1State by viewModel.section1State.collectAsState()
    val section2State by viewModel.section2State.collectAsState()
    val section3State by viewModel.section3State.collectAsState()
    val section4State by viewModel.section4State.collectAsState()
    val isButtonEnabled by viewModel.isButtonEnabled.collectAsState()

    Column {
        Section1(
            selectedDropdownItem = section1State.selectedDropdownItem,
            textValue = section1State.textFieldValue,
            onUpdate = viewModel::updateSection1
        )

        if (section1State.shouldShowSection2) {
            Section2(
                textFieldValues = section2State.textFieldValues,
                numberInputValues = section2State.numberInputFields,
                onUpdate = viewModel::updateSection2
            )
        }

        if (section1State.shouldShowSection3) {
            Section3(
                textFieldValues = section3State.textFieldValues,
                numberInputValues = section3State.numberInputFields,
                selectedDropdownItem = section3State.selectedDropdownItem,
                onUpdate = viewModel::updateSection3
            )
        }

        Section4(
            selectedDropdownItem = section4State.selectedDropdownItem,
            radioSelections = section4State.radioButtonSelections,
            onUpdate = viewModel::updateSection4
        )

        Button(
            onClick = { /* ボタンの処理 */ },
            enabled = isButtonEnabled
        ) {
            Text("送信")
        }
    }
}
@Composable
fun Section1(
    selectedDropdownItem: String?,
    textValue: String,
    onUpdate: (String?, String) -> Unit
) {
    var localDropdownItem by remember { mutableStateOf(selectedDropdownItem) }
    var localTextValue by remember { mutableStateOf(textValue) }

    Column {
        // ドロップダウンメニュー
        DropdownMenuComponent(
            selectedItem = localDropdownItem,
            onItemSelected = { newValue ->
                localDropdownItem = newValue
                onUpdate(localDropdownItem, localTextValue)
            }
        )

        Spacer(modifier = Modifier.height(8.dp))

        // テキストフィールド
        TextField(
            value = localTextValue,
            onValueChange = { newValue ->
                localTextValue = newValue
                onUpdate(localDropdownItem, localTextValue)
            },
            label = { Text("入力してください") },
            modifier = Modifier.fillMaxWidth()
        )
    }
}
Section1(
    selectedDropdownItem = section1State.selectedDropdownItem,
    textValue = section1State.textFieldValue,
    onUpdate = viewModel::updateSection1
)

        val isEnabled = it.selectedOption != null &&
                        it.selectedDropdownOption != null &&
                        it.amount.isNotBlank() &&
                        it.numList.all { num -> num.isNotBlank() }

        Log.d("DEBUG", "selectedOption: ${it.selectedOption}")
        Log.d("DEBUG", "selectedDropdownOption: ${it.selectedDropdownOption}")
        Log.d("DEBUG", "amount: ${it.amount}")
        Log.d("DEBUG", "numList: ${it.numList}")
        Log.d("DEBUG", "isButtonEnabled: $isEnabled")

        it.copy(isButtonEnabled = isEnabled)

コメント

タイトルとURLをコピーしました