flutter 弹幕 yzl_flutter_bulletchat的使用

yzl_flutter_bulletchat是一个flutter弹幕库,支持动态弹幕,静态弹幕,支持定义弹幕速度,行数,支持自定义弹幕内容形式。

第一步,构建依赖

dependencies:
  yzl_flutter_bulletchat: ^0.0.5

第二步,导入依赖

import 'package:yzl_flutter_bulletchat/bulletchat.dart';

第三步,使用

import 'package:flutter/material.dart';
import 'package:yzl_flutter_bulletchat/bulletchat.dart';


void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or press Run > Flutter Hot Reload in a Flutter IDE). Notice that the
        // counter didn't reset back to zero; the application is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: Example(),
    );
  }
}

class Example extends StatefulWidget {
  @override
  _ExampleState createState() => _ExampleState();
}

class _ExampleState extends State<Example> {
  /// tip, the type must be [BulletChatState]
  GlobalKey<BulletChatState> globalKey;
  List<int> time = List();

  List<String> string = List();

  List<BulletChatData> list = List();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    buildData();
    globalKey = GlobalKey();
  }

  buildData() {
//    list.add(BulletChatData("1", BulletType.NORMAL, 100));
    for (int i = 0; i < 100; i++) {
      if (i % 3 != 0) {
        list.add(BulletChatData(i.toString(), BulletType.NORMAL, i * 100));
      } else {
        list.add(BulletChatData(i.toString(), BulletType.STATIC, i * 100,
            duration: 5000, center: true));
      }
    }
  }

  double height = 200;
  double width = 200;
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          width: width,
          height: height,
          child: BulletChat.builder(
            ///normal bulletchat,normal means move from right to left until disppear
            ///普通弹幕
            builder: (context, data) {
              return Text(
                (data as BulletChatData).content as String,
                style: TextStyle(color: Colors.orange),
              );
            },
            width: width,
            height: height,
            maxLine: 5,
            globalKey: globalKey,
            data: list,
            option: AvoidOption.AVOIDCOLLISION,
            ///static bullet builder
            ///静态弹幕构建
            staticBulletItemBuilder: (context, data) {
              return Text(
                (data as BulletChatData).content as String,
                style: TextStyle(color: Colors.red),
              );
            },
          ),
        ),

        /// when size changed should call onSizeChanged
        ///屏幕大小变化时需要调用onSizeChanged
        GestureDetector(
          child: Text("test"),
          onTap: () {
            width = 300;
            height = 500;
            globalKey.currentState.onSizeChanged(width: width, height: height);
            setState(() {});
          },
        ),

        /// add a normal bullet
        ///增加一个普通弹幕
        GestureDetector(
          child: Text("test1"),
          onTap: () {
            globalKey.currentState.addBullet(
              Text("this is a test"),
            );
          },
        ),

        /// add a centered static bullet
        ///增加一个静态弹幕,居中
        GestureDetector(
          child: Text("test2"),
          onTap: () {
            globalKey.currentState.addStaticBullet(
                child: Text(
                  "this is a  test1",
                  style: TextStyle(color: Colors.orange),
                ),
                duration: 1000,
                center: true);
          },
        ),

        /// add a static bullet not centered
        ///增加一个静态弹幕,不一定居中
        GestureDetector(
          child: Text("test3"),
          onTap: () {
            globalKey.currentState.addStaticBullet(
                child: Text(
                  "this is a test2",
                  style: TextStyle(color: Colors.orange),
                ),
                duration: 1000,
                margin: EdgeInsets.only(left: 10, top: 10));
          },
        )
      ],
    );
  }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值