前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Flutter 实现虎牙/斗鱼 弹幕功能

Flutter 实现虎牙/斗鱼 弹幕功能

作者头像
砸漏
发布2020-10-20 15:38:15
9340
发布2020-10-20 15:38:15
举报
文章被收录于专栏:恩蓝脚本恩蓝脚本

用Flutter实现弹幕功能,轻松实现虎牙、斗鱼的弹幕效果。

先来一张效果图:

实现原理

弹幕的实现原理非常简单,即将一条弹幕从左侧平移到右侧,当然我们要计算弹幕垂直方向上的偏移,不然所有的弹幕都会在一条直线上,相互覆盖。平移代码如下:

代码语言:javascript
复制
@override
void initState() {
 _animationController =
  AnimationController(duration: widget.duration, vsync: this)
 ..addStatusListener((status){
 if(status == AnimationStatus.completed){
  widget.onComplete('');
 }
 });
 var begin = Offset(-1.0, .0);
 var end = Offset(1.0, .0);
 
 _animation = Tween(begin: begin, end: end).animate(_animationController);
 //开始动画
 _animationController.forward();
 super.initState();
}

@override
 Widget build(BuildContext context) {
 return SlideTransition(
  position: _animation,
  child: widget.child,
 );
 }

计算垂直方向的偏移:

代码语言:javascript
复制
_computeTop(int index, double perRowHeight) {
 //第几轮弹幕
 int num = (index / widget.showCount).floor();
 var top;
 top = (index % widget.showCount) * perRowHeight + widget.padding;

 if (num % 2 == 1 && index % widget.showCount != widget.showCount - 1) {
 //第二轮在第一轮2行弹幕中间
 top += perRowHeight / 2;
 }
 if (widget.randomOffset != 0 && top   widget.randomOffset) {
 top += _random.nextInt(widget.randomOffset) * 2 - widget.randomOffset;
 }
 return top;
}

这些准备好后,就是创建一条弹幕了,现创建一条最简单的文字弹幕:

代码语言:javascript
复制
Text(
 text,
 style: TextStyle(color: Colors.white),
);

效果如下:

创建一条VIP用户的弹幕,其实就是字体变下颜色:

代码语言:javascript
复制
Text(
 text,
 style: TextStyle(color: Color(0xFFE9A33A)),
)

效果如下:

给文字加个圆角背景:

代码语言:javascript
复制
return Center(
 child: Container(
 padding: EdgeInsets.only(left: 10, right: 10, top: 3, bottom: 3),
 decoration: BoxDecoration(
  color: Colors.red.withOpacity(.8),
  borderRadius: BorderRadius.circular(50)),
 child: Text(
  text,
  style: TextStyle(color: Colors.white),
 ),
 ),
);

效果如下:

创建一个送火箭的弹幕:

代码语言:javascript
复制
return Center(
 child: Container(
 padding: EdgeInsets.only(left: 10, right: 10, top: 3, bottom: 3),
 decoration: BoxDecoration(
  color: Colors.red.withOpacity(.8),
  borderRadius: BorderRadius.circular(50)),
 child: Row(
  mainAxisSize: MainAxisSize.min,
  children: <Widget [
  Text(
   text,
   style: TextStyle(color: Colors.white),
  ),
  Image.asset('assets/images/timg.jpeg',height: 30,width: 30,),
  Text(
   'x $count',
   style: TextStyle(color: Colors.white, fontSize: 18),
  ),
  ],
 ),
 ),
);

效果如下:

火箭有点丑了,不过这不是重点。

其实实现弹幕效果没有我开始想的那么简单,过程中也遇到了一些问题,不过好在最终都解决了,献上Github地址:https://github.com/781238222/flutter-do/tree/master/flutter_barrage_sample

如果觉得还不错,给个小小的赞。

交流

Github地址:https://github.com/781238222/flutter-do

170+组件详细用法:http://laomengit.com

总结

到此这篇关于Flutter 实现虎牙/斗鱼 弹幕功能的文章就介绍到这了,更多相关Flutter 实现虎牙斗鱼 弹幕内容请搜索ZaLou.Cn以前的文章或继续浏览下面的相关文章希望大家以后多多支持ZaLou.Cn!

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档