https://kotlinlang.org logo
#compose
Title
# compose
e

enighma

08/22/2022, 6:15 PM
I have a vague memory that it used to be possible to have a composable as an extension function, but now I get a warning that it's not(might've never worked). Here's a sample situation of what I naively want:
@Composable LazyGridScope.myFun() = ...
Is there a workaround or suggested solution for this? I guess I can pass it as a param and run
scope.apply{}
.
a

Alex Vanyo

08/22/2022, 6:18 PM
I think this might be a case of needing to know more what you’re trying to achieve? Specifically for the `Lazy*Scope`s, extensions on those probably shouldn’t be composable directly.
e

enighma

08/22/2022, 6:20 PM
I have a
LazyGrid
and I want to encapsulate each row into a composable, but maybe I'm thinking of it the wrong way and each item instead should be extracted.
I can get what I want with a regular function that looks like this
LazyGridScope.myFun()
I just feel I have to jump through a few more hoops than I expected
What is interesting is that it works with some scopes but not others. For instance I can do exactly what I want above with a
Column
a

Alex Vanyo

08/22/2022, 7:19 PM
Lazy
is a bit of a special case:
Copy code
// @Composable
LazyColumn {
    // not @Composable
    item {
        // @Composable
        Text(...)
    }
}
The nice lambdas hide the types and obscure what’s going on a bit. Right inside
LazyColumn
is setting up a DSL for which items to (potentially) emit. So each
item
is defining a part of the lazy content, but since it’s lazy, it may not actually get executed. So when you define an extension on
LazyGridScope
, you’re saying “here’s a portion of content to contribute to the DSL of items to show,” as opposed to saying “here’s content to directly place into the scope” The latter case makes sense with a
ColumnScope
, since you are directly adding normal composables.
e

enighma

08/22/2022, 8:18 PM
I see, so it's a special case then. On the topic of
LazyGrid
is there a mechanism to align content of a cell? I did not find any info in the doc nor looking at the API itself.
To put it in other words: If I call
fillMaxSize()
the composable is still not not filling up its cell, and that is fine if I at least could align it, within it.
s

Stylianos Gakis

08/22/2022, 9:51 PM
3 Views