2

I am using compose 1.1.1 in my jetpack compose. I cannot update to latest version. I am want something like this solution. I am getting error on my weight modifier. Can someone guide me how can I get my weight modifier in Row?

implementation "androidx.compose.runtime:runtime:$compose_version"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.foundation:foundation:$compose_version"
implementation "androidx.compose.foundation:foundation-layout:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
androidTestImplementation "androidx.compose.ui:ui-test:$compose_version"
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
implementation "androidx.activity:activity-compose:$compose_version"

Row.kt

Column {
    Row(
        modifier = Modifier.weight(1f, false)
    ) {
        //...
    }
 }

enter image description here

Error

Expression 'weight' cannot be invoked as a function. The function 'invoke()' is not found

Many Thanks

UPDATE

I am adding my whole code here please have a look...

@Composable
fun Input(optionData: OptionData) {
    Column(
        modifier = Modifier
            .fillMaxSize()
    ) {
        Item(optionData)
    }
}


@Composable
fun Item(optionData: OptionData) {
    /// more item of compose i.e. Text, Textfield
    Submit()
}

@Composable
fun Submit() {
    Row(
        modifier = Modifier.weight(1f, false)
    ) {
        //...
    }
}

UPDATE 2

After trying @Thracian solution it solve the problem of weight but my view is not going to bottom.

@Composable
fun Submit() {
    Column {
        OnSubmit {
            PrimaryMaterialButton(text = stringResource(id = R.string.submit)) {

            }
        }
    }
}

@Composable
fun ColumnScope.OnSubmit(content: @Composable () -> Unit) {
    Row(
        modifier = Modifier.weight(1f, false)
    ) {
        content()
    }
}
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
  • `weight` is a modifier you put on a child of a `Row` or `Column`. Is the `Row` you are putting that modifier on in a `Row` or `Column` itself? – ianhanniballake Aug 07 '22 at 21:49
  • @ianhanniballake first is `Column` and the `Row`. – Kotlin Learner Aug 07 '22 at 21:54
  • I updated my question as well to clear that. – Kotlin Learner Aug 07 '22 at 21:54
  • @vivekmodi That error is returned when you are trying to use `Modifier.weight` on a composable that does not have a `Row` or `Column` as a parent. Please show us more of the code structure around that `Row`. If this is inside a composable function that is called inside a `Column` then you have to use the `ColumnScope` as the receiver of the composable function for that to work. – Ma3x Aug 08 '22 at 04:50
  • @Ma3x I added my whole structure. Please have a look into this and guide me. – Kotlin Learner Aug 08 '22 at 08:25

1 Answers1

4
@Composable
fun Submit() {
    Row(
        modifier = Modifier.weight(1f, false)
    ) {
        //...
    }
}

For this code to be able to access Modifier.weight() from Column it should have receiver as ColumnScope. Some modifiers are created with scope so they are available only inside that scope such as Modifier.weight for Row or Column or Modifier.matchParentSize for Box.

@Composable
fun ColumnScope.Submit() {
    Row(
        modifier = Modifier.weight(1f, false)
    ) {
        //...
    }
}


// Allowed
Column {
    Submit()
}

//Not Allowed
Row() {
    Submit()
}
Thracian
  • 43,021
  • 16
  • 133
  • 222
  • one problem I am putting my view bottom of screen but it doesn't work.. – Kotlin Learner Aug 08 '22 at 08:51
  • How does it not work exactly? – Thracian Aug 08 '22 at 08:55
  • can you please check `UPDATE 2` section in my question. – Kotlin Learner Aug 08 '22 at 08:55
  • If your problem was aligning Submit button to bottom then you should add Spacer(modifier=Modifier.weight(1f)) and you don't need to add add weight for your Submit button – Thracian Aug 08 '22 at 08:57
  • When Column or Row measures and places Composables it measures Composables that don't have weight modifiers then allocate space between Composables that have weight modifiers. If only one Composable has weight it occupies space that other Composables that don't occupy. And every child composable is placed same order they are declared with new measured heights – Thracian Aug 08 '22 at 08:59
  • I think I understand that. It's better to open new question for this. – Kotlin Learner Aug 08 '22 at 09:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247114/discussion-between-thracian-and-vivek-modi). – Thracian Aug 08 '22 at 09:10
  • can you help me on this [issue](https://stackoverflow.com/q/73276689/11560810) – Kotlin Learner Aug 08 '22 at 10:57