Dim*_*urs 7 flutter flutter-navigation flutter-state
Flutter Navigator 2.0 的主要机制之一是 RouterDelegate > build > Navigator 中的 onPopPage 函数。但是,我不明白 route.didPop(result) 何时返回 false。
我们可以使用John Ryan 的著名例子来说明我的问题。他的演示代码。
onPopPage: (route, result) {
if (!route.didPop(result)) {
return false;
}
// Update the list of pages by setting _selectedBook to null
_selectedBook = null;
show404 = false;
notifyListeners();
return true;
},
Run Code Online (Sandbox Code Playgroud)
在我所有的测试中,使用 AppBar 自动生成的后退按钮,route.didPop(result) 返回 true。
文档保持不变:
bool didPop(dynamic result)
package:flutter/src/widgets/navigator.dart
A request was made to pop this route. If the route can handle it internally (e.g. because it has its own stack of internal state) then return false, otherwise return true (by returning the value of calling super.didPop). Returning false will prevent the default behavior of [NavigatorState.pop].
When this function returns true, the navigator removes this route from the history but does not yet call [dispose]. Instead, it is the route's responsibility to call [NavigatorState.finalizeRoute], which will in turn call [dispose] on the route. This sequence lets the route perform an exit animation (or some other visual effect) after being popped but prior to being disposed.
This method should call [didComplete] to resolve the [popped] future (and this is all that the default implementation does); routes should not wait for their exit animation to complete before doing so.
See [popped], [didComplete], and [currentResult] for a discussion of the result argument.
Run Code Online (Sandbox Code Playgroud)
但是“如果路由可以在内部处理它(例如因为它有自己的内部状态堆栈)然后返回 false”是什么意思?路由有自己的内部状态堆栈?如何产生这个结果?
谢谢,注意安全
经过一些研究以完全了解 Navigator 2.0,我认为这可能是问题的答案: route.didPop(result) 将返回 false,当要求弹出的 Route 保留本地历史条目并且它们必须是在弹出完整的路由之前删除。
那么什么是本地历史条目(内部状态堆栈)?
本地历史条目是一种在页面内实现本地导航的方式。您可以使用 方法来做到这一点addLocalHistoryEntry。为了更好地理解这一点,请查看官方 Flutter Docs 示例:
以下示例是具有 2 个页面的应用程序:HomePage 和 SecondPage。HomePage 可以导航到SecondPage。SecondPage 使用 LocalHistoryEntry 在该页面内实现本地导航。按“显示矩形”会显示一个红色矩形并添加一个本地历史条目。此时,按'<返回'按钮弹出最新的路由,即本地历史条目,红色矩形消失。再次按下“< back”按钮会弹出最新的路由,也就是 SecondPage 本身。因此,第二次按下导航回主页。
class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: '/',
routes: {
'/': (BuildContext context) => HomePage(),
'/second_page': (BuildContext context) => SecondPage(),
},
);
}
}
class HomePage extends StatefulWidget {
HomePage();
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text('HomePage'),
// Press this button to open the SecondPage.
ElevatedButton(
child: Text('Second Page >'),
onPressed: () {
Navigator.pushNamed(context, '/second_page');
},
),
],
),
),
);
}
}
class SecondPage extends StatefulWidget {
@override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
bool _showRectangle = false;
void _navigateLocallyToShowRectangle() async {
// This local history entry essentially represents the display of the red
// rectangle. When this local history entry is removed, we hide the red
// rectangle.
setState(() => _showRectangle = true);
ModalRoute.of(context).addLocalHistoryEntry(
LocalHistoryEntry(
onRemove: () {
// Hide the red rectangle.
setState(() => _showRectangle = false);
}
)
);
}
@override
Widget build(BuildContext context) {
final localNavContent = _showRectangle
? Container(
width: 100.0,
height: 100.0,
color: Colors.red,
)
: ElevatedButton(
child: Text('Show Rectangle'),
onPressed: _navigateLocallyToShowRectangle,
);
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
localNavContent,
ElevatedButton(
child: Text('< Back'),
onPressed: () {
// Pop a route. If this is pressed while the red rectangle is
// visible then it will will pop our local history entry, which
// will hide the red rectangle. Otherwise, the SecondPage will
// navigate back to the HomePage.
Navigator.of(context).pop();
},
),
],
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
要查看文档中的示例,请单击此处。
我希望我以一种可以理解的方式回答了这个问题。
| 归档时间: |
|
| 查看次数: |
997 次 |
| 最近记录: |