You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TimeManager/src/timemanagerapp/lib/widgets/AddCourseFormWidget.dart

281 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import 'package:flutter/material.dart';
import 'package:multi_select_flutter/dialog/multi_select_dialog_field.dart';
import 'package:multi_select_flutter/util/multi_select_item.dart';
import 'package:provider/provider.dart';
import 'package:timemanagerapp/controller/CourseController.dart';
import 'package:timemanagerapp/entity/Course.dart';
import 'package:timemanagerapp/entity/CourseForm.dart';
import '../controller/TimetableWidgetController.dart';
import '../provider/TimeProvider.dart';
class AddCourseFormWidget extends StatefulWidget {
const AddCourseFormWidget({Key? key,this.exitCourse}) : super(key: key);
final Course? exitCourse;
@override
_AddCourseFormWidgetState createState() => _AddCourseFormWidgetState(exitCourse: exitCourse);
}
class _AddCourseFormWidgetState extends State<AddCourseFormWidget> {
_AddCourseFormWidgetState({this.exitCourse});
late TextEditingController _courseController;
late TextEditingController _creditController;
late TextEditingController _noteController;
late TextEditingController _startWeekController;
late TextEditingController _endWeekController;
late TextEditingController _startTimeController;
late TextEditingController _endTimeController;
late TextEditingController _teacherController;
late TextEditingController _locationController;
CourseController courseController = CourseController();
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
CourseForm courseForm = CourseForm();
Course? exitCourse;
String course = '';
String credit = '';
String note = '';
String startWeek = '';
String endWeek = '';
String startTime = '1';
String endTime = '2';
String teacher = '';
String location = '';
List<String> selectedDays = [];
List<MultiSelectItem<String>> daysList = [
MultiSelectItem('周一', ''),
MultiSelectItem('周二', ''),
MultiSelectItem('周三', ''),
MultiSelectItem('周四', ''),
MultiSelectItem('周五', ''),
MultiSelectItem('周六', ''),
MultiSelectItem('周日', ''),
];
final weekdayMap = {
'周一': 1,
'周二': 2,
'周三': 3,
'周四': 4,
'周五': 5,
'周六': 6,
'周日': 7,
};
@override
void initState() {
super.initState();
if(exitCourse != null){
course = exitCourse!.getName;
credit = exitCourse!.getCredit.toString();
note = exitCourse!.getRemark;
teacher = exitCourse!.getTeacher;
location = exitCourse!.getLocation;
selectedDays = TimetableWidgetController.getSelectDayList(exitCourse!.getStartTime);
startWeek = TimetableWidgetController.getWeekCount(dateTime:exitCourse!.getStartTime).toString();
endWeek = TimetableWidgetController.getWeekCount(dateTime:exitCourse!.getEndTime).toString();
}
_courseController = TextEditingController(text: course);
_creditController = TextEditingController(text: credit);
_noteController = TextEditingController(text: note);
_startWeekController = TextEditingController(text: startWeek);
_endWeekController = TextEditingController(text: endWeek);
_startTimeController = TextEditingController(text: startTime);
_endTimeController = TextEditingController(text: endTime);
_teacherController = TextEditingController(text: teacher);
_locationController = TextEditingController(text: location);
}
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Padding( // 为了给表单添加内边距
padding: const EdgeInsets.all(16.0), // 四周添加16像素补白
child: Form( // 表单
key: _formKey, // 设置globalKey用于后面获取FormState
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch, // 水平方向占满
children: <Widget>[
TextFormField( // 课程输入框
controller: _courseController,
decoration: InputDecoration(labelText: '课程*'), // 输入框前面的提示文字
onSaved: (value) => course = value ?? '', // 当用户确定已经完成编辑时触发
validator: (value) { // 验证表单
if (value == null || value.isEmpty) {
return '课程名为必填项';
}
return null;
},
),
SizedBox(height: 16.0),// 间隔
TextFormField(
controller: _creditController,
decoration: InputDecoration(labelText: '学分'),
onSaved: (value) => credit = value ?? '',
),
SizedBox(height: 16.0),
TextFormField(
controller: _noteController,
decoration: InputDecoration(labelText: '备注'),
onSaved: (value) => note = value ?? '',
),
SizedBox(height: 16.0),
MultiSelectDialogField<String>(
items: daysList,
initialValue: selectedDays, // 设置初始选定的值
title: Text('上课日*'),
validator: (values) {
if (values == null || values.isEmpty) {
return '上课日为必填项';
}
return null;
},
onConfirm: (values) {
setState(() {
selectedDays = values;
});
},
),
SizedBox(height: 16.0),
TextFormField(
controller: _startWeekController,
decoration: InputDecoration(labelText: '开始周'),
onSaved: (value) => startWeek = value ?? '',
validator: (value) {
if (value == null || value.isEmpty) {
return '开始周为必填项';
}
return null;
},
),
SizedBox(height: 16.0),
TextFormField(
controller: _endWeekController,
decoration: InputDecoration(labelText: '结束周'),
onSaved: (value) => endWeek = value ?? '',
validator: (value) {
if (value == null || value.isEmpty) {
return '结束周为必填项';
}
return null;
},
),
SizedBox(height: 16.0),
DropdownButtonFormField<String>(
// controller: _startTimeController,
value: startTime,
onChanged: (newValue) {
setState(() {
startTime = newValue ?? '';
});
},
items: List.generate(12, (index) => (index + 1).toString())
.map((time) => DropdownMenuItem(
value: time,
child: Text(time),
))
.toList(),
decoration: InputDecoration(labelText: '上课时间'),
validator: (value) {
if (value == null || value.isEmpty) {
return '上课时间为必填项';
}
return null;
},
),
SizedBox(height: 16.0),
DropdownButtonFormField<String>(
value: endTime,
onChanged: (newValue) {
setState(() {
endTime = newValue ?? '';
});
},
items: List.generate(12, (index) => (index + 1).toString())
.map((time) => DropdownMenuItem(
value: time,
child: Text(time),
))
.toList(),
decoration: InputDecoration(labelText: '下课时间*'),
validator: (value) {
if (value == null || value.isEmpty) {
return '下课时间为必填项';
}
return null;
},
),
SizedBox(height: 16.0),
TextFormField(
controller: _teacherController,
decoration: InputDecoration(labelText: '老师'),
onSaved: (value) => teacher = value ?? '',
),
SizedBox(height: 16.0),
TextFormField(
controller: _locationController,
decoration: InputDecoration(labelText: '地点'),
onSaved: (value) => location = value ?? '',
),
SizedBox(height: 24.0),
Row( // 按钮组
mainAxisAlignment: MainAxisAlignment.spaceAround, // 水平方向均匀排列每个元素
children: <Widget>[
ElevatedButton(
onPressed: () {
// 点击取消按钮后的操作
Navigator.pop(context); // 关闭当前界面
},
child: Text('取消'),
),
ElevatedButton(
onPressed: () {
// 点击确定按钮后的操作
if (_formKey.currentState!.validate()) {
_formKey.currentState!.save();
// 在这里执行表单提交操作
courseForm.course = course;
courseForm.credit = double.parse(credit);
courseForm.note = note;
courseForm.location = location;
courseForm.selectedDays = selectedDays.map((e) => weekdayMap[e]!).toList();
courseForm.startWeek = int.parse(startWeek);
courseForm.endWeek = int.parse(endWeek);
courseForm.startTime = int.parse(startTime);
courseForm.endTime = int.parse(endTime);
courseForm.teacher = teacher;
if(exitCourse != null){//先删除再添加
courseController.deleteCourse(exitCourse!.getCourseId).then((value){
courseController.addCourseForm(courseForm).then(((value){
// 关闭当前界面
// Provider.of<TimeProvider>(context, listen: false).updateTimetable(); // 更新菜单栏页面操作
// timePro.updatTimtTablecount = 100;
Navigator.pop(context);
}));
});
}else{
courseController.addCourseForm(courseForm).then(((value){
// 关闭当前界面
// Provider.of<TimeProvider>(context, listen: false).updateTimetable(); // 更新菜单栏页面操作
// timePro.updatTimtTablecount = 100;
Navigator.pop(context);
}));
}
};
},
child: Text('确定'),
),
],
),
],
),
),
),
);
}
}