I've been trying to set this button to the bottom left of the screen. Can anyone find me a better way or suggest a fix to this error?

huangapple go评论68阅读模式
英文:

I've been trying to set this button to the bottom left of the screen. Can anyone find me a better way or suggest a fix to this error?

问题

@Composable
fun LoginForm() {
    var username by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }

    // 存储底部表单的状态
    var bottomSheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)

    val coroutineScope = rememberCoroutineScope()

    Column(
        modifier = Modifier.fillMaxSize(),
    ) {
        Box(modifier = Modifier.weight(1f)) {
            // weight修饰符用于扩展Box的高度,以填充Column的剩余高度
            // 这是为了将按钮推到屏幕底部
        }

        Box(
            modifier = Modifier
                .fillMaxSize()
                .align(Alignment.BottomStart) // 将按钮放置在屏幕底部左侧
        ) {
            Button(
                onClick = {
                    coroutineScope.launch { bottomSheetState.show() }
                },
                modifier = Modifier.padding(16.dp)
            ) {
                Text("登录")
            }
        }

        // 定义底部表单内容
        ModalBottomSheetLayout(
            sheetContent = {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp)
                ) {
                    OutlinedTextField(
                        value = username,
                        onValueChange = { username = it },
                        label = { Text("用户名") },
                        modifier = Modifier.fillMaxWidth()
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    OutlinedTextField(
                        value = password,
                        onValueChange = { password = it },
                        label = { Text("密码") },
                        modifier = Modifier.fillMaxWidth()
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    Button(
                        onClick = { coroutineScope.launch { bottomSheetState.hide() } },
                        modifier = Modifier.align(Alignment.End)
                    ) {
                        Text("登录")
                    }
                }
            },
            sheetState = bottomSheetState,
            sheetShape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp)
        ) {
            // 这个组合是底部表单打开时可见的“背景”
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.BottomCenter
            ) {
                // 在这里添加任何您想要在背景中显示的内容
            }
        }
    }
}

这是一个用于应用程序的登录界面。

您遇到的错误是“Type mismatch: inferred type is Alignment but Alignment.Horizontal was expected”(类型不匹配:推断的类型是Alignment,但期望的是Alignment.Horizontal)。您在Box布局中使用了Alignment.BottomEnd,但您想要将按钮放在屏幕的左下角。要实现这一目标,您可以将Alignment.BottomEnd更改为Alignment.BottomStart

英文:
@Composable
fun LoginForm() {
    var username by remember { mutableStateOf("") }
    var password by remember { mutableStateOf("") }

    // Store the state of the bottom sheet
    var bottomSheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)

    val coroutineScope = rememberCoroutineScope()

    Column(
        modifier = Modifier.fillMaxSize(),
    ) {
        Box(modifier = Modifier.weight(1f)) {
            // The weight modifier is added to expand the height of the Box to fill the remaining height of the Column
            // This is necessary to push the buttons to the bottom of the screen
        }

        Box(  modifier = Modifier
            .fillMaxSize()
            .align(Alignment.BottomEnd)
        )

         {
            Button(
                onClick = {
                    coroutineScope.launch { bottomSheetState.show() }
                },
                modifier = Modifier.padding(16.dp)
            ) {
                Text("Login")
            }
        }

    // Define the bottom sheet content
        ModalBottomSheetLayout(
            sheetContent = {
                Column(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp)
                ) {
                    OutlinedTextField(
                        value = username,
                        onValueChange = { username = it },
                        label = { Text("Username") },
                        modifier = Modifier.fillMaxWidth()
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    OutlinedTextField(
                        value = password,
                        onValueChange = { password = it },
                        label = { Text("Password") },
                        modifier = Modifier.fillMaxWidth()
                    )
                    Spacer(modifier = Modifier.height(16.dp))
                    Button(
                        onClick = { coroutineScope.launch { bottomSheetState.hide() } },
                        modifier = Modifier.align(Alignment.End)
                    ) {
                        Text("Login")
                    }
                }
            },
            sheetState = bottomSheetState,
            sheetShape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp)
        ) {
            // This composable is the "background" that will be visible when the bottom sheet is open
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.BottomCenter
            ) {
                // Add any content you want to show in the background here
            }
        }


    }


}

This is for a login screen for an app.

This is the error I have been getting : "Type mismatch: inferred type is Alignment but Alignment.Horizontal was expected"

I have been changing the Alignment.BottomEnd in the box layout for the Login button but nothing works.
I wanted to set the button to the bottom left end of the screen.

答案1

得分: 0

ColumnScope 中,align 修饰符需要一个 Alignment.Horizontal 参数。

在你的情况下,只需使用:

Box(  
   modifier = Modifier
      .fillMaxWidth()
      .background(Yellow), //不需要这一行
   contentAlignment = Alignment.BottomStart,
)

否则,你可以避免使用第一个 Box,使用:

Column(
    modifier = Modifier.fillMaxSize(),
    verticalArrangement = Arrangement.Bottom
) {

    Box(  
        modifier = Modifier
            .fillMaxWidth()
    )

}

I've been trying to set this button to the bottom left of the screen. Can anyone find me a better way or suggest a fix to this error?

英文:

In the ColumnScope the align modifier requires a Alignment.Horizontal parameter.

In you case just use:

Box(  
   modifier = Modifier
      .fillMaxWidth()
      .background(Yellow), //it is not neeeded
   contentAlignment = Alignment.BottomStart,
)

Otherwise you can avoid the 1st Box using:

Column(
    modifier = Modifier.fillMaxSize(),
    verticalArrangement =  Arrangement.Bottom
) {

    Box(  modifier = Modifier
        .fillMaxWidth()
    )

}

I've been trying to set this button to the bottom left of the screen. Can anyone find me a better way or suggest a fix to this error?

huangapple
  • 本文由 发表于 2023年2月18日 21:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定