pref: 动态页 Tab 文字样式

This commit is contained in:
HuanCheng65 2023-07-23 22:39:44 +08:00
parent 9cc4d7ec8d
commit 5248ce6cf8
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
1 changed files with 25 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.PagerState import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold import androidx.compose.material.Scaffold
import androidx.compose.material.Tab import androidx.compose.material.Tab
import androidx.compose.material.TabRow import androidx.compose.material.TabRow
@ -21,7 +22,11 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.util.fastForEachIndexed
import com.huanchengfly.tieba.post.R import com.huanchengfly.tieba.post.R
import com.huanchengfly.tieba.post.activities.NewSearchActivity import com.huanchengfly.tieba.post.activities.NewSearchActivity
import com.huanchengfly.tieba.post.arch.GlobalEvent import com.huanchengfly.tieba.post.arch.GlobalEvent
@ -46,7 +51,7 @@ import kotlinx.coroutines.launch
@Immutable @Immutable
data class ExplorePageItem( data class ExplorePageItem(
val id: String, val id: String,
val name: @Composable () -> Unit, val name: @Composable (selected: Boolean) -> Unit,
val content: @Composable () -> Unit, val content: @Composable () -> Unit,
) )
@ -73,14 +78,14 @@ private fun ColumnScope.ExplorePageTab(
.align(Alignment.CenterHorizontally) .align(Alignment.CenterHorizontally)
.width(76.dp * pages.size), .width(76.dp * pages.size),
) { ) {
pages.forEachIndexed { index, item -> pages.fastForEachIndexed { index, item ->
Tab( Tab(
text = item.name, text = { item.name(pagerState.currentPage == index) },
selected = pagerState.currentPage == index, selected = pagerState.currentPage == index,
onClick = { onClick = {
coroutineScope.launch { coroutineScope.launch {
if (pagerState.currentPage == index) { if (pagerState.currentPage == index) {
coroutineScope.emitGlobalEvent(GlobalEvent.Refresh(item.id)) emitGlobalEvent(GlobalEvent.Refresh(item.id))
} else { } else {
pagerState.animateScrollToPage(index) pagerState.animateScrollToPage(index)
} }
@ -91,6 +96,19 @@ private fun ColumnScope.ExplorePageTab(
} }
} }
@Composable
private fun TabText(
text: String,
selected: Boolean
) {
val style = MaterialTheme.typography.button.copy(
letterSpacing = 0.75.sp,
fontWeight = if (selected) FontWeight.Bold else FontWeight.Normal,
textAlign = TextAlign.Center
)
Text(text = text, style = style)
}
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun ExplorePage() { fun ExplorePage() {
@ -103,17 +121,17 @@ fun ExplorePage() {
listOfNotNull( listOfNotNull(
if (loggedIn) ExplorePageItem( if (loggedIn) ExplorePageItem(
"concern", "concern",
{ Text(text = stringResource(id = R.string.title_concern)) }, { TabText(text = stringResource(id = R.string.title_concern), selected = it) },
{ ConcernPage() } { ConcernPage() }
) else null, ) else null,
ExplorePageItem( ExplorePageItem(
"personalized", "personalized",
{ Text(text = stringResource(id = R.string.title_personalized)) }, { TabText(text = stringResource(id = R.string.title_personalized), selected = it) },
{ PersonalizedPage() } { PersonalizedPage() }
), ),
ExplorePageItem( ExplorePageItem(
"hot", "hot",
{ Text(text = stringResource(id = R.string.title_hot)) }, { TabText(text = stringResource(id = R.string.title_hot), selected = it) },
{ HotPage() } { HotPage() }
), ),
).toImmutableList() ).toImmutableList()