【问题标题】:Slide-up screen change animation上滑换屏动画
【发布时间】:2019-07-30 10:00:36
【问题描述】:

我是 Flutter 开发的新手,我想在换屏时创建这种动画(第一页也向上滑动)。我搜索了有关此的教程和帖子,但没有找到。有没有办法做到这一点?

【问题讨论】:

  • 应该可以的; Flutter 非常专注于 UI。看看这个link

标签: flutter flutter-animation


【解决方案1】:

这是工作代码。您可以尝试更改持续时间和值 from 以进行反向动画。但目前看来效果还不错

class _MyWidgetState extends State<MyWidget>
    with SingleTickerProviderStateMixin {

  AnimationController _animationController;

  @override
  void initState() {
    _animationController = AnimationController(vsync: this, duration: Duration(milliseconds: 2000)); // you can try to set another duration
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    _animationController.reverse(from: 0.2); // you can try to set another value for from
    return SlideTransition(
      position: Tween<Offset>(
        begin: Offset.zero,
        end: const Offset(0.0, -1.0),
      ).animate(_animationController),
      child: Scaffold(
        appBar: AppBar(
          title: Text('MyWidget'),
        ),
        body: _createBody(),
      ));
  }

  Widget _createBody() {
    // create body here
    // perform this action on click:
    Navigator.of(context).push(getRoute());
  }

  PageRoute getRoute() {
    _animationController.forward(from: 0.0);
    return PageRouteBuilder(
      pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
        return MySecondScreen();
      },
      transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
        return SlideTransition(
          position: Tween<Offset>(
            begin: const Offset(0.0, 1.0),
            end: Offset.zero,
          ).animate(animation),
          child: SlideTransition(
            position: Tween<Offset>(
              begin: Offset.zero,
              end: const Offset(0.0, 1.0),
            ).animate(secondaryAnimation),
            child: child,
          ),
        );
      },
    );
  }

  dispose() {
    super.dispose();
    _animationController.dispose();
  }
}

【讨论】:

  • 成功了!我刚刚添加了 CurvedAnimation 以实现平滑过渡。不错
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 2013-07-20
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多