Tobias Gronbach
02/22/2022, 1:01 PMval dialogModifier = Modifier
.padding(10.dp)
.background(MaterialTheme.colors.surface)
.width(400.dp)
But then build is failing with: @Composable invocations can only happen from the context of a @Composable function . What am I doing wrong?Alexander Maryanovsky
02/22/2022, 1:05 PMMaterialTheme.colors
can only be dereferenced inside a @Composable
function.10.dp
, it’s composition-dependent. So you can’t just use it.@Composable
fun dialogModifier() = ...
Tobias Gronbach
02/22/2022, 1:18 PMAlexander Maryanovsky
02/22/2022, 1:29 PMAndrew Neal
02/22/2022, 1:30 PMAlexander Maryanovsky
02/22/2022, 1:31 PMTobias Gronbach
02/22/2022, 1:35 PMmattinger
02/22/2022, 2:18 PMcomposed
function. There's subtle differences in using this as opposed to just marking a function with @Composable.
If you mark your function with @Composable as @Alexander Maryanovsky suggested, any composable references will be evaluated at the time of the function invocation. If you use the composed
function, it happens a bit later in the recomposition. The official description:
Declare a just-in-time composition of a Modifier that will be composed for each element it modifies. composed may be used to implement stateful modifiers that have instance-specific state for each modified element, allowing the same Modifier instance to be safely reused for multiple elements while maintaining element-specific state.
https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier#(androidx.compose.ui.Modifier).com[…](kotlin.Function1,kotlin.Function1)Adam Powell
02/22/2022, 3:47 PMModifier.composed
is the way to create a modifier factory that will be invoked at each usage site to preserve this reusability by giving you a place to create and remember
point of use instance-specific information.Alexander Maryanovsky
02/22/2022, 4:01 PMAdam Powell
02/22/2022, 4:24 PM@Composable
function (including lambdas) is a factory for instances of that content in a composition. They can be reused in multiple places and keep instance-specific state. Modifier.composed {}
gives that same capability to modifiers and lets you assemble them into a chain with other modifiers like you would expect