EloBalans
EloBalans

Reputation: 1

This If statement not working as expected

My if statement isn't working as expected. Any suggestion how to fix it?

child: RaisedButton(
  color: Colors.green[500],
  if(resources<resToUpabilityOne){
    onPressed:(){
      setStateContext(() {
        resources -= resToUpabilityThree;
        lvl_abilityThree +=1;
        intrest+=lvl_abilityThree;
        resToUpabilityThree=resToUpabilityThree*2;});},
  }else{
    onPressed:(){
    null;
  },
}

Upvotes: 0

Views: 61

Answers (2)

Robert Sandberg
Robert Sandberg

Reputation: 8635

You cannot have an if-statement where the constructor expects a named argument.

Try just moving the if-statement down a row.

RaisedButton(
  color: Colors.green[500],
  onPressed: () {
  if(resources<resToUpabilityOne) {
    setStateContext(() {
      resources -= resToUpabilityThree;
      lvl_abilityThree +=1;
      intrest+=lvl_abilityThree;
      resToUpabilityThree=resToUpabilityThree*2;});
      },
  },

If you want to disable the button in case of that condition, you can try something like this:

child: RaisedButton(
  color: Colors.green[500],
  onPressed: (resources < resToUpabilityOne)
      ? () {
          setStateContext(() {
            resources -= resToUpabilityThree;
            lvl_abilityThree += 1;
            intrest += lvl_abilityThree;
            resToUpabilityThree = resToUpabilityThree * 2;
          });
        }
      : null,
),

Upvotes: 1

J. S.
J. S.

Reputation: 9635

You can't put an if condition in the middle of your Widget properties. To do what you want to do, move the condition to an onPressed method and then call that method on the onPressed call of the widget:

RaisedButton(
  color: Colors.green[500],
  onPressed: () => onPressed()

  ...
void onPressed() {
  if(resources<resToUpabilityOne){
    setStateContext(() {
      resources -= resToUpabilityThree;
      lvl_abilityThree +=1;
      intrest+=lvl_abilityThree;
      resToUpabilityThree=resToUpabilityThree*2;
    });
  },
}

Upvotes: 0

Related Questions