loadwidget(DateTime date) { var eventcahe = []; //判断title title = date.toString().substring(0, 10) == DateTime.now().toString().substring(0, 10) ? '今日课程' : '非本日课程'; var cacheindex = 0; var pos = 0; for (var i = 0; i < Global.courseInfox.length; i++) { //判断是否是今天的课程 if (Global.courseInfox[i]['jsrq'] == date.toString().substring(0, 10)) { cacheindex++; eventcahe.add(Global.courseInfox[i]); } //如果读取之后的时间在今天之前则跳过 if (Global.courseInfox[i]['jsrq'].compareTo(date.toString().substring(0, 10)) < 0) { pos = i; break; } }

//判断是否有课程 if (cacheindex == 0) { //判断jsrq和date的日期差 var date1 = DateTime.parse(Global.courseInfox[pos]['jsrq']); //获取date1的周日 var date2 = date1.add(Duration(days: 7 - date1.weekday)); var date3 = DateTime.parse(date2.toString().substring(0, 10)); //获取date的周一 var date4 = date.add(Duration(days: 1 - date.weekday)); var difference = date4.difference(date3).inDays; if (difference == 0) difference = 1;

int zc = Global.courseInfox[pos]['zc'];
if (difference > 0)
  zc = Global.courseInfox[pos]['zc'] + //向上取整
      ((difference / 7).ceil());
if (difference < 28)
  title = '第' + zc.toString() + '周' + '当天没课哦';
else
  title = '假期中。。。';

} else { setState(() { title = '第' + eventcahe[0]['zc'].toString() + '周' + title; }); } //对eventcahe进行排序 eventcahe.sort((a, b) { return a['qssj'].toString().compareTo(b['qssj'].toString()); });

dailycourse = [ Container( padding: EdgeInsets.only(left: 60, top: 30), child: Row( children: [ Text( '上午', style: TextStyle( fontSize: 30, fontWeight: FontWeight.bold, color: Global.home_currentcolor), ), ], ), ), Container( child: TimelineTile( oppositeContents: Padding( padding: const EdgeInsets.all(8.0), child: Card( child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '1,2节\n8;00 - 9:35', style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), ), ), ), contents: Card( //透明 color: Colors.transparent, shadowColor: Colors.transparent, child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '', style: TextStyle(color: Colors.white), ), ), ), node: TimelineNode( indicator: DotIndicator( color: Global.home_currentcolor, ), endConnector: SolidLineConnector( color: Global.home_currentcolor, ), ), ), ), Container( child: TimelineTile( oppositeContents: Padding( padding: const EdgeInsets.all(8.0), child: Card( child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '3,4节\n9:55 -11:30', style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), ), ), ), contents: Card( //透明 color: Colors.transparent, shadowColor: Colors.transparent, child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '', style: TextStyle(color: Colors.white), ), ), ), node: TimelineNode( indicator: DotIndicator( color: Global.home_currentcolor, ), startConnector: SolidLineConnector( color: Global.home_currentcolor, ), ), ), ), Container( padding: EdgeInsets.only(left: 60, top: 30), child: Row( children: [ Text( '下午', style: TextStyle( fontSize: 30, fontWeight: FontWeight.bold, color: Global.home_currentcolor), ), ], ), ), Container( child: TimelineTile( oppositeContents: Padding( padding: const EdgeInsets.all(8.0), child: Card( child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '5,6节\n13;30 - 15:05', style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), ), ), ), contents: Card( //透明 color: Colors.transparent, shadowColor: Colors.transparent, child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '', style: TextStyle(color: Colors.white), ), ), ), node: TimelineNode( indicator: DotIndicator( color: Global.home_currentcolor, ), endConnector: SolidLineConnector( color: Global.home_currentcolor, ), ), ), ), Container( child: TimelineTile( oppositeContents: Padding( padding: const EdgeInsets.all(8.0), child: Card( child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '7,8节\n15:25 - 17:00', style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), ), ), ), contents: Card( shadowColor: Colors.transparent, //透明 color: Colors.transparent, child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '', style: TextStyle(color: Colors.white), ), ), ), node: TimelineNode( indicator: DotIndicator( color: Global.home_currentcolor, ), startConnector: SolidLineConnector( color: Global.home_currentcolor, ), ), ), ), Container( padding: EdgeInsets.only(left: 60, top: 30), child: Row( children: [ Text( '晚课', style: TextStyle( fontSize: 30, fontWeight: FontWeight.bold, color: Global.home_currentcolor), ), ], ), ), Container( child: TimelineTile( oppositeContents: Padding( padding: const EdgeInsets.all(8.0), child: Card( child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '9,10节\n18:00 - 19:35', style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), ), ), ), contents: Card( //透明 color: Colors.transparent, shadowColor: Colors.transparent, child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '', style: TextStyle(color: Colors.white), ), ), ), node: TimelineNode( indicator: DotIndicator( color: Global.home_currentcolor, ), endConnector: SolidLineConnector( color: Global.home_currentcolor, ), ), ), ), Container( child: TimelineTile( oppositeContents: Padding( padding: const EdgeInsets.all(8.0), child: Card( child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '11,12节\n19:55 - 21:30', style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), ), ), ), contents: Card( //透明 color: Colors.transparent, shadowColor: Colors.transparent, child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '', style: TextStyle(color: Colors.white), ), ), ), node: TimelineNode( indicator: DotIndicator( color: Global.home_currentcolor, ), startConnector: SolidLineConnector( color: Global.home_currentcolor, ), ), ), ), ]; if (eventcahe.length == 0) { dailycourse = []; print('dailycourse为空'); } for (var i = 0; i < eventcahe.length; i++) { String msg = '课程名称:' + eventcahe[i]['kcmc'] + '\n' + '教室:' + eventcahe[i]['jxcdmc'] + '\n' + '第' + eventcahe[i]['zc'].toString() + '周课程' + '\n' + '教师:' + eventcahe[i]['teaxms'] + '\n' + '教学班级:' + eventcahe[i]['jxbmc'] + '\n' + '额外备注:' + eventcahe[i]['sknrjj']; //判断课程时间段,如果是08,替换dailycourse中第二个Container if (eventcahe[i]['qssj'].toString().split(' ')[0].substring(0, 2) == '08') { //清空dailycourse dailycourse[1] = Container( child: TimelineTile( oppositeContents: Padding( padding: const EdgeInsets.all(8.0), child: Card( child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '1,2节\n' + eventcahe[i]['qssj'].toString().split(' ')[0].substring(0, 5) + ' - ' + eventcahe[i]['jssj'].toString().split(' ')[0].substring(0, 5), style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), ), ), ), contents: //ontap GestureDetector( onTap: (() => Dialogs.bottomMaterialDialog( color: Colors.white, msg: msg, title: '详细信息', lottieBuilder: Lottie.asset( 'assets/course.json', fit: BoxFit.contain, ), context: context, )), child: Card( color: Global.home_currentcolor, child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( eventcahe[i]['kcmc'] + '\n' + eventcahe[i]['jxcdmc'], style: TextStyle(color: Colors.white), ), ), ), ), node: TimelineNode( indicator: DotIndicator( color: Global.home_currentcolor, ), endConnector: SolidLineConnector( color: Global.home_currentcolor, ), ), ), ); } //判断课程时间段,如果是09,替换dailycourse中第三个Container if (eventcahe[i]['qssj'].toString().split(' ')[0].substring(0, 2) == '09') { dailycourse[2] = Container( child: TimelineTile( oppositeContents: Padding( padding: const EdgeInsets.all(8.0), child: Card( child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '3,4节\n' + eventcahe[i]['qssj'].toString().split(' ')[0].substring(0, 5) + ' - ' + eventcahe[i]['jssj'].toString().split(' ')[0].substring(0, 5), style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), ), ), ), contents: //ontap GestureDetector( onTap: (() => Dialogs.bottomMaterialDialog( color: Colors.white, msg: msg, title: '详细信息', lottieBuilder: Lottie.asset( 'assets/course.json', fit: BoxFit.contain, ), context: context, )), child: Card( color: Global.home_currentcolor, child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( eventcahe[i]['kcmc'] + '\n' + eventcahe[i]['jxcdmc'], style: TextStyle(color: Colors.white), ), ), ), ), node: TimelineNode( indicator: DotIndicator( color: Global.home_currentcolor, ), startConnector: SolidLineConnector( color: Global.home_currentcolor, ), ), ), ); } //判断课程时间段,如果jssj是11,替换dailycourse中第三个Container if (eventcahe[i]['jssj'].toString().split(' ')[0].substring(0, 2) == '11') { dailycourse[2] = Container( child: TimelineTile( oppositeContents: Padding( padding: const EdgeInsets.all(8.0), child: Card( child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '3,4节\n' + eventcahe[i]['qssj'].toString().split(' ')[0].substring(0, 5) + ' - ' + eventcahe[i]['jssj'].toString().split(' ')[0].substring(0, 5), style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), ), ), ), contents: //ontap GestureDetector( onTap: (() => Dialogs.bottomMaterialDialog( color: Colors.white, msg: msg, title: '详细信息', lottieBuilder: Lottie.asset( 'assets/course.json', fit: BoxFit.contain, ), context: context, )), child: Card( color: Global.home_currentcolor, child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( eventcahe[i]['kcmc'] + '\n' + eventcahe[i]['jxcdmc'], style: TextStyle(color: Colors.white), ), ), ), ), node: TimelineNode( indicator: DotIndicator( color: Global.home_currentcolor, ), startConnector: SolidLineConnector( color: Global.home_currentcolor, ), ), ), ); } //判断课程时间段,如果jssj是21,替换dailycourse中第八个Container if (eventcahe[i]['jssj'].toString().split(' ')[0].substring(0, 2) == '21') { dailycourse[8] = Container( child: TimelineTile( oppositeContents: Padding( padding: const EdgeInsets.all(8.0), child: Card( child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( '9,10节\n' + eventcahe[i]['qssj'].toString().split(' ')[0].substring(0, 5) + ' - ' + eventcahe[i]['jssj'].toString().split(' ')[0].substring(0, 5), style: TextStyle( color: Colors.black, fontWeight: FontWeight.bold), ), ), ), ), contents: //ontap GestureDetector( onTap: (() => Dialogs.bottomMaterialDialog( color: Colors.white, msg: msg, title: '详细信息', lottieBuilder: Lottie.asset( 'assets/course.json', fit: BoxFit.contain, ), context: context, )), child: Card( color: Global.home_currentcolor, child: Container( width: widthx, padding: EdgeInsets.all(8.0), child: Text( eventcahe[i]['kcmc'] + '\n' + eventcahe[i]['jxcdmc'], style: TextStyle(color: Colors.white), ), ), ), ), node: TimelineNode( indicator: DotIndicator( color: Global.home_currentcolor, ), startConnector: SolidLineConnector( color: Global.home_currentcolor, ), ), ), ); } } setState(() {});

Flutter代码优化:精简日期判断和课程展示逻辑

原文地址: https://www.cveoy.top/t/topic/odco 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录