Android Studio 更改组件中的可变状态值

qoefvg9y  于 9个月前  发布在  Android
关注(0)|答案(1)|浏览(93)

我有一个组件(滑块)与浮动值,并希望改变它的价值与加号和地雷按钮在另一个组件(changeSliderValue),我想:
1.仅在选择slider组件时创建changeSliderValue,并在changeSliderValue中的floatValue更改时更改slider的值。
1.当它被创建的时候,在右下角固定changeSliderValue UI组件的位置。
我的主要组成部分是:

@composable
fun slider(text: String, value: Float) {

    var value: Float by remember {
        mutableStateOf(value)
    }
    var selected: Boolean by remember {
        mutableStateOf(false)
    }
    val backgroundColor: Color = when (selected) {
        true -> Color(24, 45, 122)
        false -> Color.Transparent
    }
    val fontColor: Color = when (selected) {
        true -> Color.White
        false -> Color(143, 143, 143)
    }
    value = when (selected){
       true -> changeSliderValue(value = value)
        false -> value
    }
    Box(
        modifier = Modifier
            .width(108.dp)
            .height(60.dp)
            .background(backgroundColor, shape = RoundedCornerShape(16))
            .padding(5.dp),
    ) {
        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center
        ) {
            CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
                Text(text = text)
            }
            Text(
                text = value.toString(),
                fontFamily = FontFamily(Font(R.font.sevensegment)),
                color = fontColor,
                fontSize = 20.sp
            )

        }
    }

}

我的第二个组成部分是:

@Composable
fun changeSliderValue(value: Float): Float {

    var floatValue:Float by remember{
        mutableStateOf(value)
    }

    Box(
        modifier = Modifier
            .width(227.dp)
            .height(58.dp)
            .background(color = Color(19, 36, 98), shape = RoundedCornerShape(20))
    ) {
        Row(
            modifier = Modifier.fillMaxSize(),
            verticalAlignment =Alignment.CenterVertically ,
            horizontalArrangement = Arrangement.spacedBy(17.dp , alignment = Alignment.CenterHorizontally)
        ) {
            Box(
                Modifier
                    .background(Color.White, shape = RoundedCornerShape(20))
                    .width(48.dp)
                    .height(48.dp)
                    .clickable {
                        floatValue -= 0.1F
                    },
                contentAlignment = Alignment.Center

            ) {
                Icon(
                    painter = painterResource(id = R.drawable.minus),
                    contentDescription = null,

                    tint = Color(19, 36, 98)
                )
            }
            CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
                Text(
                    text = "افزایش/کاهش",
                    color = Color.White
                )
            }
            Box(
                Modifier
                    .background(Color.White, shape = RoundedCornerShape(20))
                    .width(48.dp)
                    .height(48.dp)
                    .clickable { floatValue += 0.1F },
                contentAlignment = Alignment.Center

            ) {
                Icon(
                    painter = painterResource(id = R.drawable.plus),
                    contentDescription = null,

                    tint = Color(19, 36, 98)
                )
            }

        }
    }

}
cidc1ykv

cidc1ykv1#

Jetpack Compose遵循单向数据流(UDF)模式。这意味着数据会沿着Composable层次结构向下流动,并且更改/事件会使用回调传递给父Composable。
我不完全确定你想达到什么目的,但我认为有一些预定义的Composables可以使用。特别是看看滑块组合和OutlinedButton组合。
还可以看看作为Jetpack Compose框架一部分的Icons library。它允许您使用各种图标,而无需手动将其复制到项目中。您可以通过将以下依赖项添加到模块的build.gradle来获取它:

implementation "androidx.compose.material:material-icons-extended:$compose_version"

解决方案可能如下所示:

@Composable
fun SliderComposable() {

    var value by rememberSaveable { mutableStateOf(50f) }
    
    Row(
        modifier = Modifier.fillMaxWidth(),
        horizontalArrangement = Arrangement.SpaceEvenly
    ) {
        OutlinedButton(onClick = { value--; value.coerceIn(0f, 100f) },
            modifier = Modifier.size(50.dp),
            enabled = value != 0, 
            shape = CircleShape,
            contentPadding = PaddingValues(0.dp)
        ) {
            Icon(Icons.Default.Minus, contentDescription = "content description")
        }
        
        // Use the predefined Jetpack Compose Slider Composable
        Slider(
            modifier = Modifier.weight(1f),
            enabled = true,
            value = value,
            onValueChange = { value = it },
            valueRange = 0f..100f,
            steps = 1
        )
        
        OutlinedButton(onClick = { value++; value.coerceIn(0f, 100f) },
            modifier= Modifier.size(50.dp),
            enabled = value != 100,
            shape = CircleShape,
            contentPadding = PaddingValues(0.dp)
        ) {
            Icon(Icons.Default.Plus, contentDescription = "content description")
        }
    }
}

这将产生一个范围从0到100的滑块,其左侧有一个减号按钮,右侧有一个加号按钮。拖动滑块会更新值,按下按钮会更新滑块中显示的值。我希望这足以让你开始。

相关问题