mattinger
01/18/2023, 9:43 PM@OptIn(ExperimentalPagerApi::class)
@Preview
@Composable
fun CarouselPreview() {
MaterialTheme {
Scaffold(
topBar = {
TopAppBar(
title = { },
actions = {
IconButton(onClick = { }) {
Icon(imageVector = Icons.Default.Close, contentDescription = "Close")
}
}
)
}
) {
Column(
modifier = Modifier
.padding(it)
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = spacedBy(16.dp),
) {
val pagerState = rememberPagerState()
val focusRequesters = remember {
(0 until 6).map {
FocusRequester()
}
}
val flow = snapshotFlow { pagerState.currentPage }
LaunchedEffect(key1 = pagerState) {
flow.collect { index ->
focusRequesters[index].requestFocus()
}
}
HorizontalPager(
modifier = Modifier
.weight(1.0f, true)
.fillMaxWidth(),
count = 6,
state = pagerState
) { index ->
Box(s
contentAlignment = Alignment.Center
) {
Text(
modifier = Modifier
.focusable(true)
.focusRequester(focusRequesters[index]),
text = "Page ${index}"
)
}
}
HorizontalPagerIndicator(pagerState = pagerState)
}
}
}
}