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