Flutter如何實(shí)現(xiàn)虎牙/斗魚彈幕功能

這篇文章主要為大家展示了Flutter如何實(shí)現(xiàn)虎牙/斗魚彈幕功能,內(nèi)容簡而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來看看吧。

禹會(huì)網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,禹會(huì)網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為禹會(huì)成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個(gè)售后服務(wù)好的禹會(huì)做網(wǎng)站的公司定做!

先來一張效果圖:

Flutter如何實(shí)現(xiàn)虎牙/斗魚彈幕功能

實(shí)現(xiàn)原理

彈幕的實(shí)現(xiàn)原理非常簡單,即將一條彈幕從左側(cè)平移到右側(cè),當(dāng)然我們要計(jì)算彈幕垂直方向上的偏移,不然所有的彈幕都會(huì)在一條直線上,相互覆蓋。平移代碼如下:

@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);
 //開始動(dòng)畫
 _animationController.forward();
 super.initState();
}

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

計(jì)算垂直方向的偏移:

_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;
}

這些準(zhǔn)備好后,就是創(chuàng)建一條彈幕了,現(xiàn)創(chuàng)建一條最簡單的文字彈幕:

Text(
 text,
 style: TextStyle(color: Colors.white),
);

效果如下:

Flutter如何實(shí)現(xiàn)虎牙/斗魚彈幕功能

創(chuàng)建一條VIP用戶的彈幕,其實(shí)就是字體變下顏色:

Text(
 text,
 style: TextStyle(color: Color(0xFFE9A33A)),
)

效果如下:

Flutter如何實(shí)現(xiàn)虎牙/斗魚彈幕功能

給文字加個(gè)圓角背景:

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),
 ),
 ),
);

效果如下:

Flutter如何實(shí)現(xiàn)虎牙/斗魚彈幕功能

創(chuàng)建一個(gè)送火箭的彈幕:

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),
  ),
  ],
 ),
 ),
);

效果如下:

Flutter如何實(shí)現(xiàn)虎牙/斗魚彈幕功能

火箭有點(diǎn)丑了,不過這不是重點(diǎn)。

以上就是關(guān)于Flutter如何實(shí)現(xiàn)虎牙/斗魚彈幕功能的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。

當(dāng)前標(biāo)題:Flutter如何實(shí)現(xiàn)虎牙/斗魚彈幕功能
鏈接分享:http://muchs.cn/article16/jpiogg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站標(biāo)簽優(yōu)化、ChatGPT營銷型網(wǎng)站建設(shè)、電子商務(wù)手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)