Skip to content

Instantly share code, notes, and snippets.

@Nike2406
Created December 1, 2025 08:34
Show Gist options
  • Select an option

  • Save Nike2406/bdb9210d3aebcbb9e1dcf914ab82eec4 to your computer and use it in GitHub Desktop.

Select an option

Save Nike2406/bdb9210d3aebcbb9e1dcf914ab82eec4 to your computer and use it in GitHub Desktop.
SplashScreen
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.LinearProgressIndicator
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun SplashScreen(
navHostController: NavHostController,
) {
val images = listOf(
R.drawable.splash_first,
R.drawable.splash_second,
R.drawable.splahs_third
)
var position by remember { mutableStateOf(0) }
var play by remember { mutableStateOf(true) }
val pagerState = rememberPagerState { images.size } // rememberPagerState(initialPage = 0)
val scope = rememberCoroutineScope()
LaunchedEffect(play) {
while (true) {
delay(2000)
position = (pagerState.currentPage + 1) % (images.size)
pagerState.animateScrollToPage(
page = position,
animationSpec = tween(700)
)
}
}
Box(
Modifier
.fillMaxSize()
) {
HorizontalPager(
state = pagerState,
beyondBoundsPageCount = images.size,
userScrollEnabled = false,
) { item ->
Image(
modifier = Modifier.fillMaxSize(),
painter = painterResource(id = images[item]),
contentDescription = null
)
}
SplashScreenTopAppBar(
itemCount = images.size,
currentItem = position
)
Column(Modifier.fillMaxSize()) {
Row(Modifier.weight(1f)) {
Box(
Modifier
.fillMaxSize()
.weight(1f)
.clickable {
if (position > 0) {
--position
scope.launch {
play = false
pagerState.animateScrollToPage(position)
play = true
}
}
})
Box(
Modifier
.fillMaxSize()
.weight(1f)
.clickable {
if (position < images.size) {
++position
scope.launch {
play = false
pagerState.animateScrollToPage(position)
play = true
}
}
})
}
SubmitButton(
modifier = Modifier
.height(48.dp)
.padding(start = 16.dp, end = 16.dp),
title = stringResource(R.string.registration)
) {
navHostController.navigate(Screen.RegistrationScreen.route)
}
Row(
modifier = Modifier
.fillMaxWidth()
.padding(start = 48.dp, end = 48.dp, top = 20.dp, bottom = 58.dp),
horizontalArrangement = Arrangement.Center
) {
Text(text = stringResource(id = R.string.already_have_account))
Spacer(modifier = Modifier.size(8.dp))
Text(
text = stringResource(id = R.string.authorize),
color = Orange,
modifier = Modifier.clickable {
navHostController.navigate(Screen.AuthorisationScreen.route)
})
}
}
}
}
@Composable
fun SplashScreenTopAppBar(
itemCount: Int,
currentItem: Int,
) {
TopAppBar(
backgroundColor = Color.White,
elevation = 0.dp,
) {
for (i in 0 until itemCount) {
var progress by remember { mutableStateOf(0f) }
val animationProgress by animateFloatAsState(
targetValue = progress,
visibilityThreshold = 0f,
animationSpec = tween(2000)
)
if (i == currentItem) {
LaunchedEffect(true) {
progress = 1f
}
} else {
progress = 0f
}
LinearProgressIndicator(
modifier = Modifier
.weight(1f)
.padding(4.dp),
progress = when {
currentItem == i -> animationProgress
currentItem > i -> 1f
else -> 0f
},
strokeCap = StrokeCap.Round
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment