尝试使用androidx.navigation:navigation-compose:1.0.0-alpha04
库,并在尝试构建项目时出现以下"aar"错误。我用的是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"
}
}
}
以下是路由器的示例:
@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()
}
有了数据类,就可以在这个路由上将值传递给compositable。
如何使用:
您调用可组合路由器并传递默认路由,该路由将在启动时显示并实现内容lambda。
lambda的第一个参数为您提供了当前路由,您可以在何时处理所有定义的路由时将其传递给。
第二个参数是活动路由的设置器。调用时,路由将发生更改。
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())
}
}
}
}
}