使用Flutter通量4.1.2
如果某个方法正在侦听某个动作,则该方法第一次被点击一次;如果用户返回同一页面,则下一次该方法将被点击两次,第三次该方法被点击3次,每次增加一次。
下面是一个代码,可以在单击“下一步”时模拟相同的行为,然后调用一个动作,并在更改动作时将用户带到第二页,但是第二次,该动作侦听方法被击中两次。如果我错过了什么,谁能解决这个问题或指导我。
import 'package:flutter/material.dart';
import 'package:flutter_flux/flutter_flux.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHome(),
);
}
}
//========
class MyHome extends StatefulWidget {
@override
_MyHomeState createState() => _MyHomeState();
}
class _MyHomeState extends State<MyHome> with StoreWatcherMixin<MyHome> {
AppStore store;
@override
void initState() {
store = listenToStore(appStoreToken);
changeStatus.listen(_openSetupDialogue);
super.initState();
}
void _openSetupDialogue(dynamic payload){
if(payload == true) {
print("method got hit");
Navigator.push(context, MaterialPageRoute(builder: (context) => SecondPage()));
}
}
@override
Widget build(BuildContext context) {
return RaisedButton(
child: Text("Next"),
onPressed: (){
changeStatus(true);
},
);
}
}
//===========
class SecondPage extends StatefulWidget {
@override
_SecondPageState createState() => _SecondPageState();
}
class _SecondPageState extends State<SecondPage> {
@override
Widget build(BuildContext context) {
return RaisedButton(
child: Text("back"),
onPressed: (){Navigator.push(context, MaterialPageRoute(builder: (context) =>MyHome()),
);
},
);
}
}
//===========
class AppStore extends Store {
bool state = true;
AppStore(){
triggerOnAction(changeStatus, (status){
state = status;
});
}
bool get getAppStore => state;
}
final Action changeStatus = Action<bool>();
final appStoreToken = StoreToken(AppStore());