dispatchFunction method

  1. @override
void dispatchFunction(
  1. ScrollViewRenderViewModel viewModel,
  2. String functionName,
  3. VoltronArray array, {
  4. Promise? promise,
})
override

Implementation

@override
void dispatchFunction(
  ScrollViewRenderViewModel viewModel,
  String functionName,
  VoltronArray array, {
  Promise? promise,
}) {
  super.dispatchFunction(
    viewModel,
    functionName,
    array,
    promise: promise,
  );
  // 滚动事件
  if (functionName == kFuncScrollTo) {
    // 先确定滚动方向
    var orientation = 'vertical';
    if (viewModel.isHorizontal) {
      orientation = 'horizontal';
    }
    // 再确定滚动值
    final destX = array.get<double>(0) ?? array.get<int>(0)?.toDouble() ?? 0.0;
    final destY = array.get<double>(1) ?? array.get<int>(1)?.toDouble() ?? 0.0;
    final animated = array.get<bool>(2) ?? true;
    var offset = destY;
    if (orientation == 'horizontal') {
      offset = destX;
    }
    viewModel.scrollTo(offset, animated ? 1000 : 0);
  } else if (functionName == kFuncScrollToWithOptions) {
    // 先确定滚动方向
    var orientation = 'vertical';
    if (viewModel.isHorizontal) {
      orientation = 'horizontal';
    }
    // 再确定滚动值
    final m = array.get(0);
    if (m is VoltronMap) {
      final destX = m.get<double>("x") ?? m.get<int>("x")?.toDouble() ?? 0.0;
      final destY = m.get<double>("y") ?? m.get<int>("y")?.toDouble() ?? 0.0;
      var offset = destY;
      if (orientation == 'horizontal') {
        offset = destX;
      }
      // 最后确定是否有动画
      final duration = m.get("duration") ?? 0;
      var d = 0;
      if (duration is int && duration > 0) {
        d = duration;
      }
      viewModel.scrollTo(offset, d);
    }
  }
}