Created
November 24, 2021 10:36
-
-
Save mustafayigitt/2497ff5575a33edb5253b7c2d578ecc5 to your computer and use it in GitHub Desktop.
ui with Jetpack Compose
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
val scrollState = rememberScrollState() | |
val scope = rememberCoroutineScope() | |
var selectedAnimal by remember { mutableStateOf<Animal?>(null) } | |
Column( | |
modifier = Modifier | |
.fillMaxSize() | |
.verticalScroll(scrollState), | |
verticalArrangement = Arrangement.spacedBy(20.dp), | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Text( | |
text = "Hello Compose", | |
style = MaterialTheme.typography.h2, | |
textAlign = TextAlign.Center, | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(horizontal = 24.dp) | |
) | |
Image( | |
painter = painterResource( | |
id = selectedAnimal?.imageSrc ?: R.drawable.ic_launcher_foreground | |
), | |
contentDescription = "selected animal image", | |
modifier = Modifier | |
.fillMaxWidth(fraction = 0.75f) | |
.aspectRatio(3 / 8f), | |
contentScale = ContentScale.Crop | |
) | |
Row( | |
modifier = Modifier | |
.fillMaxWidth() | |
.wrapContentHeight(), | |
horizontalArrangement = Arrangement.SpaceEvenly | |
) { | |
RadioButton( | |
selected = selectedAnimal is Animal.Dog, | |
onClick = { selectedAnimal = Animal.Dog }, | |
) | |
RadioButton( | |
selected = selectedAnimal is Animal.Cat, | |
onClick = { selectedAnimal = Animal.Cat }, | |
) | |
} | |
Button( | |
onClick = { | |
scope.launch { | |
scrollState.animateScrollTo(0) | |
} | |
}, | |
colors = ButtonDefaults.buttonColors( | |
backgroundColor = Color.Black, | |
contentColor = Color.White, | |
) | |
) { | |
Text(text = "Scroll to Top") | |
} | |
Spacer(modifier = Modifier.size(100.dp)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment