jaqxues
09/02/2020, 12:23 PMTimo Drick
09/02/2020, 12:30 PMjaqxues
09/02/2020, 12:30 PM@Composable
@OptIn(ExperimentalMaterialApi::class)
fun Switch(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
color: Color = MaterialTheme.colors.secondaryVariant
) {
val minBound = 0f
val maxBound = with(DensityAmbient.current) { ThumbPathLength.toPx() }
val swipeableState = rememberSwipeableStateFor(checked, onCheckedChange, AnimationSpec)
val interactionState = remember { InteractionState() }
val isRtl = LayoutDirectionAmbient.current == LayoutDirection.Rtl
Stack(
modifier
.toggleable(
value = checked,
onValueChange = onCheckedChange,
enabled = enabled,
interactionState = interactionState,
indication = null
)
.swipeable(
state = swipeableState,
anchors = mapOf(minBound to false, maxBound to true),
thresholds = { _, _ -> FractionalThreshold(0.5f) },
orientation = Orientation.Horizontal,
enabled = enabled,
reverseDirection = isRtl,
interactionState = interactionState,
resistanceFactorAtMin = 0f,
resistanceFactorAtMax = 0f
)
.padding(DefaultSwitchPadding)
) {
SwitchImpl(
checked = checked,
enabled = enabled,
checkedColor = color,
thumbValue = swipeableState.offset,
interactionState = interactionState
)
}
}
Zach Klippenstein (he/him) [MOD]
09/02/2020, 12:37 PMjaqxues
09/02/2020, 12:38 PMAndroid Public Tracker > App Development > Jetpack Compose > UI Libraries > Material
i suppose?matvei
09/02/2020, 12:51 PMModifier.size
on the Switch?jaqxues
09/02/2020, 12:54 PMmatvei
09/02/2020, 12:55 PMjaqxues
09/02/2020, 12:56 PMvar isActive by remember { mutableStateOf(true) }
Switch(isActive, onCheckedChange = { isActive = it }, color = Color(0xFF00AA00), modifier = Modifier.size(48.dp).padding(horizontal = 16.dp))
matvei
09/02/2020, 12:57 PMjaqxues
09/02/2020, 12:57 PMmatvei
09/02/2020, 12:59 PMvar isActive by remember { mutableStateOf(true) }
Switch(isActive, onCheckedChange = { isActive = it }, color = Color(0xFF00AA00), modifier = Modifier.size(48.dp))
This should result with proper switch, clickable within all 48dps and centered within this 48.dp boxjaqxues
09/02/2020, 1:06 PMmatvei
09/02/2020, 1:14 PMjaqxues
09/02/2020, 1:27 PMmatvei
09/02/2020, 1:38 PMjaqxues
09/02/2020, 1:39 PMmatvei
09/07/2020, 10:19 AM