Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preview a "screen" in android jetpack compose navigation with a PreviewParameter NavController

I am discovering android Jetpack Compose (and Navigation) and try to display a preview of a view with a navController as parameter.

To achieve this, I use the PreviewParameter and I have no error, but nothing is displayed in the Preview window.

Does anyone know how pass a fake NavController instance to a Composable?

class FakeNavController : PreviewParameterProvider<NavController> {
    override val values: Sequence<NavController>
        get() {}
}

@Preview
@Composable
fun Preview(
    @PreviewParameter(FakeNavController::class) fakeNavController: NavController
) {
    HomeView(fakeNavController)
}
like image 485
david CHOLLEZ Avatar asked Dec 10 '25 01:12

david CHOLLEZ


1 Answers

You don't have to make it nullable and pass null to it. You just need to pass this: rememberNavController()

@Preview
@Composable
fun Preview() {
    HomeView(rememberNavController())
}
like image 104
Alireza Nezami Avatar answered Dec 13 '25 17:12

Alireza Nezami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!