gpt4 book ai didi

flutter - 如何使用 Flutter 实现这个特定的滚动动画?

转载 作者:行者123 更新时间:2023-12-05 04:56:37 25 4
gpt4 key购买 nike

我正在尝试实现这个滚动动画(下面的 GIF)。但是,在网上广泛搜索之后,我还没有找到任何关于这个问题的具体信息。我查看了 ScrollPhysics 类,但我认为这不是正确的路径。我的想法是在小部件之间使用透明分隔符,然后更改每个分隔符的高度以创建所需的“错觉”。我将不胜感激任何关于如何实现这件事的建议。

enter image description here

最佳答案

更新:

这是 DartPad 上的完整示例,您可以自己尝试: https://dartpad.dev/26671eeec673a663b26c5dda68c934c2

原创:

这是一个关于如何使用 NotificationListener 处理此问题的简单示例:

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

class _ExampleState extends State<Example> {
double spaceBetween = 10.0;
final _duration = Duration(milliseconds: 200);


_onStartScroll(ScrollMetrics metrics) {
// if you need to do something at the start
}

_onUpdateScroll(ScrollMetrics metrics) {
// do your magic here to change the value
if(spaceBetween == 30.0) return;
spaceBetween = 30.0;
setState((){});
}

_onEndScroll(ScrollMetrics metrics) {
// do your magic here to return the value to normal
spaceBetween = 10.0;
setState((){});
}

@override
Widget build(BuildContext context) {
return NotificationListener<ScrollNotification>(
onNotification: (scrollNotification) {
if (scrollNotification is ScrollStartNotification) {
_onStartScroll(scrollNotification.metrics);
} else if (scrollNotification is ScrollUpdateNotification) {
_onUpdateScroll(scrollNotification.metrics);
} else if (scrollNotification is ScrollEndNotification) {
_onEndScroll(scrollNotification.metrics);
}
return true; // see docs
},
child: ListView(
children: [
Container(height: 100, width: 100, color: Colors.red),
AnimatedContainer(duration: _duration, height: spaceBetween),
Container(height: 100, width: 100, color: Colors.blue),
AnimatedContainer(duration: _duration, height: spaceBetween),
Container(height: 100, width: 100, color: Colors.red),
AnimatedContainer(duration: _duration, height: spaceBetween),
Container(height: 100, width: 100, color: Colors.blue),
AnimatedContainer(duration: _duration, height: spaceBetween),
Container(height: 100, width: 100, color: Colors.red),
AnimatedContainer(duration: _duration, height: spaceBetween),
Container(height: 100, width: 100, color: Colors.blue),
AnimatedContainer(duration: _duration, height: spaceBetween),
Container(height: 100, width: 100, color: Colors.red),
AnimatedContainer(duration: _duration, height: spaceBetween),
Container(height: 100, width: 100, color: Colors.blue),
AnimatedContainer(duration: _duration, height: spaceBetween),
Container(height: 100, width: 100, color: Colors.red),
AnimatedContainer(duration: _duration, height: spaceBetween),
Container(height: 100, width: 100, color: Colors.blue),
AnimatedContainer(duration: _duration, height: spaceBetween),
],
),
);
}
}

我在这里使用了 AnimatedContainer,但如果您愿意,也可以使用显式动画。

关于flutter - 如何使用 Flutter 实现这个特定的滚动动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64826393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com