|
| 1 | +package com.smarttoolfactory.composematerialslider |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import androidx.activity.ComponentActivity |
| 5 | +import androidx.activity.compose.setContent |
| 6 | +import androidx.compose.animation.ExperimentalAnimationApi |
| 7 | +import androidx.compose.foundation.layout.Column |
| 8 | +import androidx.compose.foundation.layout.fillMaxSize |
| 9 | +import androidx.compose.material.* |
| 10 | +import androidx.compose.runtime.Composable |
| 11 | +import androidx.compose.runtime.rememberCoroutineScope |
| 12 | +import androidx.compose.ui.Modifier |
| 13 | +import androidx.compose.ui.graphics.Color |
| 14 | +import androidx.compose.ui.unit.dp |
| 15 | +import com.google.accompanist.pager.* |
| 16 | +import com.smarttoolfactory.composematerialslider.demo.ColorfulSliderDemo |
| 17 | +import com.smarttoolfactory.composematerialslider.demo.SliderGradientDemo |
| 18 | +import com.smarttoolfactory.composematerialslider.demo.SliderGradientDemo2 |
| 19 | +import com.smarttoolfactory.composematerialslider.demo.SliderPropertiesDemo |
| 20 | +import com.smarttoolfactory.composematerialslider.ui.theme.ComposeMaterialSliderTheme |
| 21 | +import kotlinx.coroutines.launch |
| 22 | + |
| 23 | +@ExperimentalPagerApi |
| 24 | +class MainActivity : ComponentActivity() { |
| 25 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 26 | + super.onCreate(savedInstanceState) |
| 27 | + setContent { |
| 28 | + ComposeMaterialSliderTheme { |
| 29 | + // A surface container using the 'background' color from the theme |
| 30 | + Surface(color = MaterialTheme.colors.background) { |
| 31 | + Column( |
| 32 | + modifier = Modifier.fillMaxSize() |
| 33 | + ) { |
| 34 | + HomeContent() |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +@ExperimentalPagerApi |
| 43 | +@OptIn(ExperimentalAnimationApi::class) |
| 44 | +@Composable |
| 45 | +private fun HomeContent() { |
| 46 | + |
| 47 | + val pagerState: PagerState = rememberPagerState(initialPage = 0) |
| 48 | + val coroutineScope = rememberCoroutineScope() |
| 49 | + |
| 50 | + ScrollableTabRow( |
| 51 | + backgroundColor = Color(0xff00897B), |
| 52 | + contentColor = Color.White, |
| 53 | + edgePadding = 8.dp, |
| 54 | + // Our selected tab is our current page |
| 55 | + selectedTabIndex = pagerState.currentPage, |
| 56 | + // Override the indicator, using the provided pagerTabIndicatorOffset modifier |
| 57 | + indicator = { tabPositions -> |
| 58 | + TabRowDefaults.Indicator( |
| 59 | + Modifier.pagerTabIndicatorOffset(pagerState, tabPositions) |
| 60 | + ) |
| 61 | + } |
| 62 | + ) { |
| 63 | + // Add tabs for all of our pages |
| 64 | + tabList.forEachIndexed { index, title -> |
| 65 | + Tab( |
| 66 | + text = { Text(title) }, |
| 67 | + selected = pagerState.currentPage == index, |
| 68 | + onClick = { |
| 69 | + coroutineScope.launch { |
| 70 | + pagerState.animateScrollToPage(index) |
| 71 | + } |
| 72 | + }, |
| 73 | + ) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + HorizontalPager( |
| 78 | + state = pagerState, |
| 79 | + count = tabList.size |
| 80 | + ) { page: Int -> |
| 81 | + |
| 82 | + when (page) { |
| 83 | + 0 -> ColorfulSliderDemo() |
| 84 | + 1 -> SliderGradientDemo() |
| 85 | + 2 -> SliderGradientDemo2() |
| 86 | + else -> SliderPropertiesDemo() |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +internal val tabList = |
| 92 | + listOf( |
| 93 | + "Slider Dimensions", |
| 94 | + "Slider Gradients1", |
| 95 | + "Slider Gradients2", |
| 96 | + "Slider Properties", |
| 97 | + ) |
| 98 | + |
0 commit comments