小编lux*_*i78的帖子

如何使按钮背景颜色在“启用”更改时动画变化?

假设有一个按钮定义如下

val btnEnabled by remember { mutableStateOf(true) }

...

Button(
  colors = ButtonDefaults.buttonColors(
    background = Color.Yellow
    disabledBackgroundColor = Color.Black ),
  enabled = btnEnabled

) {
...
}

Run Code Online (Sandbox Code Playgroud)

当 的值btnEnabled改变时,按钮的背景会立即刚性地改变。有什么办法让它变成动画过渡吗?

android-jetpack-compose

7
推荐指数
1
解决办法
4616
查看次数

是否可以从collect的代码块中停止flow的收集?

我是协程/流的新手,想知道collect当它获得所需的值时从 的代码块关闭流的适当方法。

代码是这样的:

suspend fun findService(scope:CoroutineScope, context:Context, name:String) {
  val flow = getWifiDebuggingConnectDiscoveryFlow( context )
  try {
    flow.collect {
      if(name == it.serviceName)  {
        /* need to exit the collection and execute the code that follows */
      }
    }
    println("service found!")
  } catch(e: Throwable) {
    println("Exception from the flow: $e")
  }

  /* need to do something after service found */

}

private fun getWifiDebuggingConnectDiscoveryFlow(context:Context) = callbackFlow {
  val nsdManager:NsdManager = context.getSystemService(Context.NSD_SERVICE) as NsdManager
  val listener = object : NsdManager.DiscoveryListener …
Run Code Online (Sandbox Code Playgroud)

kotlin kotlin-coroutines

1
推荐指数
1
解决办法
615
查看次数