Flutter Navigator,使用 PageRoutebuilder 进行水平转换

Flutter Navigator, Horizontal Transiation with PageRoutebuilder

我正在尝试在 Flutter Navigator Widget 中强制执行水平转换。 Navigator 使用从一个屏幕到下一个屏幕的平台默认转换。在 iOS 中,过渡是从右到左。从左到右弹出。在 Android 中是从下到上。我认为解决方案是使用 PageRouteBuilder,但没有成功。我用 Navigator Widget 标记了方法,我想用 PageRouteBuilder 修改它以获得所需的水平过渡。

代码片段 2,是我一直在尝试但没有成功的转换代码。


此代码用作默认转换。

import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Flutter Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Demo Home Page'),
      initialRoute: "/",
      routes: {
        '/Second': (context) => SecondScreen(),
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            ButtonMaterial02(),
            new Text('NAV DEMO...',),
            new Text('How do I get a Horizontal Transition in Android?',),
          ],
        ),
      ),
 // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

//================================================================
//================================================================
  Padding ButtonMaterial02() {
    return Padding(
            padding: const EdgeInsets.all(18.0),
            child: new MaterialButton(
              onPressed: MatButton02_onPress,
              child: new Text("Material Button 02"),
              padding: EdgeInsets.all(50.0),
              minWidth: double.infinity,
              color: Theme.of(context).primaryColor,
            ),
          );
  }

  //  add Horizontal code here
  void MatButton02_onPress() {
              print(" MaterialButton02 onPressed...");
              Navigator.pushNamed(context, '/Second');
    //*************************************************
    //*************************************************
    //  HOW do I replace the Navigator above to use
    //  PageRouteBuilder so I can force ANDROID to
    //  Transition Right to Left instead of BottomToTop?
    //*************************************************
    //*************************************************
            }
}

//================================================================
//================================================================
class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),
      body: Center(
        child: RaisedButton(
          child: new Text("RETURN"),
          onPressed: (){
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}
//================================================================
//================================================================

代码片段 2...我一直在尝试使用的转换代码。

  transitionsBuilder: (
      BuildContext context, 
      Animation<double> animation,
      Animation<double> secondaryAnimation, 
      Widget child) {
        return SlideTransition(
          position: new Tween<Offset>(
          begin: const Offset(1.0, 0.0),
          end: Offset.zero,
         ).animate(animation),
        child: new SlideTransition(
        position: new Tween<Offset>(
          begin: Offset.zero,
          end: const Offset(1.0, 0.0),
        ).animate(secondaryAnimation),
        child: child,
      ),
    );
  },
);
Navigator.of(context).push('Second');

您有兴趣使用 CupertinoPageRoute。 它从右到左动画,旨在模仿 iOS 的过渡动画。

按照示例 here,将 MaterialPageRoute 引用替换为 CupertinoPageRoute,您就大功告成了!