在Flatter中导航到/从页面导航时,有没有办法更改默认动画?

推荐答案

您可以通过使用CupertinoPageRoute来实现这一点. 请判断以下代码.

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';


void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Transition Animation Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new FirstPage(),
    );
  }
}

class FirstPage extends StatefulWidget {
  @override
  _FirstPageState createState() => new _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('First Page'),
      ),
      body: new Center(
        child: new RaisedButton(
          child: new Text('Goto Second Page'),
          onPressed: () {
            Navigator.of(context).push(new SecondPageRoute());
          },
        ),
      ),
    );
  }
}

class SecondPageRoute extends CupertinoPageRoute {
  SecondPageRoute()
      : super(builder: (BuildContext context) => new SecondPage());


  // OPTIONAL IF YOU WISH TO HAVE SOME EXTRA ANIMATION WHILE ROUTING
  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return new FadeTransition(opacity: animation, child: new SecondPage());
  }
}

class SecondPage extends StatefulWidget {
  @override
  _SecondPageState createState() => new _SecondPageState();
}

class _SecondPageState extends State<SecondPage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Second Page'),
      ),
      body: new Center(
        child: new Text('This is the second page'),
      ),
    );
  }
}

Some play-around with animation

  // OPTIONAL IF YOU WISH TO HAVE SOME EXTRA ANIMATION WHILE ROUTING
  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return new RotationTransition(
        turns: animation,
        child: new ScaleTransition(
          scale: animation,
          child: new FadeTransition(
            opacity: animation,
            child: new SecondPage(),
          ),
        ));
  }

Flutter相关问答推荐

如何在flater中实现Notch?

Flutter -设计具有奇怪边界的容器的方法Radius

如何解决这个错误,同时获得的高度的小部件,因此高度的可用工作区域在Flutter ?

如何使一个悬浮的appbar在flutter?

如何在Flutter 中不从getX库中初始化GetxController中的变量

Flutter 图标记移动

在Flutter 中实现中小型应用程序栏标题

如何在用Ffltter成功登录后重定向下一页?

在Flutter 中每次点击按钮时都很难调用自定义函数

如何处理平台游戏的对象冲突?

在 Flutter 中从 FireStore 获取数据并编译它们需要太多时间

无状态和有状态小部件,我可以同时使用吗?

Flutter Firebase 升级后出现问题? ARC 语义问题 (Xcode): Select 器appleCredentialWithIDToken:rawNonce:fullName:没有已知的类方法

Freezed+BLoc 如何从 BLoc Listener 查看当前状态和特定状态的变量

Flutter 错误:OutletListModel类型的值不能分配给List类型的变量?

有什么办法可以消除Flutter 中的传递依赖?

我可以定义一次 sharedPreferences 并在需要数据显示到 flutter 应用程序时调用它吗?

如何在 cupertino switch flutter 中添加文本

如何限制用户每 24 小时执行的操作数?

将 onSaved(value) 从自定义 TextFormField 小部件传递给另一个?