我的片段中有三个编写函数.
@Composable
fun MessageView(
action: ApiManagementAction
) {
Column(
modifier = Modifier.padding(15.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Spacer(modifier = Modifier.height(20.dp))
Image(
painter = painterResource(id = R.drawable.ic_api),
contentDescription = null,
)
Spacer(modifier = Modifier.height(20.dp))
Text(
text = stringResource(id = R.string.ABCD),
fontWeight = FontWeight.Bold,
fontSize = 14.sp,
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 5.dp)
)
Spacer(modifier = Modifier.height(10.dp))
Text(
text = stringResource(id = R.string.GFHD),
fontSize = 12.sp,
color = colorResource(id = R.color.text_secondary),
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 10.dp)
)
Spacer(modifier = Modifier.height(30.dp))
CreateApiButton(action = action, showElevation = false)
Spacer(modifier = Modifier.height(10.dp))
GotoDocument(action = action)
}
}
@Composable
fun CreateApiButton(
action: ApiManagementAction,
modifier: Modifier = Modifier,
showElevation: Boolean,
) {
Column(
modifier = modifier.fillMaxWidth(),
verticalArrangement = Arrangement.Center
) {
Box(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 12.dp),
) {
Text(
text = stringResource(id = R.string.title_create_api),
textAlign = TextAlign.Center,
fontSize = 12.sp,
modifier = Modifier
.background(
color = colorResource(id = R.color.color_primary),
shape = RoundedCornerShape(5.dp)
)
.clickable { action.checkGABeforeEditAndCreate.invoke(null) }
.height(45.dp)
.fillMaxWidth()
.wrapContentHeight(align = Alignment.CenterVertically)
.align(Alignment.TopCenter),
color = colorResource(id = R.color.black)
)
}
}
}
@Composable
fun GotoDocument(
action: ApiManagementAction,
) {
Row(
modifier = Modifier
.height(45.dp)
.fillMaxWidth()
.padding(horizontal = 12.dp)
.clickable {
action.gotoDocument.invoke()
}
.border(
width = 1.dp,
shape = RoundedCornerShape(5.dp),
color = colorResource(id = R.color.divider)
),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Icon(
painter = painterResource(id = R.drawable.ic_api_doc),
contentDescription = null,
modifier = Modifier
.height(20.dp)
.width(15.dp)
)
Spacer(modifier = Modifier.width(5.dp))
Text(
text = stringResource(id = R.string.title_api_doc),
textAlign = TextAlign.Center,
fontSize = 12.sp,
modifier = Modifier
.height(45.dp)
.wrapContentHeight(align = Alignment.CenterVertically)
.align(Alignment.CenterVertically),
)
}
}
我运行Compose Metric并获得以下结果:
restartable scheme("[androidx.compose.ui.UiComposable]") fun MessageView(
stable action: ApiManagementAction
unstable <this>: ApiManagementFragment
)
restartable skippable scheme("[androidx.compose.ui.UiComposable]") fun CreateApiButton(
stable action: ApiManagementAction
stable modifier: Modifier? = @static Companion
unused stable showElevation: Boolean
unused unstable <this>: ApiManagementFragment
)
restartable skippable scheme("[androidx.compose.ui.UiComposable]") fun GotoDocument(
stable action: ApiManagementAction
unused unstable <this>: ApiManagementFragment
)
如你所见,MessageView
是not,skippable
,因为它的参数ApiManagementFragment
不稳定.
现在我的问题是:为什么Compose要将我的片段注入到Composed函数中,即使它没有被使用?