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

时间:2018-07-14 21:01:24

标签: flutter

我正在尝试在Flutter Navigator小部件中强制执行水平转换。 Navigator使用平台从一个屏幕到另一个屏幕的默认过渡。在iOS中,过渡是从右到左。从左到右弹出。在Android中是自下而上。我认为解决方案是使用 PageRouteBuilder ,但运气不好。我用导航器小部件标记了该方法,我想用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');

1 个答案:

答案 0 :(得分:1)

您对使用CupertinoPageRoute感兴趣。 它从右到左进行动画处理,旨在模仿iOS的过渡动画。

在示例here之后,将MaterialPageRoute引用替换为CupertinoPageRoute,您将被设置!