替换MaterialApp中的初始路线而不动画?

KBD*_*ums 24 dart flutter

我们的应用程序是建立在顶部Scaffold和这一点,我们已经能够使用内提供的电话,以适应大多数的我们的路线和导航的要求NavigatorState(pushNamed(),pushReplacementNamed(),等).我们不想要的是,当用户从我们的抽屉(导航)菜单中选择一个项目时,可以使用任何类型的"推送"动画.我们希望从导航菜单中单击目标屏幕,以有效地成为堆栈的新初始路径.目前我们正在使用pushReplacementNamed()来确保应用栏中没有后退箭头.但是,右下方的幻灯片意味着正在构建堆栈.

在没有动画的情况下更改初始路线的最佳选择是什么?我们可以这样做同时同时关闭抽屉的动画吗?或者我们是否正在考虑这样的情况:我们需要从导航器转移到仅使用单个脚手架并在用户想要更换屏幕时直接更新"正文"?

我们注意到有一个replace()电话NavigatorState我们认为它可能是开始寻找的正确位置,但目前还不清楚如何访问我们最初建立的各种路线new MaterialApp().replaceNamed()可能有点像顺序;-)

提前致谢!

Col*_*son 46

你在做什么听起来有点像BottomNavigationBar,所以你可能想要考虑其中一个而不是一个Drawer.

然而,拥有单个Scaffold并更新body用户点击抽屉项目的时间是一种非常合理的方法.您可以考虑将a FadeTransition从一个身体更改为另一个身体.

或者,如果您喜欢使用Navigator但不想使用默认幻灯片动画,则可以通过扩展来自定义(或禁用)动画MaterialPageRoute.这是一个例子:

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

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);
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Navigation example',
      onGenerateRoute: (RouteSettings settings) {
        switch (settings.name) {
          case '/': return new MyCustomRoute(
            builder: (_) => new MyHomePage(),
            settings: settings,
          );
          case '/somewhere': return new MyCustomRoute(
            builder: (_) => new Somewhere(),
            settings: settings,
          );
        }
        assert(false);
      }
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Navigation example'),
      ),
      drawer: new Drawer(
        child: new ListView(
          children: <Widget> [
            new DrawerHeader(
              child: new Container(
                  child: const Text('This is a header'),
              ),
            ),
            new ListTile(
              leading: const Icon(Icons.navigate_next),
              title: const Text('Navigate somewhere'),
              onTap: () {
                Navigator.pushNamed(context, '/somewhere');
              },
            ),
          ],
        ),
      ),
      body: new Center(
        child: new Text(
          'This is a home page.',
        ),
      ),
    );
  }
}

class Somewhere extends StatelessWidget {
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new Text(
          'Congrats, you did it.',
        ),
      ),
      appBar: new AppBar(
        title: new Text('Somewhere'),
      ),
      drawer: new Drawer(
        child: new ListView(
          children: <Widget>[
            new DrawerHeader(
              child: new Container(
                child: const Text('This is a header'),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)


Cop*_*oad 5

您也可以PageRouteBuilder为此使用。

例:

@override
Widget build(BuildContext context) {
  return RaisedButton(
    onPressed: () {
      Navigator.push(
        context,
        PageRouteBuilder(
          pageBuilder: (context, anim1, anim2) => SecondScreen(),
          transitionsBuilder: (context, anim1, anim2, child) => FadeTransition(opacity: anim1, child: child),
          transitionDuration: Duration(seconds: 1),
        ),
      );
    },
  );
}
Run Code Online (Sandbox Code Playgroud)

如果您不希望有任何动画,请替换上面transitionsBuilder

transitionsBuilder: (context, anim1, anim2, child) => Container(child: child),
Run Code Online (Sandbox Code Playgroud)