gpt4 book ai didi

flutter - 什么时候 route.didPop(result) 在 Flutter Navigator 2.0 中等于 false

转载 作者:行者123 更新时间:2023-12-03 13:29:10 25 4
gpt4 key购买 nike

Flutter Navigator 2.0 的主要机制之一是 RouterDelegate > build > Navigator 中的 onPopPage 函数。但是,我不明白 route.didPop(result) 何时返回 false。
我们可以使用 John Ryan's famous example显示我的问题。他的 demo code .

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;
},
在我所有的测试中,使用 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.
但是“如果路由可以在内部处理它(例如,因为它有自己的内部状态堆栈)然后返回 false”是什么意思?路由有自己的内部状态堆栈?如何产生这个结果?
谢谢,注意安全

最佳答案

经过一些研究以完全了解 Navigator 2.0,我认为这可能是问题的答案:
route.didPop(result) 将返回 false,当要求弹出的 Route 保留本地历史条目并且在弹出完整的 Route 之前必须删除它们。
那么什么是本地历史条目(内部状态堆栈)?
本地历史条目是在页面内实现本地导航的一种方式。您可以使用方法 addLocalHistoryEntry .为了更好地理解这一点,请查看官方 Flutter Docs 示例:

The following example is an app with 2 pages: HomePage and SecondPage.The HomePage can navigate to the SecondPage. The SecondPage uses aLocalHistoryEntry to implement local navigation within that page.Pressing 'show rectangle' displays a red rectangle and adds a localhistory entry. At that point, pressing the '< back' button pops thelatest route, which is the local history entry, and the red rectangledisappears. Pressing the '< back' button a second time once again popsthe latest route, which is the SecondPage, itself. Therefore, thesecond press navigates back to the HomePage.


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();
},
),
],
),
),
);
}
}
要查看文档中的示例,请单击 here .
我希望我以一种可以理解的方式回答了这个问题。

关于flutter - 什么时候 route.didPop(result) 在 Flutter Navigator 2.0 中等于 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64772436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com