[英]How to restart the build method on TabPage in flutter app with auto_route package
Hi a use auto_route package in flutter app.您好,请在 flutter 应用程序中使用 auto_route package。 How to restart the build method on TabPage or not save the stack state when switching tabs. state 切换标签页时如何在TabPage上重启build方法或不保存堆栈。 I should have an animation run in the build method on TabPage every time I switch a tab in the navigation bar.每次在导航栏中切换选项卡时,我都应该在 TabPage 的构建方法中运行 animation。 But the build method is not called when I click the buttons in the navigation bar但是当我单击导航栏中的按钮时不会调用构建方法
I guess you are the same user that commented on my article.我猜你就是评论我文章的那个用户。 I'll give the same answer here.我会在这里给出相同的答案。 this is not related to the autoroute package itself but it is the default behavior of a bottombar navigation.这与自动路由 package 本身无关,但它是底部导航栏的默认行为。 The objective being to always minimize the number of rebuilds necessary for your UI.目标是始终尽量减少 UI 所需的重建次数。
I didn't test it but you could try to make your tab a stateful widget (if it isn't already) and modify this behavior with the AutomaticKeepAliveClientMixin<YourWidget>
:我没有测试它,但您可以尝试使您的选项卡成为有状态的小部件(如果它还没有)并使用AutomaticKeepAliveClientMixin<YourWidget>
修改此行为:
class _YourWidgetState extends State<YourWidget> with AutomaticKeepAliveClientMixin<YourWidget>{
// Here you build your Widget after calling the super method
@override Widget build(BuildContext context) {
super.build(context)
return Container();
}
// the important part is returning false here
@override bool get wantKeepAlive => false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.