我有一个容器,我在其上检测手势,以便我可以滚动浏览我使用自定义画家创建的图像,就像这样
class CustomScroller extends StatefulWidget {
@override
_CustomScrollerState createState() => _CustomScrollerState();
}
class _CustomScrollerState extends State<CustomScroller>
with SingleTickerProviderStateMixin {
AnimationController _controller;
double dx = 0;
Offset velocity = Offset(0, 0);
double currentLocation = 0;
@override
void initState() {
_controller =
AnimationController(vsync: this, duration: Duration(seconds: 1));
super.initState();
}
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
double width = MediaQuery.of(context).size.width;
return GestureDetector(
onHorizontalDragUpdate: (DragUpdateDetails dragUpdate) {
dx = dragUpdate.delta.dx;
currentLocation =
currentLocation + (dx * 10000) / (width * _absoluteRatio);
setState(() {});
},
onHorizontalDragEnd: (DragEndDetails dragUpdate) {
velocity = dragUpdate.velocity.pixelsPerSecond;
//_runAnimation(velocity, size);
},
child: Container(
width: width,
height: 50,
child: Stack(
children: <Widget>[
CustomPaint(
painter: NewScrollerPainter(1, true, currentLocation),
size: Size.fromWidth(width)),
],
),
),
);
}
}
我编写了一些代码,以便在拖动时使用指针的 dx 值来计算 currentLocation,customPainter 'NewScrollerPaint' 使用它来确定它需要绘制的区域。我本质上想要一个投掷动画,即从一个速度的拖动中释放,自定义画家“NewScrollerPainter”将继续滚动,直到它失去动力。我相信这需要从速度计算 currentLocation 的值,然后返回以更新 NewScrollerPainter,但我花了几个小时查看 Flutter 的动画文档并玩弄开源代码,但我仍然没有确定我将如何去做。任何人都可以对这个问题有所了解吗?