Home Articles

Flutter Navigator,与PageRoutebuilder的水平翻译

Asked
Viewed 1573 times
1

我正试图在Flutter Navigator Widget中强制使用 horizontal Transiation . Navigator使用从一个屏幕到下一个屏幕的平台Default Transition . 在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');

1 Answer

  • 1

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

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

Related