JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
15.26k stars 1.11k forks source link

ModalBottomSheet Closes on Back Press with Keyboard Open in Compose Multiplatform #5015

Closed Jenefa closed 5 days ago

Jenefa commented 5 days ago

Description: When using the ModalBottomSheet from the material3 package in Jetpack Compose, the bottom sheet unexpectedly closes when the back button is pressed while the keyboard is open. This issue should be addressed to ensure that the ModalBottomSheet remains open in such cases.

Steps to Reproduce:

Expected Behavior: The ModalBottomSheet should remain open when the back button is pressed if the keyboard is open. Only the keyboard should be closed.

Actual Behavior: The ModalBottomSheet closes when the back button is pressed with the keyboard open.

Sample Code

import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.displayCutoutPadding import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.statusBarsPadding import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material3.Button import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ModalBottomSheet import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Text import androidx.compose.material3.TextFieldDefaults import androidx.compose.material3.rememberModalBottomSheetState import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.unit.dp import kotlinx.coroutines.launch

object TestScreen {

// Constants
const val route = "/test"
const val title = "Test"

// Public Methods
@Composable
fun Content() {
    Main()
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Main(modifier: Modifier = Modifier) {
    var openBottomSheet by remember { mutableStateOf(false) }
    val scope = rememberCoroutineScope()
    val sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)

    if (openBottomSheet) {
        ModalBottomSheet(
            sheetState = sheetState,
            modifier = Modifier.statusBarsPadding()
                .displayCutoutPadding(),
            shape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp),
            dragHandle = null,
            windowInsets = WindowInsets(0.dp),
            onDismissRequest = {
                openBottomSheet = false
            },
        ) {
            var searchText by remember { mutableStateOf("") }

            OutlinedTextField(
                value = searchText,
                onValueChange = { searchText = it },
                modifier = Modifier.padding(16.dp),
                placeholder = { Text(text = "Placeholder", color = Color.Gray) },
                colors = TextFieldDefaults.outlinedTextFieldColors(
                    containerColor = Color.White,
                    focusedBorderColor = Color.Blue,
                    unfocusedBorderColor = Color.Gray
                ),
                singleLine = true,
                shape = RoundedCornerShape(4.dp)
            )
            Spacer(modifier = Modifier.height(160.dp))
        }
    }

    Button(
        onClick = {
            scope.launch {
                openBottomSheet = true
            }
        },
        modifier = modifier
    ) {
        Text(text = "Click Me!")
    }
}

} ` Attachment ModalBottomSheetIssue.webm

kropp commented 5 days ago

It looks like it is an Android-specific bug, please report it to Google: https://issuetracker.google.com/issues?q=jetpack%20compose