[英]Hoist pressed state in Jetpack Compose Beta 1 after InteractionState was removed
在 compose-beta01 之前,使用InteractionState
在 Jetpack Compose 中提升按下的 state 非常容易:
@Composable
fun App() {
val interactionState = remember { InteractionState() }
val pressed = interactionState.contains(Interaction.Pressed)
MyComposable(Modifier.clickable(interactionState = interactionState) { })
}
InteractionState 在 beta01 中被刪除,現在有明顯的方法來復制這種行為。 如何使用clickable
的修飾符提升按下的 state?
您是否嘗試過應用Compose beta01發行說明中解釋的內容?
InteractionState
已替換為[Mutable]InteractionSource
- 接口負責發出/收集交互事件。
- 不要將
interactionState = remember { InteractionState() }
傳遞給Button
和Modifier.clickable()
等組件,而是使用interactionSource = remember { MutableInteractionSource() }
。- 而不是:
Interaction.Pressed in interactionState
您應該使用 InteractionSource 上的擴展函數,例如 InteractionSource.collectIsPressedAsState()。- 對於復雜的用例,您可以使用 InteractionSource.interactions 來觀察交互的 stream。 有關詳細信息,請參閱 InteractionSource 文檔和示例。
- ( I85965 、 b/152525426 、 b/171913923 、 b/171710801 、 b/174852378 )
所以,在你的例子中,我會嘗試這樣的事情:
val interactionSource = remember { MutableInteractionSource() }
val pressedState = interactionSource.collectIsPressedAsState()
MyComposable(
Modifier.clickable(
interactionSource = interactionSource,
indication = LocalIndication.current
) {}
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.