Skip to content

Commit

Permalink
added slider with optional label
Browse files Browse the repository at this point in the history
  • Loading branch information
charlee-dev committed Feb 28, 2022
1 parent baec5a8 commit d3e5177
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 118 deletions.
10 changes: 10 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.adwi.neumorph.android.components.PreviewTemplate
import com.adwi.neumorph.android.components.MorphSlider

@OptIn(ExperimentalAnimationApi::class)
@Composable
Expand All @@ -23,8 +24,8 @@ fun OptionsItem(
value: Float,
onValueChange: (Float) -> Unit,
valueRange: ClosedFloatingPointRange<Float>,
elevation: Dp = 4.dp,
cornerRadius: Dp = 4.dp,
elevation: Dp = 10.dp,
cornerRadius: Dp = 10.dp,
backgroundColor: Color = MaterialTheme.colors.background,
labelColor: Color = MaterialTheme.colors.onBackground,
) {
Expand All @@ -34,7 +35,7 @@ fun OptionsItem(
color = MaterialTheme.colors.onSurface
)
Spacer(modifier = Modifier.size(12.dp))
SliderWithLabel(
MorphSlider(
value = value,
onValueChange = onValueChange,
valueRange = valueRange,
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fun HomeScreen() {
isExpanded.scrollToItemIfExpanded(scope, lazyListState, 6)
}
) { elevation, cornerRadius, color ->
MorphSlider(
MorphSliderBar(
value = sliderValue,
onValueChange = { sliderValue = it },
elevation = elevation.dp,
Expand Down
Binary file modified art/sliderd.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified art/sliderl.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import com.github.krottv.compose.sliders.SliderValueHorizontal
* @param darkShadowColor color of darker shadow
*/
@Composable
fun MorphSlider(
fun MorphSliderBar(
modifier: Modifier = Modifier,
value: Float = 20f,
onValueChange: (Float) -> Unit,
Expand Down Expand Up @@ -85,7 +85,7 @@ fun MorphSlider(

BoxWithConstraints(
contentAlignment = Alignment.CenterStart,
modifier = Modifier.fillMaxSize()
modifier = Modifier.fillMaxWidth()
) {
val indicatorWidth = 1f / (valueRange.endInclusive - valueRange.start) * value
val offset = (this.maxWidth - handleSize.height) * indicatorWidth
Expand Down Expand Up @@ -196,7 +196,7 @@ private fun SliderLight() {
darkTheme = false,
) {
var value by remember { mutableStateOf(10f) }
MorphSlider(
MorphSliderBar(
value = value,
onValueChange = { value = it },
handleColor = MaterialTheme.colors.primary
Expand All @@ -211,7 +211,7 @@ private fun SliderDark() {
darkTheme = true,
) {
var value by remember { mutableStateOf(10f) }
MorphSlider(
MorphSliderBar(
value = value,
onValueChange = { value = it },
handleColor = MaterialTheme.colors.primary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.adwi.neumorph.sample.ui.components
package com.adwi.neumorph.android.components

import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
Expand All @@ -9,14 +9,15 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import com.adwi.neumorph.android.MorphSlider
import com.adwi.neumorph.android.components.PreviewTemplate
import com.adwi.neumorph.android.MorphSliderBar
import com.adwi.neumorph.android.neumorph.LightSource
import com.adwi.neumorph.android.theme.AppColors
import com.adwi.neumorph.sample.ui.components.SliderLabel

@Composable
fun SliderWithLabel(
fun MorphSlider(
modifier: Modifier = Modifier,
hasLabel: Boolean = true,
value: Float = 10f,
onValueChange: (Float) -> Unit,
valueRange: ClosedFloatingPointRange<Float> = 0f..30f,
Expand All @@ -36,28 +37,33 @@ fun SliderWithLabel(
labelColor: Color = MaterialTheme.colors.onBackground,
) {
Column(modifier = modifier) {
BoxWithConstraints(
modifier = Modifier
.fillMaxWidth()
) {
val offset = getSliderOffset(
value = value,
valueRange = valueRange,
boxWidth = maxWidth,
labelWidth = labelMinWidth + 8.dp
)
SliderLabel(
value = value.toInt(),
backgroundColor = backgroundColor,
labelColor = labelColor,
cornerRadius = cornerRadius,
elevation = elevation,
if (hasLabel) {
BoxWithConstraints(
modifier = Modifier
.padding(start = offset)
.size(labelSize.dp)
)
.fillMaxWidth()
) {
val offset = getSliderOffset(
value = value,
valueRange = valueRange,
boxWidth = maxWidth,
labelWidth = labelMinWidth + 8.dp
)
SliderLabel(
value = value.toInt(),
backgroundColor = backgroundColor,
labelColor = labelColor,
cornerRadius = cornerRadius,
elevation = elevation,
modifier = Modifier
.padding(
start = offset,
bottom = 4.dp
)
.size(labelSize.dp)
)
}
}
MorphSlider(
MorphSliderBar(
value = value,
onValueChange = onValueChange,
valueRange = valueRange,
Expand All @@ -84,10 +90,12 @@ private fun OptionsSliderLight() {
darkTheme = false,
) {
var value by remember { mutableStateOf(10f) }
SliderWithLabel(
MorphSlider(
value = value,
onValueChange = { value = it },
handleColor = MaterialTheme.colors.primary
handleColor = MaterialTheme.colors.primary,
cornerRadius = 5.dp,
elevation = 10.dp
)
}
}
Expand All @@ -99,10 +107,12 @@ private fun OptionsSliderDark() {
darkTheme = true,
) {
var value by remember { mutableStateOf(10f) }
SliderWithLabel(
MorphSlider(
value = value,
onValueChange = { value = it },
handleColor = MaterialTheme.colors.primary
handleColor = MaterialTheme.colors.primary,
cornerRadius = 5.dp,
elevation = 10.dp
)
}
}
Expand Down
5 changes: 1 addition & 4 deletions neumorph/src/main/kotlin/com/adwi/neumorph/android/Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package com.adwi.neumorph.android

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.adwi.neumorph.android.components.PreviewTemplate
import com.adwi.neumorph.android.theme.MorphUiTheme
import com.adwi.neumorph.android.theme.AppColors
import com.adwi.neumorph.android.components.MorphSlider

private val lightBrush =
Brush.verticalGradient(listOf(AppColors.Light.BackgroundTop, AppColors.Light.BackgroundBottom).reversed())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.adwi.neumorph.android.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.adwi.neumorph.android.theme.AppColors
import com.adwi.neumorph.android.theme.MorphUiTheme

@Composable
Expand All @@ -19,7 +22,22 @@ fun PreviewTemplate(
content: @Composable (content: @Composable () -> Unit) -> Unit,
) {
MorphUiTheme(darkTheme = darkTheme) {
MorphBackground {
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.fillMaxSize()
.background(brush = Brush.verticalGradient(
if (darkTheme)
listOf(
AppColors.Dark.BackgroundTop,
AppColors.Dark.BackgroundBottom
)
else
listOf(
AppColors.Light.BackgroundTop,
AppColors.Light.BackgroundBottom
)
))) {
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier
Expand Down Expand Up @@ -69,7 +87,7 @@ private fun PreviewTemplateDark() {
PreviewTemplate(
darkTheme = true,
) {
PreviewTemplate() {
PreviewTemplate(darkTheme = true) {
it()
}
}
Expand Down

0 comments on commit d3e5177

Please sign in to comment.