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'), ), ], ), ); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)