skip to Main Content

I need to decorate input based on some boolean flag.
If flag is On – I need change edge insets.
If flag is Off – I need no changes and want to use current value of contentPadding (instead of ????)

TextField(
      decoration: InputDecoration(
        contentPadding: someBoolFlagIsOn ? EdgeInsets.zero.copyWith(left: 10) : ????,

How can I do that in ternary operator?

2

Answers


  1. @Artem Zyuzko Please take a moment to review the code below. I think it could be really helpful.

    TextField(
      decoration: InputDecoration(
        contentPadding: someBoolFlagIsOn
            ? EdgeInsets.zero.copyWith(left: 10)
            : InputDecoration().contentPadding,
      ),
    )
    

    If someBoolFlagIsOn is true, it applies EdgeInsets.zero.copyWith(left: 10) as the content padding. If false, it uses the current contentPadding value by creating a new InputDecoration and accessing its contentPadding property.

    Output:

    When someBoolFlagIsOn is true, or in other words, under the condition that someBoolFlagIsOn is true.

    enter image description here

    When someBoolFlagIsOn is not true, or in other words, when it is false.

    enter image description here

    Login or Signup to reply.
  2. You could just assign a different decoration without the contentPadding in that case. So like

          TextField(
              decoration: someBoolFlagIsOn
                  ? InputDecoration(
                      contentPadding: EdgeInsets.zero.copyWith(left: 10))
                  : const InputDecoration()),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search