如何在颤动中获取当前的路径路径?
问题描述:
在执行persistent bottom bar时,之前的route need to be restored当点击bottom bar中的按钮时。如何在颤动中获取当前的路径路径?
当点击底部的一个按钮时,它的当前路径路径(/a/b/c)被保存,并且先前保存的路线根据按钮点击恢复。
从概念上讲,用户会将每个按钮视为工作区,并且其状态永远不会丢失(包括后退堆栈)。用户可以安全地从一个工作区切换到另一个工作区
如何在Flutter中获取当前路径路径,当路由为rewinding to root?
答
NavigatorState
不公开API以获取当前路由的路径,而Route
也不公开用于确定路由路径的API。路线可以是(并且通常是)匿名的。您现在可以使用isCurrent
方法找出给定的Route
是否位于导航器堆栈的顶部,但这对您的用例来说不是很方便。
我会建议你采取不同的方法来解决这个问题,不要倒退到根目录。相反,请对BottomNavigationBar
的每个窗格使用不同的Navigator
小部件。这样,在切换窗格时不必倒带堆栈。你可以将你的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'),
),
],
),
);
}
}
哇!美化。该解决方案还解决了我遇到的其他状态问题。 –
事实上,我在摆弄'Stack'和'Opacity',但无法让它工作。我不知道“Navigator”可以被本地化。 'IgnorePointer'是我无法想象的另一个技巧。结合两者,内置[Hide-Show Widget](https://stackoverflow.com/questions/44489804/show-hide-widgets-on-flutter-programmatically)将会很好,让它们可以被发现。 –
这个奇妙的解决方案有[其他问题](https://stackoverflow.com/questions/46500186/flutter-keyboard-issue-when-wrap-under-stack-opacity-scrollable)。 –