0

I'm new to kotlin jetpack compose and I was wondering if I could use a custom image from my drawable as my icon. I have this sealed class BottomBarScreen and has 3 objects.enter image description here

I've tried using a composable but it just omits an error called "@Composable invocations can only happen from the context of a @Composable function." I hope someone can enlighten me on this matter, thank you^^

1 Answer 1

0

I also pass resources as a parameter. You can check the below examples.


MenuItem.kt


data class MenuItem(
    val id: String,
    val title: String,
    val contentDescription: String,
    val icon: Painter
)

Data

@Composable
private fun prepareNavigationDrawerItems(): List<MenuItem> {
    val itemsList = arrayListOf<MenuItem>()

    itemsList.add(
        MenuItem(
            id = "contact_us",
            title = stringResource(R.string.contact_us),
            contentDescription = "Contact Us",
            icon = painterResource(id = R.drawable.contact_us)
        )
    )
    return itemsList
}

Final Screen

@Composable
fun MyCompleteDrawer() {
    val itemsList = prepareNavigationDrawerItems()
    LazyColumn(
        modifier = Modifier
            .fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        contentPadding = PaddingValues(vertical = 20.dp)
    ) {

        items(itemsList) { item ->
            NavigationListItem(item = item) {
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.