A question, maybe a little basic since I am in an early learning phase. If I want to change the image of a composable when calling it, how would I do it? I know that this is possible with Int and Strings thanks to the parameters that I define in the original function, but how would it be done in the case of images?
@Composable
fun ComposableTest(exampleText:String){
Column() {
Image (
painter = painterResource(R.drawable.sample_image),
contentDescription = null,
Modifier.size(200.dp))
//Omit the text code, it's a filler
Text("exampleText",
Modifier
.background(Color.White)
.width(200.dp)
.padding(vertical = 30.dp), textAlign = TextAlign.Center)
}
}
@Preview
@Composable
fun CalledTest (){
ComposableTest(text = "Composable Text")
}
CodePudding user response:
please try:
@Composable
fun ComposableTest(exampleText:String,@DrawableRes img: Int = R.drawable.sample_image){
Column() {
Image (
painter = painterResource(img),
contentDescription = null,
Modifier.size(200.dp))
//Omit the text code, it's a filler
Text("exampleText",
Modifier
.background(Color.White)
.width(200.dp)
.padding(vertical = 30.dp), textAlign = TextAlign.Center)
}
}
@Preview
@Composable
fun CalledTest (){
ComposableTest(text = "Composable Text")
}