尝试使用 androidx.navigation:navigation-compose:1.0.0-alpha04
库并在尝试构建我的项目时收到以下“aar”错误。
我使用的是 gradle 6.7。
Incompatible because this component declares a component, with the library elements 'aar' and the consumer needed a component
plugins {
kotlin("jvm") version "1.4.20"
id("org.jetbrains.compose") version "0.2.0-build132"
}
group = "me.g"
version = "1.0"
repositories {
jcenter()
google()
mavenCentral()
maven { url = uri("https://maven.pkg.jetbrains.space/public/p/compose/dev") }
}
dependencies {
implementation("android.arch.lifecycle:livedata:1.1.1")
testImplementation(kotlin("test-junit5"))
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
implementation(compose.desktop.currentOs)
implementation("com.fasterxml.jackson.core:jackson-databind:2.9.0")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.0")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.0")
implementation("com.github.docker-java:docker-java:3.2.7")
implementation("com.github.docker-java:docker-java-transport-httpclient5:3.2.7")
implementation("androidx.navigation:navigation-compose:1.0.0-alpha04")
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "15"
}
compose.desktop {
application {
mainClass = "MainKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "cc-launcher"
}
}
}
答案 0 :(得分:1)
@Suppress("MoveLambdaOutsideParentheses", "FunctionName")
@Composable
private fun Router(
defaultRoute: ApplicationRoutes,
content: @Composable (current: ApplicationRoutes, setRoute: (ApplicationRoutes) -> Unit) -> Unit
) {
val route = remember { mutableStateOf(defaultRoute) }
content(route.value, { route.value = it })
}
private sealed class ApplicationRoutes {
object Menu : ApplicationRoutes()
data class ViewWithData(val data: Any) : ApplicationRoutes()
object Login : ApplicationRoutes()
}
使用数据类,可以在此路由上将值传递给可组合。
您调用可组合路由器并传递默认路由,该路由将在启动时显示并实现内容 lambda。
lambda 的第一个参数为您提供当前路由,您可以传递给 when 来处理所有定义的路由。
第二个参数是活动路由的设置器。调用时路由会改变。
fun main() = Window {
MaterialTheme {
val defaultRoute = Login as ApplicationRoutes
Router(
defaultRoute = defaultRoute
) { current, setRoute ->
when (current) {
Menu -> NavBtns(current, setRoute)
is ViewWithData -> {
Column {
NavBtns(current, setRoute)
Text("NavigationFrom: ${current.data}")
}
}
Login -> NavBtns(current, setRoute)
}
}
}
}
另一个可组合的完整示例:
@Suppress("FunctionName")
@Composable
private fun NavBtns(current: ApplicationRoutes, setRoute: (ApplicationRoutes) -> Unit) {
Column {
Text(current.toString())
Column {
listOf(Menu, ViewWithData(current.toString()), Login)
.forEach {
Button(onClick = { setRoute(it) }) {
Text(text = it.toString())
}
}
}
}
}