0
0

State backed values should use the lambda overload of Modifier.offset

Posted at

先に結論

// Before
val someOffset by /* State<Float> */
Modifier.offset(x = someOffset)
// After
val someOffset by /* State<Float> */
Modifier.offset { IntOffset(x = someOffset.dp.toPx().toInt(), y = 0) }

詳細

androidx.compose.foundationを1.7.0アップデートしたところ、次のwarningが出るようになりました。

Modifier.offset{ } is preferred over Modifier.offset() for State backed arguments.: State backed values should use the lambda overload of Modifier.offset

Modifier.offset() is recommended to be used with static arguments only to avoid unnecessary recompositions. Modifier.offset{ } is preferred in the cases where the arguments are backed by a State.

Modifier.offset()をStateによって管理されている値(典型的にはアニメーション等)に使用すると、不必要に再コンポーズが発生するようです。
修正はModifier.offset{ }に置き換えるだけですが、dpを使用する場合、IntOffsetを返すよう適宜変換する必要があります。

fun Modifier.offset(offset: Density.() -> IntOffset)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0