addDanmaku method

AddBulletResBody addDanmaku(
  1. String text, {
  2. FlutterDanmakuBulletType bulletType = FlutterDanmakuBulletType.scroll,
  3. Color? color,
  4. Widget builder(
    1. Text
    )?,
  5. int? offsetMS,
  6. FlutterDanmakuBulletPosition position = FlutterDanmakuBulletPosition.any,
})

Implementation

AddBulletResBody addDanmaku(String text,
    {FlutterDanmakuBulletType bulletType = FlutterDanmakuBulletType.scroll,
    Color? color,
    Widget Function(Text)? builder,
    int? offsetMS,
    FlutterDanmakuBulletPosition position =
        FlutterDanmakuBulletPosition.any}) {
  assert(text.isNotEmpty);
  // 先获取子弹尺寸
  Size bulletSize = FlutterDanmakuUtils.getDanmakuBulletSizeByText(text);
  // 寻找可用的轨道
  FlutterDanmakuTrack? track = _findAvailableTrack(bulletSize,
      bulletType: bulletType, position: position, offsetMS: offsetMS);
  // 如果没有找到可用的轨道
  if (track == null) {
    return AddBulletResBody(
      AddBulletResCode.noSpace,
    );
  }
  FlutterDanmakuBulletModel bullet = _bulletManager.initBullet(
      text, track.id, bulletSize, track.offsetTop,
      prevBulletId: track.lastBulletId,
      position: position,
      bulletType: bulletType,
      color: color,
      builder: builder,
      offsetMS: offsetMS);
  if (bulletType == FlutterDanmakuBulletType.scroll) {
    track.lastBulletId = bullet.id;
  } else {
    // 底部弹幕 不记录到轨道上
    // 查询是否可注入弹幕时 底部弹幕 和普通被注入到底部的静止弹幕可重叠
    if (position == FlutterDanmakuBulletPosition.any) {
      track.loadFixedBulletId(bullet.id);
    }
  }
  return AddBulletResBody(AddBulletResCode.success, data: bullet.id);
}