4

我正在尝试在 Flutter Navigator 小部件中强制进行水平转换。Navigator 使用平台默认转换从一个屏幕到下一个屏幕。在 iOS 中,转换是从右到左。从左到右弹出。在 Android 中,它是自下而上的。我相信解决方案是使用PageRouteBuilder,但没有运气让它工作。我用我想用 PageRouteBuilder 修改的 Navigator Widget标记了该方法,以获得所需的水平过渡。

Code Snippet 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);
          },
        ),
      ),
    );
  }
}
//================================================================
//================================================================

Code Snippet 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');
4

1 回答 1

3

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

按照此处的示例,将MaterialPageRoute引用替换为CupertinoPageRoute,您将被设置!

于 2018-07-18T02:47:53.427 回答