Compose ModalBottomSheetLayout

预览

在这里插入图片描述

依赖
implementation "androidx.compose.material:material:1.5.0-alpha01" // 请根据你的 Compose 版本调整
代码
@OptIn(ExperimentalMaterialApi::class)
@Composable
@Preview
fun TestBottomDialog() {
    val scope = rememberCoroutineScope()
    val sheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
    IconButton(onClick = {
        scope.launch {
            sheetState.show()
        }
    }) {
        Icon(Icons.Default.Add, contentDescription = null)
    }
    BottomDialog(sheetState)
}

@Composable
@Preview
fun PreviewBottomDialog() {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(
                color = Color.White,
                shape = RoundedCornerShape(topStart = 8.dp, topEnd = 8.dp)
            )
            .padding(20.dp), horizontalArrangement = Arrangement.Center
    ) {
        Spacer(modifier = Modifier.weight(1f))
        LabelButton(painterResource(R.mipmap.ic_wx), "微信", onClick = {})
        Spacer(modifier = Modifier.weight(1f))
        LabelButton(painterResource(R.mipmap.ic_qq), "QQ", onClick = {})
        Spacer(modifier = Modifier.weight(1f))
        LabelButton(painterResource(R.mipmap.ic_wb), "微博", onClick = {})
        Spacer(modifier = Modifier.weight(1f))
    }
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun BottomDialog(sheetState: ModalBottomSheetState) {
    ModalBottomSheetLayout(sheetContent = {
        Row(
            modifier = Modifier
                .width(LocalConfiguration.current.screenWidthDp.dp)
                .background(
                    color = Color.White,
                    shape = RoundedCornerShape(topStart = 15.dp, topEnd = 15.dp)
                )
                .padding(20.dp), horizontalArrangement = Arrangement.Center
        ) {
            Spacer(modifier = Modifier.weight(1f))
            LabelButton(painterResource(R.mipmap.ic_wx), "微信", onClick = {})
            Spacer(modifier = Modifier.weight(1f))
            LabelButton(painterResource(R.mipmap.ic_qq), "QQ", onClick = {})
            Spacer(modifier = Modifier.weight(1f))
            LabelButton(painterResource(R.mipmap.ic_wb), "微博", onClick = {})
            Spacer(modifier = Modifier.weight(1f))
        }
    }, sheetState = sheetState, sheetBackgroundColor = Color.Transparent) { }
}

@Composable
fun LabelButton(icon: Painter, label: String, onClick: () -> Unit) {
    Button(
        onClick,
        contentPadding = PaddingValues(0.dp),
        colors = ButtonDefaults.buttonColors(containerColor = Color.Transparent)
    ) {
        Column(modifier = Modifier, horizontalAlignment = Alignment.CenterHorizontally) {
            Image(painter = icon, contentDescription = null)
            Text(
                modifier = Modifier.padding(top = 8.dp),
                textAlign = TextAlign.Center,
                text = label,
                fontSize = TextUnit(18f, TextUnitType.Sp),
                color = Color.Black
            )
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值