Closed3

Modifier.onFocusChangedと該当箇所を隠さないコツ

izumarthizumarth
  • Modifier.onFocusChangedを使うと該当箇所にFocusが当たった際の処理が記載できる
  • TextFieldなどで入力カーソルが来た場合などに使える
TextField(
        modifier = modifier
            .onFocusChanged { state ->
                if (state.hasFocus) {
                  ...
                }
            },
       ....
izumarthizumarth
  • TextFieldなどが下に存在する場合、Keyboardが表示されると隠れる場合がある
  • その場合に使える
  • Focus時にbringIntoViewすれば良い
TextField(
        modifier = modifier
                .bringIntoViewRequester(requester)
                .onFocusEvent { focusState ->
                    if (focusState.isFocused) {
                        coroutineScope.launch {
                            delay(200)
                            requester.bringIntoView()
                        }
                    }
                }
       ....
izumarthizumarth
  • requesterだけでなくこういった使い方も(公式より)
        Button(
            onClick = {
                val circleCoordinates = Rect(500f, 0f, 1000f, 500f)
                coroutineScope.launch {
                    // This sends a request to all parents that asks them to scroll so that
                    // the circle is brought into view.
                    bringIntoViewRequester.bringIntoView(circleCoordinates)
                }
            }
        ) {
            Text("Bring circle into View")
        }
このスクラップは2023/07/20にクローズされました