在我的应用程序的第一页用户将选择从1到10的否,在选择数字之后我的第二页将显示5个GradeScreen小部件,如果用户选择5,就像明智我可以使用下面的代码创建小部件,但我无法得到用户从我的GradeScreen小部件中的dropdownbutton小部件中选择的值,
请帮我从这个GradeScreen小部件中获取价值。我是Flutter的新手。提前致谢。
import 'package:flutter/material.dart';
class CalculateCGPAScreen extends StatelessWidget {
final int noOfSubjectsCount;
CalculateCGPAScreen(this.noOfSubjectsCount);
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(),
body: Column(
children: noOfSubjects(noOfSubjectsCount),
),
);
}
List<Widget> noOfSubjects(int subjectCount) {
List<Widget> list = [];
for (int i = 1; i <= subjectCount; i++) {
list.add(GradeScreen("Subject $i", 1));
}
list.add(RaisedButton(
onPressed: () {},
color: Colors.amber,
splashColor: Colors.amberAccent,
child: Text("Calculate CGPA"),
));
return list;
}
}
class GradeScreen extends StatefulWidget {
final String subjectName;
final int gradeSystem;
GradeScreen(this.subjectName, this.gradeSystem);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _GradeScreenState();
}
}
class _GradeScreenState extends State<GradeScreen> {
String selectedGrade = "S";
List<int> creditList = [1, 2, 3, 4, 5, 6];
int selectedCredit = 1;
@override
Widget build(BuildContext context) {
// TODO: implement build
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(widget.subjectName),
DropdownButton(
items: getGradeList(widget.gradeSystem).map((String loopValue) {
return DropdownMenuItem(
child: Text(loopValue),
value: loopValue,
);
}).toList(),
onChanged: (String value) {
setState(() {
selectedGrade = value;
});
},
value: selectedGrade,
),
DropdownButton(
items: creditList.map((int credit) {
return DropdownMenuItem(
child: Text(credit.toString()),
value: credit,
);
}).toList(),
onChanged: (int value) {
setState(() {
selectedCredit = value;
});
},
value: selectedCredit,
)
],
);
}
List<String> getGradeList(int option) {
List<String> gradeSystem;
if (option == 1) {
gradeSystem = ['S', 'A', 'B', 'C', 'D', 'E'];
} else {
gradeSystem = ['O', 'A+', 'A', 'B+', 'B'];
}
return gradeSystem;
}
}
import 'package:flutter/material.dart';
class CalculateCGPAScreen extends StatelessWidget {
final int noOfSubjectsCount;
CalculateCGPAScreen(this.noOfSubjectsCount);
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(),
body: Column(
children: noOfSubjects(noOfSubjectsCount),
),
);
}
List<Widget> noOfSubjects(int subjectCount) {
List<Widget> list = [];
for (int i = 1; i <= subjectCount; i++) {
list.add(GradeScreen("Subject $i", 1));
}
list.add(RaisedButton(
onPressed: () {
for (var item in list) {
if (item is GradeScreen) {
GradeScreen gs = item;
print(gs.grade);
print(gs.credit);
}
}
},
color: Colors.amber,
splashColor: Colors.amberAccent,
child: Text("Calculate CGPA"),
));
return list;
}
}
class GradeScreen extends StatefulWidget {
final String subjectName;
final int gradeSystem;
int _credit;
String _grade;
GradeScreen(this.subjectName, this.gradeSystem);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _GradeScreenState();
}
set credit(int value) {
_credit = value;
}
get credit => _credit;
set grade(String value) {
_grade = value;
}
get grade => _grade;
}
class _GradeScreenState extends State<GradeScreen> {
String selectedGrade = "S";
List<int> creditList = [1, 2, 3, 4, 5, 6];
int selectedCredit = 1;
@override
Widget build(BuildContext context) {
// TODO: implement build
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(widget.subjectName),
DropdownButton(
items: getGradeList(widget.gradeSystem).map((String loopValue) {
return DropdownMenuItem(
child: Text(loopValue),
value: loopValue,
);
}).toList(),
onChanged: (String value) {
setState(() {
selectedGrade = value;
widget.grade = value;
});
},
value: selectedGrade,
),
DropdownButton(
items: creditList.map((int credit) {
return DropdownMenuItem(
child: Text(credit.toString()),
value: credit,
);
}).toList(),
onChanged: (int value) {
setState(() {
selectedCredit = value;
widget.credit = value;
});
},
value: selectedCredit,
)
],
);
}
List<String> getGradeList(int option) {
List<String> gradeSystem;
if (option == 1) {
gradeSystem = ['S', 'A', 'B', 'C', 'D', 'E'];
} else {
gradeSystem = ['O', 'A+', 'A', 'B+', 'B'];
}
return gradeSystem;
}
}
更改下拉列表值并将结果输入调试控制台。
你想在哪里获得下拉值?
import 'package:flutter/material.dart';
class CalculateCGPAScreen extends StatelessWidget {
final int noOfSubjectsCount;
CalculateCGPAScreen(this.noOfSubjectsCount);
@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
appBar: AppBar(),
body: Column(
children: noOfSubjects(noOfSubjectsCount),
),
);
}
List<Widget> noOfSubjects(int subjectCount) {
List<Widget> list = [];
for (int i = 1; i <= subjectCount; i++) {
list.add(GradeScreen("Subject $i", 1));
}
list.add(RaisedButton(
onPressed: () {},
color: Colors.amber,
splashColor: Colors.amberAccent,
child: Text("Calculate CGPA"),
));
return list;
}
}
class GradeScreen extends StatefulWidget {
final String subjectName;
final int gradeSystem;
int _data;
GradeScreen(this.subjectName, this.gradeSystem);
@override
State<StatefulWidget> createState() {
// TODO: implement createState
return _GradeScreenState();
}
set data(int value) {
_data = value;
}
get data => _data;
}
class _GradeScreenState extends State<GradeScreen> {
String selectedGrade = "S";
List<int> creditList = [1, 2, 3, 4, 5, 6];
int selectedCredit = 1;
@override
Widget build(BuildContext context) {
// TODO: implement build
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text(widget.subjectName),
DropdownButton(
items: getGradeList(widget.gradeSystem).map((String loopValue) {
return DropdownMenuItem(
child: Text(loopValue),
value: loopValue,
);
}).toList(),
onChanged: (String value) {
setState(() {
selectedGrade = value;
});
},
value: selectedGrade,
),
DropdownButton(
items: creditList.map((int credit) {
return DropdownMenuItem(
child: Text(credit.toString()),
value: credit,
);
}).toList(),
onChanged: (int value) {
setState(() {
selectedCredit = value;
widget.data = selectedCredit;
});
},
value: selectedCredit,
)
],
);
}
List<String> getGradeList(int option) {
List<String> gradeSystem;
if (option == 1) {
gradeSystem = ['S', 'A', 'B', 'C', 'D', 'E'];
} else {
gradeSystem = ['O', 'A+', 'A', 'B+', 'B'];
}
return gradeSystem;
}
}