Created
March 4, 2020 18:51
-
-
Save ShikaSD/2364ff60116659d3fc6cd9df2f956050 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.lifelike | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.compose.Composable | |
import androidx.compose.onActive | |
import androidx.compose.state | |
import androidx.ui.animation.Crossfade | |
import androidx.ui.core.setContent | |
import androidx.ui.foundation.Clickable | |
import androidx.ui.foundation.ColoredRect | |
import androidx.ui.graphics.Color | |
import androidx.ui.layout.LayoutHeight | |
import androidx.ui.layout.LayoutWidth | |
import com.example.lifelike.MainActivity.Route.* | |
class MainActivity : AppCompatActivity() { | |
enum class Route { | |
Red, Black | |
} | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
var route by state { Black } | |
Crossfade(current = route) { | |
Test(it) { | |
route = when (route) { | |
Red -> Black | |
Black -> Red | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun Test(route: Route, onSwitch: () -> Unit) { | |
onActive { | |
println("${route.name}: active") | |
onDispose { | |
println("${route.name}: dispose") | |
} | |
} | |
val color = when (route) { | |
Red -> Color.Red | |
Black -> Color.Black | |
} | |
Clickable(onSwitch) { | |
ColoredRect(color = color, modifier = LayoutWidth.Fill + LayoutHeight.Fill) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment