GitHub链接git
滑动曝光埋点框架,支持SliverList、SliverGridgithub
滑动曝光埋点用于滑动列表组件中的模块曝光,例如Flutter中的SliverList
、SliverGrid
。 当SliverList
中的某一个行(或列)移动到ViewPort
中,而且显示比例超过必定阈值时,咱们把这个事件记为一次滑动曝光事件。框架
固然咱们对滑动曝光有一些额外的要求:ide
滑动曝光的核心难点是计算组件的露出比例。也是说咱们须要知道ListView
中的组件的总高度
、当前显示高度
。 这两个高度作除法就能够得出比例。布局
组件的总高度能够在renderObject
中获取。咱们能够获取renderObject
下的size
属性,其中包含了组件的长宽。ui
显示高度能够从SliverGeometry.paintExtent
中获取。spa
dependencies:
flutter_sliver_tracker: ^1.0.0
复制代码
import 'package:xm_sliver_listener/flutter_sliver_tracker.dart';
复制代码
ScrollViewListener
捕获滚动事件,ScrollViewListener
必须包裹在CustomScrollView
之上。class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
// 经过ScrollViewListener捕获滚动事件
body: ScrollViewListener(
child: CustomScrollView(
slivers: <Widget>[
],
),
),
);
}
}
复制代码
SliverToBoxAdapter
中监听滚动中止事件,并计算显示比例class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
// 经过ScrollViewListener捕获滚动事件
body: ScrollViewListener(
child: CustomScrollView(
slivers: <Widget>[
SliverToBoxAdapter(
// 监听中止事件,若是在页面上展现比例,能够自行setState
child: SliverEndScrollListener(
onScrollInit: (SliverConstraints constraints, SliverGeometry geometry) {
// 显示高度 / sliver高度
Fluttertoast.showToast(msg: "展现比例:${geometry.paintExtent / geometry.scrollExtent}");
},
onScrollEnd: (ScrollEndNotification notification,
SliverConstraints constraints,
SliverGeometry geometry) {
Fluttertoast.showToast(msg: "展现比例:${geometry.paintExtent / geometry.scrollExtent}");
},
child: Container(
height: 300,
color: Colors.amber,
),
),
),
],
),
),
);
}
}
复制代码
SliverList
和SliverGrid
中监听滚动中止事件,并计算显示比例class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
// 经过ScrollViewListener捕获滚动事件
body: ScrollViewListener(
child: CustomScrollView(
slivers: <Widget>[
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
// 监听滚动中止
return SliverMultiBoxScrollEndListener(
debounce: 1000,
child: Container(
height: 300,
color: Colors.redAccent,
child: Center(
child: Text("SliverList Item", style: TextStyle(fontSize: 30, color: Colors.white))
),
),
onScrollInit: (double itemLength, double displayedLength) {
Fluttertoast.showToast(msg: "显示高度:${displayedLength}");
},
onScrollEnd: (double itemLength, double displayedLength) {
Fluttertoast.showToast(msg: "显示高度:${displayedLength}");
},
);
},
childCount: 1
),
),
],
),
),
);
}
}
复制代码
SliverList
和SliverGrid
中监听滚动更新事件,并计算显示比例class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
// 经过ScrollViewListener捕获滚动事件
body: ScrollViewListener(
child: CustomScrollView(
slivers: <Widget>[
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
// 监听滚动更新事件
return SliverMultiBoxScrollUpdateListener(
onScrollInit: (double percent) {
// percent 列表项显示比例
},
onScrollUpdate: (double percent) {
// percent 列表项显示比例
},
debounce: 1000,
// percent 列表项显示比例
builder: (BuildContext context, double percent) {
return Container(
height: 200,
color: Colors.amber.withAlpha((percent * 255).toInt()),
child: Center(
child: Text("SliverList Item Percent ${percent.toStringAsFixed(2)}", style: TextStyle(fontSize: 30, color: Colors.white))
),
);
},
);
},
childCount: 6
),
),
],
),
),
);
}
}
复制代码