![](/img/trans.png)
[英]Why doesn't Switch in Flutter change value when I tap it but only on drag?
[英]Value doesn't change when I call a flutter class
我有这个主页 class 我有一个脚手架,如果 selectedSubject = null 通过调用另一个 class 这是 SubjectPage(selectedSubject) selectedSubject 作为参数,首先当条件为真时 UI 和 selectedSubject SubjectPage class 内部的变量发生了变化,但是当我使用 setState 在 home_page 更改 selectedSubject 时,将再次调用 SubjectPage。 但 SubjectPage 内的值将保留。
这是代码
//Where selectedSubject change
onTap: (){
setState(() {
selectedSubject = new SubjectModel(document['uid'],document['name'],document['description'],document['instructorName'],document['subjectColor']);
});
Navigator.of(context).pop();
},
这是我改变脚手架主体的地方
body: selectedSubject == null ?
Container(
height: 200,
margin: EdgeInsets.all(5),
alignment: Alignment.topLeft,
child: Column(
children: [
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Subject for Today",style:TextStyle(fontWeight: FontWeight.bold,fontSize: 30) ,),
Text(DateTime.now().toString(),style:TextStyle(fontWeight: FontWeight.w200,fontSize: 15) ,),
FlatButton(onPressed: (){}, child: Text("Go to subject"),
color: Color(0xfff9683a),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(topRight: Radius.circular(20)),
),),
// ListView(
// children: [
// //list task
// ],
// )
],
),
],
))
: SubjectPage(selectedSubject)
这是主题页面 Class
class SubjectPage extends StatefulWidget {
final SubjectModel selectedSubject;
SubjectPage(this.selectedSubject);
@override
SubjectPageState createState() => SubjectPageState(selectedSubject);
}
class SubjectPageState extends State<SubjectPage>{
final SubjectModel selectedSubject;
SubjectPageState(this.selectedSubject);
TimeOfDay currentTime = TimeOfDay.now();
Color color;
@override
Widget build(BuildContext context) {
print(selectedSubject.name);
color = Color(int.parse("0x"+selectedSubject.subjectColor.toString().replaceAll('#', "")));
return Container(
margin: EdgeInsets.all(5),
alignment: Alignment.topLeft,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(selectedSubject.name.toString().titleCase,
style: TextStyle(fontWeight: FontWeight.bold,
fontSize: 50),),
Text(selectedSubject.instructorName,
style: TextStyle(fontSize: 15,
fontWeight: FontWeight.w200),),
Text(selectedSubject.description),
Container(
margin: EdgeInsets.only(top: 10),
width: double.infinity,
height: 30,
child: ListView(
scrollDirection: Axis.horizontal,
children: [
Container(
alignment: Alignment.center,
decoration: BoxDecoration(
border: Border.all(
color: color,
),
borderRadius: BorderRadius.all(Radius.circular(50))
),
height: 5,
width: 200,
child: Text("Tues 3:10pm - 4:10pm"),
),
],
),
)
],
),
);
}
请帮助:(新手
您可以使用SubjectPage
的key
属性在每次密钥更改时重建它。 试试这个代码:
body: selectedSubject == null
? Container(...)
: SubjectPage(
// Here I assumed that uid is a String. If not, use string literals
key: Key(selectedSubject.uid),
selectedSubject: selectedSubject,
)
...
class SubjectPage extends StatefulWidget {
final SubjectModel selectedSubject;
SubjectPage({Key key, @required this.selectedSubject}): super(key: key);
@override
SubjectPageState createState() => SubjectPageState(selectedSubject);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.