Created
July 14, 2024 07:05
-
-
Save mzennis/4a949dab68a16c2aadbfeb05f473ed7f to your computer and use it in GitHub Desktop.
Swipe views with tabs in Compose Multiplatform
This file contains hidden or 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
import androidx.compose.foundation.ExperimentalFoundationApi | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.pager.HorizontalPager | |
import androidx.compose.foundation.pager.rememberPagerState | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Scaffold | |
import androidx.compose.material.ScrollableTabRow | |
import androidx.compose.material.Tab | |
import androidx.compose.material.Text | |
import androidx.compose.material.TopAppBar | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.derivedStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.runtime.rememberCoroutineScope | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.unit.dp | |
import kotlinx.coroutines.launch | |
import ui.theme.AppTheme | |
enum class Tabs(val text: String) { | |
Explore("Explore"), | |
Missions("Missions"), | |
HumansInSpace("Humans in Space"), | |
EarthAndClimate("Earth & Climate"), | |
TheSolarSystem("The Solar System"), | |
TheUniverse("The Universe"), | |
Science("Science"), | |
Aeronautics("Aeronautics"), | |
Technology("Technology") | |
} | |
@OptIn(ExperimentalFoundationApi::class) | |
@Composable | |
fun App() { | |
val scope = rememberCoroutineScope() | |
val pagerState = rememberPagerState(pageCount = { Tabs.entries.size }) | |
val selectedTabIndex = remember { derivedStateOf { pagerState.currentPage } } | |
AppTheme { | |
Scaffold( | |
topBar = { | |
TopAppBar( | |
title = { | |
Text("NASA \uD83D\uDCAB") | |
}, | |
backgroundColor = MaterialTheme.colors.background, | |
elevation = 0.dp | |
) | |
} | |
) { | |
Column( | |
modifier = Modifier | |
.fillMaxSize() | |
.padding(top = it.calculateTopPadding()) | |
) { | |
ScrollableTabRow( | |
backgroundColor = Color.Transparent, | |
selectedTabIndex = selectedTabIndex.value, | |
modifier = Modifier.fillMaxWidth() | |
) { | |
Tabs.entries.forEachIndexed { index, currentTab -> | |
Tab( | |
selected = selectedTabIndex.value == index, | |
onClick = { | |
scope.launch { | |
pagerState.animateScrollToPage(currentTab.ordinal) | |
} | |
}, | |
text = { Text(text = currentTab.text) } | |
) | |
} | |
} | |
HorizontalPager( | |
state = pagerState, | |
modifier = Modifier | |
.fillMaxWidth() | |
.weight(1f) | |
) { | |
Box( | |
modifier = Modifier.fillMaxSize(), | |
contentAlignment = Alignment.Center | |
) { | |
/** | |
* @TODO render page based on selectedTabIndex | |
*/ | |
Text(text = Tabs.entries[selectedTabIndex.value].text) | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment