Home > Software engineering >  Jetpack compose: scrolling in dialog gives strange effects
Jetpack compose: scrolling in dialog gives strange effects

Time:10-23

I'm trying to create a dialog where you can pick a number. I use FlowRow from Accompanist. But it gives strange results when scrolling. I guess that I'm missing something here.

The code:

@Composable
fun AlertDialogErrorTest() {
    var showDlg by remember { mutableStateOf(false)}
    val scrollState = rememberScrollState()
    if (showDlg) {
        AlertDialog(
            onDismissRequest = { showDlg = false },
            title = { Text(text = "Pick something from the list") },
            text = {
                   FlowRow(modifier = Modifier.verticalScroll(scrollState)) {
                       for (i in 1..100) {
                           Box (modifier = Modifier.size(48.dp).padding(8.dp).background(Color.LightGray), contentAlignment = Alignment.Center) {
                               Text (i.toString())
                           }
                       }
                   }
            },
            confirmButton = {
                TextButton(onClick = { showDlg = false }) {
                        Text("Done")
                    }
            },
            dismissButton = {
                TextButton(onClick = { showDlg = false }) {
                        Text("Cancel")
                    }
            }
        )
    }

    Button(onClick = {showDlg = true}) {
        Text("Show dialog")
    }
}

The result when scrolling:

CodePudding user response:

This is because AlertDialog does not currently support scrollable content. If you think this is a bug, you can report it in the Compose issue tracker.

Meanwhile, you can use a plain Dialog, which lays under the AlertDialog.

Dialog(
    onDismissRequest = { showDlg = false },
    content = {
        Column(Modifier.background(Color.White)) {
            Text(
                text = "Pick something from the list",
                style = MaterialTheme.typography.subtitle1,
                modifier = Modifier.align(Alignment.CenterHorizontally)
            )
            FlowRow(
                modifier = Modifier
                    .verticalScroll(scrollState)
                    .weight(1f)
            ) {
                for (i in 1..100) {
                    Box(
                        modifier = Modifier
                            .size(48.dp)
                            .padding(8.dp)
                            .background(Color.LightGray),
                        contentAlignment = Alignment.Center
                    ) {
                        Text(i.toString())
                    }
                }
            }
            FlowRow(
                mainAxisSpacing = 8.dp,
                crossAxisSpacing = 12.dp,
                modifier = Modifier
                    .align(Alignment.End)
                    .padding(horizontal = 8.dp, vertical = 2.dp)
            ) {
                TextButton(onClick = { showDlg = false }) {
                    Text("Cancel")
                }
                TextButton(onClick = { showDlg = false }) {
                    Text("Done")
                }
            }
        }
    },
)

Result:

  • Related