Skip to content

Instantly share code, notes, and snippets.

@mezhevikin
Created August 9, 2023 12:43
Show Gist options
  • Save mezhevikin/27d23484ff5ae41a137544593f85f376 to your computer and use it in GitHub Desktop.
Save mezhevikin/27d23484ff5ae41a137544593f85f376 to your computer and use it in GitHub Desktop.
dependencies {
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
implementation("androidx.compose.ui:ui:1.4.3")
implementation("androidx.compose.material:material:1.4.3")
implementation("androidx.compose.ui:ui-tooling:1.4.3")
implementation("androidx.navigation:navigation-compose:2.7.0-rc01")
implementation("androidx.compose.animation:animation:1.4.3")
implementation("androidx.activity:activity-compose:1.7.2")
}
package com.mezhevikin.converter.testus
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.*
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NavigationApp()
}
}
}
fun NavGraphBuilder.animatedComposable(
route: String,
content: @Composable () -> Unit
) {
composable(
route,
enterTransition = {
slideInHorizontally(
initialOffsetX = { 1000 },
animationSpec = tween(200, easing = LinearEasing)
)
},
exitTransition = {
slideOutHorizontally(
targetOffsetX = { 1000 },
animationSpec = tween(200, easing = LinearEasing)
)
},
) {
content()
}
}
@Composable
fun NavigationApp() {
val navController = rememberNavController()
NavHost(navController, startDestination = "greenScreen") {
composable("greenScreen") { GreenScreen(navController) }
animatedComposable("redScreen") { RedScreen(navController) }
}
}
@Composable
fun GreenScreen(navController: NavHostController) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Green),
contentAlignment = Alignment.Center
) {
Button(onClick = { navController.navigate("redScreen") }) {
Text("Open Red")
}
}
}
@Composable
fun RedScreen(navController: NavHostController) {
Box(
modifier = Modifier
.fillMaxSize()
.background(Color.Red),
contentAlignment = Alignment.Center
) {
Button(onClick = { navController.popBackStack() }) {
Text("Back")
}
}
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
NavigationApp()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment