gpt4 book ai didi

dart - 如何在另一个类小部件中获取自定义有状态小部件值?

转载 作者:IT王子 更新时间:2023-10-29 07:22:30 26 4
gpt4 key购买 nike

在我的应用程序的第一个页面中,用户将选择 1 到 10 之间的“否”,在选择数字后,如果用户选择 5,我的第二个页面将显示 5 个 GradeScreen 小部件,就像我可以使用下面的代码创建小部件一样,但我不能'在我的 GradeScreen 小部件中获取用户从下拉按钮小部件中选择的值,

请帮助我从这个 GradeScreen 小部件中获取值(value)。我是 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;
}
}

更改下拉值并将结果输入调试控制台。

关于dart - 如何在另一个类小部件中获取自定义有状态小部件值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54949605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com