Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a preview of a dialog in jetpack compose?

I have this code:

@Composable
fun SomeDialog() {
    Dialog(onDismissRequest = { }, properties = DialogProperties()) {
        ....
    }
}

@Preview(showBackground = true)
@Composable
fun SomeDialogPreview() {
    MyTherapyPreviewTheme {
        Scaffold {
            SomeDialog()
        }
    }
}

I expected that this preview will generate a preview similar to other composable functions, but instead of this I only see a white rectangle in the preview (pure Scaffold).

How can I generate a preview of dialog in a proper way? Or maybe I can only make a preview of the content of the dialog in a separate function?

like image 762
Krystian Kaniowski Avatar asked Sep 14 '21 07:09

Krystian Kaniowski


People also ask

How do I show compose preview?

Click the Deploy to Device icon next to the @Preview annotation or at the top of the preview, and Android Studio will deploy that @Preview to your connected device or emulator.

Does jetpack compose use views?

Jetpack Compose is designed to work with the established View-based UI approach. If you're building a new app, the best option might be to implement your entire UI with Compose.

What is LazyColumn in jetpack compose?

A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It's similar to a Recyclerview in the classic Android View system.


2 Answers

@MihaiBC is spot-on about why you can not preview (yet). Meanwhile, you can preview dialog content by extracting content of dialog in to a separate composable.

For example, if your dialog looks like this:

@Composable
fun SomeDialog() {
    Dialog(onDismissRequest = { }, properties = DialogProperties()) {
         Card(modifier = Modifier.fillMaxWidth()) {
             .....
        }
    }
}

then extract content as following:

@Composable
fun SomeDialog() {
    Dialog(onDismissRequest = { }, properties = DialogProperties()) {
         SomeDialogContent()
    }
}

@Composable
fun SomeDialogContent(){
   Card(modifier = Modifier.fillMaxWidth()) {
       .....
   }
}

and finally preview this SomeDialogContent with some padding:

@Preview
@Composable
fun PreviewSomeDialogContent() {
    AppTheme {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(MaterialTheme.colors.background)
                .padding(20.dp),
            contentAlignment = Alignment.Center,
        ) {
            SomeDialogContent({})
        }
    }
}

enter image description here

like image 81
HBB20 Avatar answered Nov 10 '22 18:11

HBB20


There is one more way to see the preview, but this time it will be on your live or simulator device.

In the preview window find a Deploy Preview button, which will build your preview on the phone.enter image description here

Note that only this composable will be built.

like image 24
ShadeToD Avatar answered Nov 10 '22 18:11

ShadeToD