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

Prashant Priyadarshi

11/28/2020, 2:01 PM
In below code I am getting compiler error " @Composable invocations can only happen from the context of a @Composable function " in placeholder and trailingIcon Any idea why is it so
Copy code
@Composable
  fun inputTextField(value : MutableState<String> = mutableStateOf(""),
          modifier: Modifier = Modifier, onChange : (String)-> Unit= {}) {

    Box(modifier = modifier, children = {
      TextField(
        value = value.value,
        onValueChange = { newValue ->
          value.value = newValue
          onChange(newValue)
        },
        backgroundColor = Color.White,
        modifier = Modifier.fillMaxWidth(0.75f).preferredHeight(35.dp)
          .align(Alignment.CenterStart),
        keyboardType = KeyboardType.Text,
        imeAction = ImeAction.Send,
        onImeActionPerformed = { action: ImeAction,
                                 keyboard: SoftwareKeyboardController? ->
          keyboard?.hideSoftwareKeyboard()
        },

        placeholder = {
          Text(
            text = "some text", color = MaterialDesignColors.grey800,
            modifier = Modifier.align(Alignment.TopStart)
          )
        },

        trailingIcon = {
          if (value.value.isNotBlank())
            Icon(
              asset = Outlined.Close,
              modifier = Modifier.align(Alignment.CenterEnd)
                //   .padding(end = 5.dp)
                .clickable(onClick = {
                  value.value = ""
                  onChange("")
                })
            )
        }
      )
    })
  }
I am also getting this compiler error
Copy code
None of the following functions can be called with the arguments supplied: 
public fun TextField(value: TextFieldValue, onValueChange: (TextFieldValue) -> Unit, modifier: Modifier = ..., textStyle: TextStyle = ..., label: (() -> Unit)? = ..., placeholder: (() -> Unit)? = ..., leadingIcon: (() -> Unit)? = ..., trailingIcon: (() -> Unit)? = ..., isErrorValue: Boolean = ..., visualTransformation: VisualTransformation = ..., keyboardOptions: KeyboardOptions = ..., maxLines: Int = ..., onImeActionPerformed: (ImeAction, SoftwareKeyboardController?) -> Unit = ..., onTextInputStarted: (SoftwareKeyboardController) -> Unit = ..., interactionState: InteractionState = ..., activeColor: Color = ..., inactiveColor: Color = ..., errorColor: Color = ..., backgroundColor: Color = ..., shape: Shape = ...): Unit defined in androidx.compose.material
public fun TextField(value: String, onValueChange: (String) -> Unit, modifier: Modifier = ..., textStyle: TextStyle = ..., label: (() -> Unit)? = ..., placeholder: (() -> Unit)? = ..., leadingIcon: (() -> Unit)? = ..., trailingIcon: (() -> Unit)? = ..., isErrorValue: Boolean = ..., visualTransformation: VisualTransformation = ..., keyboardOptions: KeyboardOptions = ..., maxLines: Int = ..., onImeActionPerformed: (ImeAction, SoftwareKeyboardController?) -> Unit = ..., onTextInputStarted: (SoftwareKeyboardController) -> Unit = ..., interactionState: InteractionState = ..., activeColor: Color = ..., inactiveColor: Color = ..., errorColor: Color = ..., backgroundColor: Color = ..., shape: Shape = ...): Unit defined in androidx.compose.material
s

spierce7

11/28/2020, 2:46 PM
If you do the triple ‘`’ surrounding multi-line code, it’s a bit more readable in Slack.
👍 1
a

Alexander Sitnikov

11/28/2020, 6:30 PM
Replace parameters
keyboardType
and
imeAction
with single parameter
keyboardOptions
. These parameters were recently extracted to new
KeyboardOptions
class
Copy code
keyboardOptions = KeyboardOptions(
    keyboardType = KeyboardType.Text,
    imeAction = ImeAction.Send,
),
👍 1
3 Views