addBarrage method

dynamic addBarrage(
  1. Widget child, {
  2. Duration duration = _kDuration,
})

添加弹幕

Implementation

addBarrage(Widget child, {Duration duration = _kDuration}) {
  double perRowHeight = (_height - 2 * widget.padding) / widget.showCount;
  //计算距离顶部的偏移,
  // 不直接使用_barrageList.length的原因:弹幕结束会删除列表中此项,如果
  // 此时正好有一个弹幕来,会造成此弹幕和上一个弹幕同行
  var index = 0;
  if (_barrageList.isEmpty) {
    //屏幕中没有弹幕,从顶部开始
    index = 0;
    barrageIndex++;
  } else {
    index = barrageIndex++;
  }
  var top = _computeTop(index, perRowHeight);
  if (barrageIndex > 100000) {
    //避免弹幕数量一直累加超过int的最大值
    barrageIndex = 0;
  }
  //给每一项生成一个唯一id,用于删除
  String id = '${DateTime.now().toIso8601String()}:${_random.nextInt(1000)}';
  var item = BarrageTransitionItem(
    id: id,
    top: top,
    child: child,
    onComplete: _onComplete,
    duration: duration,
  );
  _barrageList.add(item);
  setState(() {});
}