transitionEval function

Object transitionEval(
  1. int durationMillis,
  2. RatioEvaluator evaluate,
  3. {GSBuildContext bindContext,
  4. int refreshPeriodicityMillis,
  5. int delayMillis = 0,
  6. int repeatAfterMillis,
  7. Object key,
  8. Object tag}
)

Creates a transition with a computed value. Returns its key.

Cannot be used within GS widgets build methods.

durationMillis and evaluate must not be null.

The value is computed on every update by evaluating the progress ratio on evaluate.

If key is not provided, a unique key is generated.

Refer to transition for further description about the parameters.

Example:

class MyWidget extends StatelessWidget with GS {
  ...
  static int _clicks = 0;

  @override
  Widget build(BuildContext context, MyButtonState state) {
    int t = (transitionOf(#key) ?? 0.0).toInt();
    return ...
        Text('Total clicks is... $t'),
        ...
        ...onPressed: () {
             final clicks = _clicks++;
             TransitionGroup(key: #key).cancel();
             transitionEval(5000, (r) => r * clicks, key: #key,
                 bindContext: bindContext, repeatAfterMillis: 1000);
             }
           },
        ...
    ...
  }
}

In the example above when there is a click event, a new repeating transition is created whose evaluate function scales the value to the current number of clicks. On every click event the evaluate function changes (because clicks increases), therefore the old transitions is canceled in order to register a new one.

Implementation

Object transitionEval(
  int durationMillis,
  RatioEvaluator evaluate, {
  GSBuildContext bindContext,
  int refreshPeriodicityMillis,
  int delayMillis = 0,
  int repeatAfterMillis,
  Object key,
  Object tag,
}) {
  assert(() {
    if (GSController.isListening) {
      throw gsError(
          'Cannnot invoke [transitionEval] while a GS widget is building. '
          'Use instead `evaluate(transition(...))`.');
    }
    if (durationMillis == null || evaluate == null) {
      throw gsError('Bad inputs for [transitionEval]; durationMillis and '
          'evaluate cannot be null.');
    }
    return true;
  }());
  _Transition transitionState;
  if (key != null) {
    transitionState = _Registry.getForKey(key);
  }
  key ??= UniqueKey();
  if (transitionState == null) {
    transitionState = _TransitionEval(
        key,
        durationMillis,
        evaluate,
        refreshPeriodicityMillis,
        delayMillis,
        repeatAfterMillis,
        tag,
        bindContext);
  }
  return key;
}