Skip to content

Commit

Permalink
Merge pull request #146 from dokar3/dokar/fix-m3-1.3
Browse files Browse the repository at this point in the history
Fix NoSuchMethodError when using material3 v1.3
  • Loading branch information
dokar3 authored Apr 14, 2024
2 parents d821f25 + 4c51d94 commit 955a9bd
Showing 1 changed file with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

package com.dokar.chiptextfield.m3

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.interaction.InteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.material3.TextFieldColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.graphics.Color
import com.dokar.chiptextfield.ChipTextFieldColors

// Copied from material3 v1.2
private const val AnimationDuration = 150

fun TextFieldColors.toChipTextFieldColors(): ChipTextFieldColors {
return object : ChipTextFieldColors {
@Composable
Expand All @@ -17,16 +25,19 @@ fun TextFieldColors.toChipTextFieldColors(): ChipTextFieldColors {
isError: Boolean,
interactionSource: InteractionSource
): State<Color> {
return this@toChipTextFieldColors.textColor(
enabled = enabled,
isError = isError,
interactionSource = interactionSource,
)
val focused by interactionSource.collectIsFocusedAsState()
val targetValue = when {
!enabled -> disabledTextColor
isError -> errorTextColor
focused -> focusedTextColor
else -> unfocusedTextColor
}
return rememberUpdatedState(targetValue)
}

@Composable
override fun cursorColor(isError: Boolean): State<Color> {
return this@toChipTextFieldColors.cursorColor(isError = isError)
return rememberUpdatedState(if (isError) errorCursorColor else cursorColor)
}

@Composable
Expand All @@ -35,11 +46,14 @@ fun TextFieldColors.toChipTextFieldColors(): ChipTextFieldColors {
isError: Boolean,
interactionSource: InteractionSource
): State<Color> {
return this@toChipTextFieldColors.containerColor(
enabled = enabled,
isError = isError,
interactionSource = interactionSource,
)
val focused by interactionSource.collectIsFocusedAsState()
val targetValue = when {
!enabled -> disabledContainerColor
isError -> errorContainerColor
focused -> focusedContainerColor
else -> unfocusedContainerColor
}
return animateColorAsState(targetValue, tween(durationMillis = AnimationDuration))
}
}
}

0 comments on commit 955a9bd

Please sign in to comment.