【问题标题】:Create an instant transition to page in Flutter在 Flutter 中创建到页面的即时过渡
【发布时间】:2018-12-29 16:19:20
【问题描述】:

我有这两个有状态的小部件 Page1 和 Page2。 我试图让第二页在触发转换后立即出现在第一页上,并且没有任何滚动、淡入淡出或任何效果。我怎么能做到?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    Replace initial Route in MaterialApp without animation?

        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'),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 2021-06-22
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多