gpt4 book ai didi

flutter - 一次推送两条路由时不会调用 RouteAware didPushNext

转载 作者:行者123 更新时间:2023-12-04 13:56:31 24 4
gpt4 key购买 nike

当同时推送两条路由到 Navigator 并使用 RouteAware 获取时
更新了当前状态,第一条路由的 didPopNext() 不是
叫。

似乎调用了 RouteObserver.didPush()(会调用 didPushNext)
在调用 FirstWiget 的 didChangeDependencies() 之前的两条路线
我订阅了 routeObserver。

当一个接一个地推送路线并等待小部件出现时,这就是我所期望的和我得到的:

I/flutter (30927): Navigator.push(FirstWidget)
I/flutter (30927): didChangeDependencies FirstWidget
I/flutter (30927): didPush FirstWidget
I/flutter (30927): build FirstWidget
I/flutter (30927): Navigator.push(SecondWidget)
I/flutter (30927): didPushNext FirstWidget
I/flutter (30927): didChangeDependencies SecondWidget
I/flutter (30927): didPush SecondWidget
I/flutter (30927): build SecondWidget
I/flutter (30927): didChangeDependencies FirstWidget
I/flutter (30927): didChangeDependencies FirstWidget
I/flutter (30927): build FirstWidget

同时插入两者时:
I/flutter (30927): Navigator.push(FirstWidget)
I/flutter (30927): Navigator.push(SecondWidget)
I/flutter (30927): didChangeDependencies FirstWidget
I/flutter (30927): didPush FirstWidget
I/flutter (30927): build FirstWidget
I/flutter (30927): didChangeDependencies SecondWidget
I/flutter (30927): didPush SecondWidget
I/flutter (30927): build SecondWidget
I/flutter (30927): didChangeDependencies FirstWidget
I/flutter (30927): build FirstWidget

这是运行示例:
import 'package:flutter/material.dart';

RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();

void main() {
runApp(MaterialApp(
title: 'Navigation Test',
home: StartWidget(),
navigatorObservers: <NavigatorObserver>[routeObserver],
));
}

class StartWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Start'),
),
body: Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text('Open both routes'),
onPressed: () {
print("Navigator.push(FirstWidget)");
Navigator.push(
context,
MaterialPageRoute(builder: (context) => FirstWidget()),
);
print("Navigator.push(SecondWidget)");
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondWidget()),
);
},
),
RaisedButton(
child: Text('Open FirstWidget'),
onPressed: () {
print("Navigator.push(FirstWidget)");
Navigator.push(
context,
MaterialPageRoute(builder: (context) => FirstWidget()),
);
},
),
],
),
),
);
}
}

class FirstWidget extends StatefulWidget {
@override
_FirstWidgetState createState() => _FirstWidgetState();
}

class _FirstWidgetState extends RouteAwareState<FirstWidget> {
Widget build(BuildContext context) {
print("build $widget");
return Scaffold(
appBar: AppBar(
title: Text('FirstWidget'),
),
body: Center(
child: Column(
children: <Widget>[
RaisedButton(
child: Text('Open SecondWidget'),
onPressed: () {
print("Navigator.push(SecondWidget)");
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondWidget()),
);
},
),
RaisedButton(
child: Text('back'),
onPressed: () {
Navigator.pop(context);
},
),
],
),
),
);
}
}

class SecondWidget extends StatefulWidget {
@override
_SecondWidgetState createState() => _SecondWidgetState();
}

class _SecondWidgetState extends RouteAwareState<SecondWidget> {
@override
Widget build(BuildContext context) {
print("build $widget");
return Scaffold(
appBar: AppBar(
title: Text('SecondWidget'),
),
body: Center(
child: RaisedButton(
child: Text('back'),
onPressed: () {
Navigator.pop(context);
},
),
),
);
}
}

abstract class RouteAwareState<T extends StatefulWidget> extends State<T>
with RouteAware {
@override
void didChangeDependencies() {
print("didChangeDependencies $widget");
routeObserver.subscribe(this, ModalRoute.of(context)); //Subscribe it here
super.didChangeDependencies();
}

@override
void didPush() {
print('didPush $widget');
}

@override
void didPopNext() {
print('didPopNext $widget');
}

@override
void didPop() {
print('didPop $widget');
}

@override
void didPushNext() {
print('didPushNext $widget');
}

@override
void dispose() {
print("dispose $widget");
routeObserver.unsubscribe(this);
super.dispose();
}
}

注意:编辑为直接包含代码。

最佳答案

放置时

WidgetsBinding.instance.addPostFrameCallback()

两者之间 Navigator.push()调用,FirstWidget 已正确注册并且 didPushNext FirstWidget回调被触发。

flutter issue #55949更多细节。

关于flutter - 一次推送两条路由时不会调用 RouteAware didPushNext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61409296/

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