当我调用颤振类时,值不会改变

时间:2021-02-16 13:15:43

标签: flutter

我有这个 home_page 类,我有一个脚架,如果 selectedSubject !=null,主体会发生变化 通过调用另一个类,即 SubjectPage(selectedSubject) selectedSubject 作为参数, 首先,当条件为真时,SubjectPage 类中的 UI 和 selectedSubject 变量会发生变化,但是当我使用 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)

这是 SubjectPage 类

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"),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }

请帮助:(新手

1 个答案:

答案 0 :(得分:4)

您可以使用 keySubjectPage 属性在每次密钥更改时重建它。试试这个代码:

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);
}