如何使用 Flutter 实现这个特定的滚动动画?
How can I implement this specific scroll animation using Flutter?
我正在尝试实现这个滚动动画(下面的 GIF)。但是,在网上广泛搜索之后,我还没有找到任何关于这个问题的具体信息。我查看了 ScrollPhysics
class 但我认为那不是正确的路径。我的想法是在小部件之间使用透明分隔符,然后更改每个分隔符的高度以创建所需的“错觉”。我将不胜感激任何关于如何实现这个东西的建议。
更新:
以下是 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,但如果您愿意,也可以使用显式动画。
我正在尝试实现这个滚动动画(下面的 GIF)。但是,在网上广泛搜索之后,我还没有找到任何关于这个问题的具体信息。我查看了 ScrollPhysics
class 但我认为那不是正确的路径。我的想法是在小部件之间使用透明分隔符,然后更改每个分隔符的高度以创建所需的“错觉”。我将不胜感激任何关于如何实现这个东西的建议。
更新:
以下是 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,但如果您愿意,也可以使用显式动画。