簡體   English   中英

如何使用 Flutter 更改導航動畫

[英]How to change navigation animation using Flutter

在 Flutter 中導航到/從頁面時,有什么方法可以更改默認動畫?

您可以使用PageRouteBuilder

以下示例顯示導航到第二個屏幕時的FadeTransition

Navigator.push(
  context,
  PageRouteBuilder(
    pageBuilder: (c, a1, a2) => Page2(),
    transitionsBuilder: (c, anim, a2, child) => FadeTransition(opacity: anim, child: child),
    transitionDuration: Duration(milliseconds: 2000),
  ),
);

您可以繼承MaterialPageRoute並覆蓋 buildTransitions。

例如:

class MyCustomRoute<T> extends MaterialPageRoute<T> {
  MyCustomRoute({ WidgetBuilder builder, RouteSettings settings })
      : super(builder: builder, settings: settings);

  @override
  Widget buildTransitions(BuildContext context,
      Animation<double> animation,
      Animation<double> secondaryAnimation,
      Widget child) {
    if (settings.isInitialRoute)
      return child;
    // Fades between routes. (If you don't want any animation,
    // just return child.)
    return new FadeTransition(opacity: animation, child: child);
  }
}

使用:

new RaisedButton(
                child: new Text('Goto'),
                onPressed: (){
                  Navigator.push(
                    context,
                    new MyCustomRoute(builder: (context) => new SecondPage()),
                  );
                }),

用您的動畫替換淡入淡出過渡

您可以通過使用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'),
      ),
    );
  }
}

一些玩轉動畫

  // 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(),
          ),
        ));
  }

我通過在pageTransitionsTheme中為我自己的builders為應用程序級主題提供自定義地圖來做到這一點。

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Startup Name Generator Tile',
      home: RandomWords(),
      theme: new ThemeData(
        primaryColor: Colors.white,
        // Add the line below to get horizontal sliding transitions for routes.
        pageTransitionsTheme: PageTransitionsTheme(builders: {TargetPlatform.android: CupertinoPageTransitionsBuilder(),}),
      ),
    );
  }
}

當然,我沒有為 ios 添加地圖條目,因為我只將 android 用於TargetPlatform

您還可以從https://pub.dev/packages/page_transition查看page_transition包。 該軟件包包含以下不同的轉換。 淡入淡出、rightToLeft、leftToRight、upToDown、downToUp、縮放(對齊)、旋轉(對齊)、大小(對齊)、rightToLeftWithFade、leftToRightWithFade

我想的最簡單的方法是使用 MaterialPageRoute 通常只需添加: fullscreenDialog: true, inside MaterialPageRoute()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM