美文网首页
路由管理

路由管理

作者: Eyes_cc | 来源:发表于2022-06-30 08:33 被阅读0次

    一、Navigator

    Navigator是一个路由管理的组件,它提供了打开和退出路由页的方法。通过一个栈来管理活动路由集合。通常当前屏幕显示的页面就是栈顶的路由。Navigator提供了一系列方法来管理路由栈,在此我们只介绍其最常用的两个方法:

    二、MaterialPageRoute

    PageRoute类是一个抽象类,表示占有整个屏幕空间的一个模态路由页面,它还定义了路由构建及切换时过渡动画的相关接口及属性。MaterialPageRoute继承自PageRoute类,是 Material组件库提供的组件,它可以针对不同平台,实现与平台页面切换动画风格一致的路由切换动画。
    构造函数的各个参数的意义:

    (new) MaterialPageRoute<dynamic> MaterialPageRoute({
      required Widget Function(BuildContext) builder, // 是一个WidgetBuilder类型的回调函数,它的作用是构建路由页面的具体内容,返回值是一个widget。我们通常要实现此回调,返回新路由的实例。如:NewRoute()
      RouteSettings? settings, // 包含路由的配置信息,如路由名称、是否初始路由(首页)。
      bool maintainState = true,  // 默认情况下,当入栈一个新路由时,原来的路由仍然会被保存在内存中,如果想在路由没用的时候释放其所占用的所有资源,可以设置maintainState为 false。
      bool fullscreenDialog = false, // 表示新的路由页面是否是一个全屏的模态对话框,在 iOS 中,如果fullscreenDialog为true,新页面将会从屏幕底部滑入(而不是水平方向)。
    })
    

    重点:如果想自定义路由切换动画,可以自己继承 PageRoute 来实现,我们将在后面介绍动画时,实现一个自定义的路由组件。

    简单示例

    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({Key? key, required this.title}) : super(key: key);
    
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
                TextButton(
                  child: const Text("open new route"),
                  onPressed: () {
                    //导航到新路由   
                    Navigator.push( 
                      context,
                      MaterialPageRoute(builder: (context) {
                        return const NewRoute();
                      }),
                    );
                  },
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: const Icon(Icons.add),
          ), 
        );
      }
    }
    class NewRoute extends StatelessWidget {
      const NewRoute({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text("New route"),
          ),
          body: const Center(
            child: Text("This is new route"),
          ),
        );
      }
    }
    

    相关文章

      网友评论

          本文标题:路由管理

          本文链接:https://www.haomeiwen.com/subject/qmxrcrtx.html