在我的flutter应用程序中,我有两个下拉按钮,第一个是select subjet,第二个是select list no.If,我选择了主题和list no,然后调用studentList接口并在列表视图中显示学生列表。
这是我的代码:
class SubjectWiseAttendance extends StatefulWidget {
final imei_no,school_code,employee_id;
SubjectWiseAttendance({Key key, this.imei_no, this.school_code, this.employee_id})
: super(key: key);
@override
_SubjectWiseAttendanceState createState() => _SubjectWiseAttendanceState();
}
class _SubjectWiseAttendanceState extends State<SubjectWiseAttendance> {
String date =DateFormat("dd-MM-yyyy").format(DateTime.now());
String holder='9';
String get imei_no => '0';
String get school_code => 'STAGING';
String get employee_id => 'EMP450';
String get class_id=>holder;
String get subject_id=>'0';
String get attendance_date=>date;
String get standard_id=>'0';
String get attendance_type=>'S';
String get hr_id=>'0';
String get batch_id=>'0';
String get sort_by_lect_no=>'0';
String get Absent_Enroll_No =>new_str;
String get proxy_flag =>'N';
String get proxy_reason =>" ";
String get late_flag=>'N';
String get homeroom_id=>'0';
String get lecture_id =>'0';
List<dynamic> filteredItems;
List<dynamic> filteredItemsType;
String _selectedLocation;
Future api1;
var result ;
var msg;
var validate;
var validate1;
var api2;
var array;
@override
void initState() {
print('initState');
_selectedLocation;
_lectures;
api1=_fetchClassdivision(imei_no, school_code, employee_id);
api2=_fetchStudentlist(imei_no, school_code, employee_id, class_id, subject_id, attendance_date, standard_id, attendance_type, hr_id, batch_id, sort_by_lect_no);
super.initState();
}
@override
void dispose() {
super.dispose();
}
_fetchClassdivision(
String imei_no, String school_code, String employee_id) async {
var apiUrl ='https://staging.ethdigitalcampus.com/T-View/service/DC/getClassDivisionSubjectwise' ;
List<ClassDivisionSubject> list;
Map<String, String> headers = {
'content-type': 'application/x-www-form-urlencoded'
};
print('api1');
Map<String, String> body = {
'imei_no': imei_no,
'school_code': school_code,
'employee_id': employee_id,
};
http.Response response =
await http.post(apiUrl, body: body, headers: headers);
var data = json.decode(response.body);
return data ;
}
**List _buildFilteredList(List<dynamic> data) {
List filteredItems = [];
for(int j=0;j<data.length;j++)
{
String subject = data[j]['class_desc']+"--"+ data[j]['subject_desc'];
filteredItems.add(subject.toString());
}
return filteredItems;
}**
_fetchStudentlist(...)
Widget listViewWidget(data) {...
}
final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
body: new SafeArea(
top: false,
bottom: false,
child: new Form(
key: _formKey,
autovalidate: true,
child: new ListView(
children: <Widget>[
Container(
child:FutureBuilder(
future:api1,
builder: (BuildContext context,snapshot) {
print('class division');
if (snapshot.data == null) {
return Column(children: <Widget>[
Container(
child: Center(
child: CircularProgressIndicator()
))
]);
} else {
if(result =='0') {
filteredItems = _buildFilteredList(array);
if (filteredItems.length > 0) {
if(_flag == true){
_selectedLocation = filteredItems[0];
}
return Column(children: <Widget>[
Divider(),
DropdownButton(
isExpanded: true,
isDense: true,
value: _selectedLocation,
onChanged: (newValue) {
setState(() {
_flag = false;
_selectedLocation = newValue
});
},
items: filteredItems.map((subject) {
return DropdownMenuItem(
child: new Text(subject),
value: subject,
);
}).toList(),
),
]
);
}
} }}),
),
child: FutureBuilder(
future: api2,
builder: (context, snapshot) {
print('check student list');
if (snapshot.hasError) print(snapshot.error);
return snapshot.data != null
? listViewWidget(snapshot.data)
: Center(child: CircularProgressIndicator());
}),
),
],
)
)
),
}
输出:
如果更改了下拉值,那么如何根据下拉值更改studentList。
发布于 2020-01-14 09:25:31
在DropDown
的api中,添加一个对调用onChanged
的方法的调用:
DropdownButton(
...
onChanged: (String subject_id) {
_fetchStudentlist(..., subject_id, ...);
},
...
),
https://stackoverflow.com/questions/59729568
复制相似问题