当前位置: 首页 > 知识库问答 >
问题:

Jetpack构成了一个底板

傅俊德
2023-03-14

我已经开始学习jetpack compose。我想在单击图标按钮时显示底部工作表。但是我得到了错误@Composable调用只能发生在@Composable函数的上下文中,我如何实现这个逻辑。

这是代码

@RequiresApi(Build.VERSION_CODES.O)
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun AddTaskScreen(navController: NavController) {
    var taskTitle by remember { mutableStateOf("") }
    val currentDate = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(Date())
    var taskDescription by remember { mutableStateOf("") }
    val taskDuration by remember { mutableStateOf(currentDate.toString()) }
    val taskTypes = listOf("Urgent", "Medium", "High")
    var expanded by remember { mutableStateOf(false) }
    var selectedOptionText by remember { mutableStateOf(taskTypes[0]) }
    val clickHandler: () -> Unit = {
       DateBottomSheet()
    }
    Column() {
        AppToolBar(title = "AddTaskScreen") {
            navController.navigateUp()
        }

        OutlinedTextField(
            value = taskTitle,
            label = { Text(text = "Please input task title") },
            onValueChange = { text -> taskTitle = text },
            modifier = textFieldModifier
        )
        OutlinedTextField(
            value = taskDescription,
            label = { Text(text = "Please input task description") },
            onValueChange = { text -> taskDescription = text },
            modifier = textFieldModifier.height(200.dp)
        )

        ExposedDropdownMenuBox(
            expanded = expanded,
            onExpandedChange = {
                expanded = !expanded
            },
            modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp)
        ) {
            OutlinedTextField(
                readOnly = true,
                value = selectedOptionText,
                onValueChange = { },
                label = { Text("Task priority") },
                trailingIcon = {
                    ExposedDropdownMenuDefaults.TrailingIcon(
                        expanded = expanded
                    )
                },
                modifier = Modifier.fillMaxWidth()
            )
            ExposedDropdownMenu(
                expanded = expanded,
                onDismissRequest = {
                    expanded = false
                }
            ) {
                taskTypes.forEach { selectionOption ->
                    DropdownMenuItem(
                        onClick = {
                            selectedOptionText = selectionOption
                            expanded = false
                        }
                    ) {
                        Text(text = selectionOption)
                    }
                }
            }

        }


        //task time textField
        OutlinedTextField(
            value = taskDuration,
            readOnly = true,
            label = { Text(text = "Please select task duration") },
            onValueChange = { text -> taskDescription = text },
            modifier = textFieldModifier,
            trailingIcon = {
                IconButton(
                    onClick = {
                    clickHandler.invoke()
                }) {
                    Icon(Icons.Filled.DateRange, contentDescription = "")
                }
            }
        )
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun DateBottomSheet() {
    val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
        bottomSheetState = rememberBottomSheetState(
            initialValue = BottomSheetValue.Collapsed
        )
    )


    BottomSheetScaffold(
        sheetContent = {
            Column() {
                Text(text = "ThisIsBottomSheet")
                Text(text = "ThisIsBottomSheet")
                Text(text = "ThisIsBottomSheet")
                Text(text = "ThisIsBottomSheet")
                Text(text = "ThisIsBottomSheet")
            }
        },
        scaffoldState = bottomSheetScaffoldState
    ) {
       
    }

但是我得到了错误@Composable调用只能发生在@Composable函数的上下文中,我如何实现这个逻辑

共有1个答案

那绪
2023-03-14

您可以简单地使用mutabelState来处理按钮单击事件,以显示底部工作表。

您可以进行以下更改-

@RequiresApi(Build.VERSION_CODES.O)
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun AddTaskScreen(navController: NavController) {
    var taskTitle by remember { mutableStateOf("") }
    val currentDate = SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(Date())
    var taskDescription by remember { mutableStateOf("") }
    val taskDuration by remember { mutableStateOf(currentDate.toString()) }
    val taskTypes = listOf("Urgent", "Medium", "High")
    var expanded by remember { mutableStateOf(false) }
    var selectedOptionText by remember { mutableStateOf(taskTypes[0]) }
    
    var openBottomSheet by rememberSaveable { mutableStateOf(false) }

    Column() {
        AppToolBar(title = "AddTaskScreen") {
            navController.navigateUp()
        }

        OutlinedTextField(
            value = taskTitle,
            label = { Text(text = "Please input task title") },
            onValueChange = { text -> taskTitle = text },
            modifier = textFieldModifier
        )
        OutlinedTextField(
            value = taskDescription,
            label = { Text(text = "Please input task description") },
            onValueChange = { text -> taskDescription = text },
            modifier = textFieldModifier.height(200.dp)
        )

        ExposedDropdownMenuBox(
            expanded = expanded,
            onExpandedChange = {
                expanded = !expanded
            },
            modifier = Modifier
                .fillMaxWidth()
                .padding(20.dp)
        ) {
            OutlinedTextField(
                readOnly = true,
                value = selectedOptionText,
                onValueChange = { },
                label = { Text("Task priority") },
                trailingIcon = {
                    ExposedDropdownMenuDefaults.TrailingIcon(
                        expanded = expanded
                    )
                },
                modifier = Modifier.fillMaxWidth()
            )
            ExposedDropdownMenu(
                expanded = expanded,
                onDismissRequest = {
                    expanded = false
                }
            ) {
                taskTypes.forEach { selectionOption ->
                    DropdownMenuItem(
                        onClick = {
                            selectedOptionText = selectionOption
                            expanded = false
                        }
                    ) {
                        Text(text = selectionOption)
                    }
                }
            }

        }


        //task time textField
        OutlinedTextField(
            value = taskDuration,
            readOnly = true,
            label = { Text(text = "Please select task duration") },
            onValueChange = { text -> taskDescription = text },
            modifier = textFieldModifier,
            trailingIcon = {
                IconButton(
                    onClick = {
                    openBottomSheet = true
                }) {
                    Icon(Icons.Filled.DateRange, contentDescription = "")
                }
            }
        )
        if (openBottomSheet) {
            DateBottomSheet()
            
        }
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun DateBottomSheet() {
    val bottomSheetScaffoldState = rememberBottomSheetScaffoldState(
        bottomSheetState = rememberBottomSheetState(
            initialValue = BottomSheetValue.Collapsed
        )
    )


    BottomSheetScaffold(
        sheetContent = {
            Column() {
                Text(text = "ThisIsBottomSheet")
                Text(text = "ThisIsBottomSheet")
                Text(text = "ThisIsBottomSheet")
                Text(text = "ThisIsBottomSheet")
                Text(text = "ThisIsBottomSheet")
            }
        },
        scaffoldState = bottomSheetScaffoldState
    ) {
       
    }
 类似资料:
  • 我正在尝试使用Compose设计一个布局,其中包括: 顶部应用栏 正文(内容) 底部应用栏 单击时表示菜单的底页(模式底页) -------顶应用栏------- -主要内容- ------底部压条----- ----模型底板--- Compose提供了3个组件: 脚手架 底板脚手架 模态底板布局 脚手架没有底板属性 BottomSheetScaffold没有BottomAppBar属性 Moda

  • 我试图确保有一个视觉指示,表明用户正在尝试滚动到底部,即使他已经到达了LazyColumn中列表的末尾。 默认情况下,此功能在XML布局中可用。 我们如何在Jetpack compose中实现这一点?对于顶部的过度滚动,我看到有一个“滑动刷新”等效项。目前有解决方案吗?

  • 我使用< code>BottomSheetScaffold来设置一个带有< code>topBar和一些< code>sheetContent的视图。 当底部工作表完全展开时,它将与 (也称为操作栏或应用栏)重叠。有没有办法确保它不会重叠?

  • 我尝试过通过堆叠< code > circular progression indicator 来使用方框布局,但是需要硬编码圆的大小。我希望戒指是大小不可知的。我如何使用Compose实现这一点?

  • 我正在使用Jetpack Compose,并尝试在用户单击中的登录按钮时使登录屏幕覆盖整个屏幕。 我使用的是和,因此我可以拥有和

  • 在Jetpack compose 1.0.0-beta01中,我这样调用BottomSheetScaffold: ...并获得以下错误: java.lang.IllegalArgumentException:初始值必须具有关联的锚点。 有什么解决办法吗?