简介
本文介绍怎么在Flutter里使用ListView实现Android的跑马灯,然后再扩展一下,实现上下滚动。
该小控件已经成功上传到pub.dev,安装方式:
dependencies:
flutterswitcher: ^0.0.1
效果图
先上效果图:
垂直模式
水平模式
上代码
主要有两种滚动模式,垂直模式和水平模式,所以我们定义两个构造方法。
参数分别有滚动速度(单位是pixels/second
)、每次滚动的延迟、滚动的曲线变化和children
为空的时候的占位控件。
class Switcher {
const Switcher.vertical({
Key key,
@required this.children,
this.scrollDelta = _kScrollDelta,
this.delayedDuration = _kDelayedDuration,
this.curve = Curves.linearToEaseOut,
this.placeholder,
}) : assert(scrollDelta != null && scrollDelta > 0 && scrollDelta <= _kMaxScrollDelta),
assert(delayDuration != null),
assert(curve != null),
spacing = 0,
_scrollDirection = Axis.vertical,
super(key: key);
const Switcher.horizontal({
Key key,
@required this.children,
this.scrollDelta = _kScrollDelta,
this.delayedDuration = _kDelayedDuration,
this.curve = Curves.linear,