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

标签 flutter dart user-experience

我正在尝试实现这个滚动动画(下面的 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/

相关文章:

javascript - 最终用户体验监控工具

user-experience - 你如何找出用户真正想要什么?

flutter - 错误 : The non-nullable local variable 'newTaskTitle' must be assigned before it can be used

json - 用flutter处理json中的空值

flutter - AnimatedContainer-如何只展开一个,其余的留在下面?

oop - Dart:泛型抽象静态方法

android - Flutter 文本按钮波纹效果

html - 隐藏/显示 div 是响应式设计的最佳实践吗?

flutter - 如何以简单的方式在 pubspec.yaml 中添加 100 多个手动图像?

flutter - 如何在flutter中的showModalBottomSheet中从CupertinoDatePicker返回数据?