Reputation: 172
I have this home_page class where I have a scafold and the body will change if the selectedSubject !=null by calling the other class which is the SubjectPage(selectedSubject) selectedSubject as a parameter, at first when the condition is true the UI and the selectedSubject variable inside of the SubjectPage class change, but when I change the selectedSubject at home_page using the setState the SubjectPage will be called again, but the value inside SubjectPage will remain.
this is the code
//Where selectedSubject change
onTap: (){
setState(() {
selectedSubject = new SubjectModel(document['uid'],document['name'],document['description'],document['instructorName'],document['subjectColor']);
});
Navigator.of(context).pop();
},
This is where I change the body of the scaffold
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)
This is the SubjectPage 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"),
),
],
),
)
],
),
);
}
please help :( newbie
Upvotes: 2
Views: 211
Reputation: 3288
You can use key
property of SubjectPage
to rebuild it every time the key will change. Try this code:
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);
}
Upvotes: 4