Skip to content

Instantly share code, notes, and snippets.

@jershell
Created August 10, 2021 12:44
Show Gist options
  • Save jershell/0691d5633f24684fe84d2f9e23719e30 to your computer and use it in GitHub Desktop.
Save jershell/0691d5633f24684fe84d2f9e23719e30 to your computer and use it in GitHub Desktop.
popup full screen compose dialog
package components
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.*
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupPositionProvider
@Composable
actual fun Dialog(content: @Composable () -> Unit) {
Popup(popupPositionProvider = object : PopupPositionProvider {
override fun calculatePosition(
anchorBounds: IntRect,
windowSize: IntSize,
layoutDirection: LayoutDirection,
popupContentSize: IntSize
): IntOffset {
return IntOffset.Zero
}
}) {
Box(
modifier = Modifier.fillMaxSize().background(Color.Green),
contentAlignment = Alignment.Center,
content = {
Text("AAA")
}
)
}
}
@jershell
Copy link
Author

if android

    val resourceId: Int = LocalContext.current.resources.getIdentifier("status_bar_height", "dimen", "android")

    val h = if (resourceId > 0) {
        LocalContext.current.resources.getDimensionPixelSize(resourceId)
    } else 0

    Popup(popupPositionProvider = object : PopupPositionProvider {
        override fun calculatePosition(
            anchorBounds: IntRect,
            windowSize: IntSize,
            layoutDirection: LayoutDirection,
            popupContentSize: IntSize
        ): IntOffset {

            println("debug_popup anchorBounds $anchorBounds")
            println("debug_popup windowSize $windowSize")
            println("debug_popup layoutDirection $layoutDirection")
            println("debug_popup popupContentSize $popupContentSize")

            return IntOffset(0, h)
        }

    })

@chientrm
Copy link

It exits the app when I open the popup and press button Back.

@Vancltkin
Copy link

Vancltkin commented Apr 23, 2024

It exits the app when I open the popup and press button Back.

Hello bro wtf

@Composable
fun DialogPopUp(onCloseClick: () -> Unit, content: @Composable () -> Unit) {
    Popup(
        onDismissRequest = onCloseClick,
        popupPositionProvider = object : PopupPositionProvider {

        override fun calculatePosition(
            anchorBounds: IntRect,
            windowSize: IntSize,
            layoutDirection: LayoutDirection,
            popupContentSize: IntSize
        ): IntOffset {
            return IntOffset.Zero
        }

    }) {
    `

        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(Color.White),
            contentAlignment = Alignment.Center,
            content = {
                content()
            }
        )

        BackHandler {
            onCloseClick()
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment