Row Text(Hi) Sample Text(App) remember Image Hi @Composable fun Sample(){ var x by remember {*.} Text("Hi") Row{ Text("App") Image(*.) } } Runtime の Smart Recomposition
Row Text(Hi) Sample Text(App) remember Image Hi @Composable fun Sample(){ var x by remember {*.} Text("Hi") Row{ Text("App") Image(*.) } } Runtime の Smart Recomposition
interface Modifier { abstract class Node { open fun onAttach() {} open fun onDetach() {} var isAttached: Boolean private set ** *. */ } } Modifier.Node がチェーンに追加さ れる直後に呼び出されます。 Modifier.Node
interface Modifier { abstract class Node { open fun onAttach() {} open fun onDetach() {} var isAttached: Boolean private set ** *. */ } } Modifier.Node チェーンから外れた 直前に呼び出されます。 Modifier.Node
abstract class ModifierNodeElement { abstract fun create(): N abstract fun update(node: N) abstract fun equals(other: Any?) abstract fun hashCode(): Int ** *. */ } Modifier がはじめてレイアウトに設定された時呼び出されます。 新しい Modifier.Node のインスタンスを返します。 ModifierNodeElement
Modifier が変更された時呼び出されます。 既存の Modifier.Node で保持されてる state を更新するチャンスです。 Modifier が変更された時呼び出されます。 abstract class ModifierNodeElement { abstract fun create(): N abstract fun update(node: N) abstract fun equals(other: Any?) abstract fun hashCode(): Int ** *. */ } ModifierNodeElement
abstract class ModifierNodeElement { abstract fun create(): N abstract fun update(node: N) abstract fun equals(other: Any?) abstract fun hashCode(): Int } このメソッドで update を呼び出すかどうかの判断行われます。 ModifierNodeElement
fun Modifier.focusable(*.) = composed { var isFocused by remember { mutableStateOf(false) } } class FocusableNode : Modifier.Node, *. { private var isFocused = false // private var isFocused by mutableStateOf(false) } Recompositionを実行させたい場合 remember{..}
class FocusableNode() : Modifier.Node(), CompositionLocalConsumerModifierNode, *. { init { currentValueOf(LocalContext) } override fun onAttach() { currentValueOf(LocalContext) } fun someMethod() { if (isAttached) { currentValueOf(LocalContext) } } } “Cannot read CompositionLocal because the Modifier node is not currently attached.” CompositionLocal の値を取得
fun Modifier.tabIndicatorOffset(*.) = composed { val currentTabWidth by animateDpAsState(*.) ** *. */ } fun Modifier.tabIndicatorOffset(*.) = composed { val currentTabWidthAnim = remember { Animatable(..) } val currentTabWidth by currentTabWidthAnim.asState() ** *. */ } 移行の事例