Skip to content

Instantly share code, notes, and snippets.

@mustafayigitt
Created November 24, 2021 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustafayigitt/2497ff5575a33edb5253b7c2d578ecc5 to your computer and use it in GitHub Desktop.
Save mustafayigitt/2497ff5575a33edb5253b7c2d578ecc5 to your computer and use it in GitHub Desktop.
ui with Jetpack Compose
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