0

I want to change the color of the text displaying the scheduled time, if the scheduled time is past the current time then it should change to red and if its in the future then it wont change. This works properly but it only changes state when I click on another button. I am using a ternary operator like this:

color: (run(todoController.todos[index].date,
            todoController.todos[index].time)
            .compareTo(tz.TZDateTime.now(tz.local))>0)
            ? Theme.of(context).hintColor
            : Colors.redAccent,

How do you add setState in a ternary operator? Thanks

Rohith Nambiar
  • 2,957
  • 4
  • 17
  • 37

1 Answers1

1

I think the neater solution would be moving the logic of the comparison of time in a separate function which will be called each 2/5 second depending on your app. In that function, you can change the state and make sure to use color depending on the state.

The code for checking the future time each minute

var cron = new Cron();
cron.schedule(new Schedule.parse('*/3 * * * *'), () async {
  if(run(todoController.todos[index].date,
        todoController.todos[index].time)
        .compareTo(tz.TZDateTime.now(tz.local))>0){
    setState(() {
      isFutureTime = true;
    });
  }
});

I have used corn. CORN documentation Your Widget color code would look like this.

isFutureTime == false? Theme.of(context).hintColor : Colors.redAccent,

isFutureTime is the state boolean variable. You can follow this StackOverflow answer on changing the state periodically.