如何在Flutter中获取当前路线路径?

如何在Flutter中获取当前路线路径?,第1张

如何在Flutter中获取当前路线路径

NavigatorState
不会公开用于获取当前路线路径的API,
Route
也不会公开用于确定路线路径的API。路由可以是(通常是匿名的)。
Route
使用该
isCurrent
方法,您现在可以确定给定对象是否位于导航器堆栈的顶部,但这对于您的用例而言并不十分方便。

我建议您对这个问题采取不同的方法,并且根本不要倒回根本。相反,请

Navigator
对的每个窗格使用不同的小部件
BottomNavigationBar
。这样,您在窗格之间切换时就不必倒回堆栈。你可以用你的
Navigator
小部件
Opacity
IgnorePointer
部件来隐藏他们时,他们不应该是不破坏他们的筹码可见。

import 'package:flutter/material.dart';void main() {  runApp(new MyApp());}class MyApp extends StatelessWidget {  @override  Widget build(BuildContext context) {    return new MaterialApp(      home: new MyHomePage(),    );  }}class SecurePage extends StatelessWidget {  final int index;  SecurePage(this.index);  Widget build(BuildContext context) {    return new Material(      color: Colors.amber,      child: new InkWell(        child: new Center(          child: new Icon( Icons.security, color: Colors.white, size: index * 100.0 + 20.0,          ),        ),        onTap: () {          Navigator.of(context).push( new MaterialPageRoute(   builder: (BuildContext context) {     return new SecurePage(index + 1);   }, ),          );        },      ),    );  }}class VerifiedPage extends StatelessWidget {  final int index;  VerifiedPage(this.index);  Widget build(BuildContext context) {    return new Material(      color: Colors.green,      child: new InkWell(        child: new Center(          child: new Icon( Icons.verified_user, color: Colors.white, size: index * 100.0 + 20.0,          ),        ),        onTap: () {          Navigator.of(context).push( new MaterialPageRoute(   builder: (BuildContext context) {     return new VerifiedPage(index + 1);   }, ),          );        },      ),    );  }}class MyHomePage extends StatefulWidget {  @override  State createState() => new MyHomePageState();}class MyHomePageState extends State<MyHomePage> {  int _page = 0;  List<Widget> initialWidgets = <Widget>[    new SecurePage(0),    new VerifiedPage(0),  ];  Widget build(BuildContext context) {    return new Scaffold(      body: new Stack(        children: new List<Widget>.generate(initialWidgets.length, (int index) {          return new IgnorePointer( ignoring: index != _page, child: new Opacity(   opacity: _page == index ? 1.0 : 0.0,   child: new Navigator(     onGenerateRoute: (RouteSettings settings) {       return new MaterialPageRoute(         builder: (_) => initialWidgets[index],       );     },   ), ),          );        }),      ),      bottomNavigationBar: new BottomNavigationBar(        currentIndex: _page,        onTap: (int index) {          setState(() { _page = index;          });        },        items: <BottomNavigationBarItem>[          new BottomNavigationBarItem( icon: new Icon(Icons.security), title: new Text('Secure'),          ),          new BottomNavigationBarItem( icon: new Icon(Icons.verified_user), title: new Text('Verified'),          ),        ],      ),    );  }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4884284.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-11
下一篇 2022-11-11

发表评论

登录后才能评论

评论列表(0条)

保存