我想设置属性todaysMood从另一个页面,每当列表是点击这里是我的第一个页面,我使用导航pushnamed从另一个页面转到
class HomeState extends State<Home> with AfterLayoutMixin<Home> {
int todaysMood = 5;
int userType = 0;
}这是我的第二页
class AssessState extends State<Assess> {
@override
Widget build(BuildContext context) {
final Questions args = ModalRoute.of(context).settings.arguments;
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Assess"),
),
body: ListView.builder(
itemCount: args.questions.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(args.questions[index]) ,
onTap: () {
//update attributes in first page here
},
);
},
));
}
}发布于 2019-12-25 19:52:59
假设您想要从AssessState类更改userType,
class HomeState extends State<Home> with AfterLayoutMixin<Home> {
int todaysMood = 5;
int userType = 0;
changeUserType(int type){
setState((){
userType = 1;
});
}
}在使用changeUserType()小部件时,请传递该函数
Assess(changeUserType);现在,在构造函数中获取changeUserType函数作为参数。
class Assess extends StatefulWidget {
const Assess(Function changeUserType);
@override
AssessState createState() => AssessState();
}
class AssessState extends State<Assess> {
@override
Widget build(BuildContext context) {
final Questions args = ModalRoute.of(context).settings.arguments;
// TODO: implement build
return Scaffold(
appBar: AppBar(
title: Text("Assess"),
),
body: ListView.builder(
itemCount: args.questions.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(args.questions[index]) ,
onTap: () {
//update attributes in first page here
widget.changeUserType(1);
},
);
},
));
}
}这样,通过将函数作为参数传递,您可以更改或控制其他类的状态。
发布于 2019-12-25 21:04:17
您想要实现的目标可以使用anywhere.You的状态管理技术来实现。我建议您使用BLOC .You可以更改应用程序中的任何活动状态微件状态anywhere.You可以获取the full documentation here
https://stackoverflow.com/questions/59477860
复制相似问题