dazza5000
04/14/2022, 5:18 PMAdam Powell
04/14/2022, 5:34 PMdazza5000
04/14/2022, 5:34 PMAdam Powell
04/14/2022, 5:41 PMKirill Grouchnikov
04/14/2022, 5:47 PMAdam Powell
04/14/2022, 5:47 PMModifier.layout
instead of defaultMinSize
so that you can work in pixels rather than having to consult LocalDensity
, then you could factor out an explicit hoisted state object to hold the max, passing that same hoisted state object any time you use the modifier. Custom modifiers are written using extension functions, so you could do
fun Modifier.myJumpyHeight(state: MyJumpyHeightState) = onSizeChanged { ... }.layout { ... }
dazza5000
04/14/2022, 5:50 PMAdam Powell
04/14/2022, 5:50 PMdazza5000
04/14/2022, 5:51 PMremembered
when definedAdam Powell
04/14/2022, 5:54 PMby mutableStateOf
, etc. but other than that, nothing specialdazza5000
04/14/2022, 6:20 PMfun Modifier.myJumpyHeight(state: MyJumpyHeightState, density: Density) = onSizeChanged { size ->
val itemHeight = with(density) {
val height = size.height
height.toDp()
}
if (itemHeight > state.minHeight ?: 0.dp) {
state.minHeight = itemHeight
}
}.defaultMinSize(minHeight = state.minHeight ?: Dp.Unspecified)
data class MyJumpyHeightState(var minHeight: Dp? = null)
val density = LocalDensity.current
var myJumpyHeightState by remember { mutableStateOf(MyJumpyHeightState()) }
LazyRow
Adam Powell
04/14/2022, 6:22 PMvar
in a data class
constructor param, compose or no compose; it means you have unstable equality and hash codes over time. The former you can rationalize in some circumstances, the latter is a Problem(TM) if someone ever uses one as the key in a mapclass MyJumpyHeightState(minHeight: Int = 0) {
var minHeight by mutableStateOf(minHeight)
}
val myJumpyHeightState = remember { MyJumpyHeightState() }
instead of the external mutableStateOf
dazza5000
05/02/2023, 2:30 PM