可以将重复的部分抽取成一个函数,传入参数来区分不同的判断条件,具体实现如下:

void replaceContainer(int index, String timeStr, String msg) {
  dailycourse[index] = Container(
    child: TimelineTile(
      oppositeContents: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Card(
          child: Container(
            width: widthx,
            padding: EdgeInsets.all(8.0),
            child: Text(
              timeStr,
              style: TextStyle(
                  color: Colors.black, fontWeight: FontWeight.bold),
            ),
          ),
        ),
      ),
      contents: 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'] + '
' + eventcahe[i]['jxcdmc'],
              style: TextStyle(color: Colors.white),
            ),
          ),
        ),
      ),
      node: TimelineNode(
        indicator: DotIndicator(
          color: Global.home_currentcolor,
        ),
        startConnector: SolidLineConnector(
          color: Global.home_currentcolor,
        ),
      ),
    ),
  );
}

// 判断课程时间段,替换dailycourse中的Container
for (var i = 0; i < eventcahe.length; i++) {
  String timeStr = '';
  if (eventcahe[i]['qssj'].toString().split(' ')[0].substring(0, 2) == '08') {
    timeStr = '1,2节
' + 
        eventcahe[i]['qssj']
            .toString()
            .split(' ')[0]
            .substring(0, 5) + 
        ' - ' + 
        eventcahe[i]['jssj']
            .toString()
            .split(' ')[0]
            .substring(0, 5);
    replaceContainer(1, timeStr, msg);
  } else if (eventcahe[i]['qssj'].toString().split(' ')[0].substring(0, 2) == '09') {
    timeStr = '3,4节
' + 
        eventcahe[i]['qssj']
            .toString()
            .split(' ')[0]
            .substring(0, 5) + 
        ' - ' + 
        eventcahe[i]['jssj']
            .toString()
            .split(' ')[0]
            .substring(0, 5);
    replaceContainer(2, timeStr, msg);
  } else if (eventcahe[i]['jssj'].toString().split(' ')[0].substring(0, 2) == '11') {
    timeStr = '3,4节
' + 
        eventcahe[i]['qssj']
            .toString()
            .split(' ')[0]
            .substring(0, 5) + 
        ' - ' + 
        eventcahe[i]['jssj']
            .toString()
            .split(' ')[0]
            .substring(0, 5);
    replaceContainer(2, timeStr, msg);
  }
}

通过将重复代码提取为函数 replaceContainer,传入不同的参数 indextimeStrmsg,实现了对不同条件下的容器替换。这样做可以有效地提高代码的可读性和可维护性。

优化后的代码优点:

  • **代码更简洁:**重复代码被提取到函数中,主代码逻辑更加清晰。
  • **代码可读性更高:**函数名称可以清晰地表达代码的功能,方便理解代码逻辑。
  • **代码可维护性更好:**如果需要修改容器替换逻辑,只需要修改函数内部代码,而不需要修改多个相同代码块。

总结:

通过提取重复代码为函数,可以有效地优化代码结构,提高代码的可读性和可维护性,是代码优化过程中常用的技巧之一。

代码优化:提取重复代码为函数

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

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