Created
September 16, 2022 09:18
-
-
Save stevdza-san/5743a32dc1ec2d8eaf62c29d9bab1c43 to your computer and use it in GitHub Desktop.
Disappearing and Animated Top Bar - Jetpack Compose
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 android.annotation.SuppressLint | |
import androidx.compose.animation.animateContentSize | |
import androidx.compose.animation.core.animateDpAsState | |
import androidx.compose.animation.core.tween | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.foundation.lazy.LazyColumn | |
import androidx.compose.foundation.lazy.LazyListState | |
import androidx.compose.foundation.lazy.items | |
import androidx.compose.foundation.lazy.rememberLazyListState | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Scaffold | |
import androidx.compose.material.Text | |
import androidx.compose.material.TopAppBar | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.remember | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.text.TextStyle | |
import androidx.compose.ui.text.font.FontWeight | |
import androidx.compose.ui.unit.dp | |
@SuppressLint("UnusedMaterialScaffoldPaddingParameter") | |
@Composable | |
fun MainScreen() { | |
val lazyListState = rememberLazyListState() | |
Scaffold( | |
content = { | |
Box(modifier = Modifier.fillMaxSize()) { | |
MainContent(lazyListState = lazyListState) | |
TopBar(lazyListState = lazyListState) | |
} | |
} | |
) | |
} | |
@Composable | |
fun TopBar(lazyListState: LazyListState) { | |
TopAppBar( | |
modifier = Modifier | |
.fillMaxWidth() | |
.background(color = MaterialTheme.colors.primary) | |
.animateContentSize(animationSpec = tween(durationMillis = 300)) | |
.height(height = if (lazyListState.isScrolled) 0.dp else TOP_BAR_HEIGHT) | |
, | |
contentPadding = PaddingValues(start = 16.dp) | |
) { | |
Text( | |
text = "Title", | |
style = TextStyle( | |
fontSize = MaterialTheme.typography.h6.fontSize, | |
color = MaterialTheme.colors.surface | |
) | |
) | |
} | |
} | |
@Composable | |
fun MainContent(lazyListState: LazyListState) { | |
val numbers = remember { List(size = 25) { it } } | |
val padding by animateDpAsState( | |
targetValue = if (lazyListState.isScrolled) 0.dp else TOP_BAR_HEIGHT, | |
animationSpec = tween(durationMillis = 300) | |
) | |
LazyColumn( | |
modifier = Modifier.padding(top = padding), | |
state = lazyListState | |
) { | |
items( | |
items = numbers, | |
key = { it } | |
) { | |
NumberHolder(number = it) | |
} | |
} | |
} | |
@Composable | |
fun NumberHolder(number: Int) { | |
Row( | |
modifier = Modifier.fillMaxWidth(), | |
verticalAlignment = Alignment.CenterVertically | |
) { | |
Text( | |
text = number.toString(), | |
style = TextStyle( | |
fontSize = MaterialTheme.typography.h1.fontSize, | |
fontWeight = FontWeight.Bold | |
) | |
) | |
} | |
} | |
val TOP_BAR_HEIGHT = 56.dp | |
val LazyListState.isScrolled: Boolean | |
get() = firstVisibleItemIndex > 0 || firstVisibleItemScrollOffset > 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment