Interface

PlatformTextInputModifierNode

A modifier node that can connect to the platform's text input IME system.

platformTextInputModifierNodeSample

@Suppress("UnusedReceiverParameter")
@Sampled
fun platformTextInputModifierNodeSample() {
    class PlatformTextInputModifierNodeSample :
        Modifier.Node(), FocusEventModifierNode, PlatformTextInputModifierNode {

        private var focusedJob: Job? = null

        override fun onFocusEvent(focusState: FocusState) {
            focusedJob?.cancel()
            focusedJob =
                if (focusState.isFocused) {
                    // establishTextInputSession is a suspend function, so it must be called from a
                    // coroutine. Launching it into this modifier node's coroutine scope ensures the
                    // session will automatically be torn down when the node is detached.
                    coroutineScope.launch {
                        // This will automatically cancel any currently-active session.
                        establishTextInputSession {
                            launch {
                                // TODO: Observe text field state, call into system to update it as
                                //  required by the platform.
                            }

                            // Call out to a platform-specific expect/actual function to create the
                            // platform-specific request.
                            val request: PlatformTextInputMethodRequest = createInputRequest()
                            startInputMethod(request)
                        }
                    }
                } else {
                    null
                }
        }

        // This would probably be an expect/actual function.
        private fun PlatformTextInputSession.createInputRequest(): PlatformTextInputMethodRequest {
            TODO("Create platform-specific request")
        }
    }
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: