build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Describes the part of the user interface represented by this widget.

The framework calls this method when this widget is inserted into the tree in a given BuildContext and when the dependencies of this widget change (e.g., an InheritedWidget referenced by this widget changes). This method can potentially be called in every frame and should not have any side effects beyond building a widget.

The framework replaces the subtree below this widget with the widget returned by this method, either by updating the existing subtree or by removing the subtree and inflating a new subtree, depending on whether the widget returned by this method can update the root of the existing subtree, as determined by calling Widget.canUpdate.

Typically implementations return a newly created constellation of widgets that are configured with information from this widget's constructor and from the given BuildContext.

The given BuildContext contains information about the location in the tree at which this widget is being built. For example, the context provides the set of inherited widgets for this location in the tree. A given widget might be built with multiple different BuildContext arguments over time if the widget is moved around the tree or if the widget is inserted into the tree in multiple places at once.

The implementation of this method must only depend on:

If a widget's build method is to depend on anything else, use a StatefulWidget instead.

See also:

  • StatelessWidget, which contains the discussion on performance considerations.

Implementation

@override
Widget build(BuildContext context) {
  final size = MediaQuery.of(context).size;
  final mCtr = Get.find<MonthBuilderController>(tag: matchId);

  double monthWidth = size.width;
  double monthHeight = monthCustomizer?.montMinhHeight ?? 300;
  double buttonHeight = monthHeight / 6;
  late double buttonWidth;
  double buttonChildHeight =
      monthCustomizer?.monthButtonCustomizer?.height ?? (buttonHeight / 1.5);
  double buttonChildWidth =
      monthCustomizer?.monthButtonCustomizer?.width ?? buttonHeight / 1.5;

  late double paddingLeft;
  double weekHeight = monthCustomizer?.monthWeekCustomizer?.height ?? 40;

  ///default = [WeekStartsFrom.sunday]
  int weekSelectedIndex =
      (WeekStartsFrom.values.indexOf(mCtr.mWeekStartsFrom));

  // List<String> weeks =
  // monthCustomizer?.weekStartsFrom == WeekStartsFrom.monday
  //     ? Global.weeksStartsMonday
  //     : monthCustomizer?.weekStartsFrom == WeekStartsFrom.saturday
  //         ? Global.weeksStartsSaturday
  //         : Global.weeksStartsSunday;

  double headerHeight = monthCustomizer?.monthHeaderCustomizer?.height ?? 40;
  double headerWidth =
      monthCustomizer?.monthHeaderCustomizer?.width ?? size.width;
  final EdgeInsetsGeometry? padding =
      monthCustomizer?.monthHeaderCustomizer?.padding;
  final TextStyle? textStyleHeader =
      monthCustomizer?.monthHeaderCustomizer?.textStyle;
  final TextStyle? textStyleWeeek =
      monthCustomizer?.monthWeekCustomizer?.textStyle;

  return Column(
    children: [
      LayoutBuilder(
        builder: (context, constraints) {
          ///[max()] is used to make -negetive values to zero
          ///[max()] imported from ['dart:math'];
          paddingLeft = max(
              0, (((constraints.maxWidth / 7) / 2) - (buttonChildWidth / 2)));
          final List<String>? userMonths =
              monthCustomizer?.monthHeaderCustomizer?.monthList;
          if (userMonths != null) {
            assert(!(userMonths.length < 12),
                '\n\nERROR ( Calendar Builder ):\n---------\nInside MonthHeaderCustomizer()\nmonthList length Should be equal to 12\n---------\n_');
          }

          var ifHeaderBuilderEmpty = SizedBox(
            height: headerHeight,
            width: headerWidth,
            child: Padding(
              padding: padding ?? EdgeInsets.only(left: paddingLeft),
              child: Align(
                alignment: Alignment.centerLeft,
                child: Text(
                  userMonths?[monthIndex] ??
                      CalendarGlobals.months[monthIndex],
                  style: textStyleHeader ??
                      const TextStyle(
                          fontSize: 22, fontWeight: FontWeight.w600),
                ),
              ),
            ),
          );
          return monthCustomizer?.monthHeaderBuilder == null
              ? ifHeaderBuilderEmpty
              : monthCustomizer!.monthHeaderBuilder!(
                  CalendarGlobals.months[monthIndex],
                  headerHeight,
                  headerWidth,
                  paddingLeft,
                );
        },
      ),
      LayoutBuilder(
        builder: (context, constraints) {
          buttonWidth = constraints.maxWidth / 7;
          return Row(
              children: List.generate(7, (weekIndex) {
            List<String>? userWeek =
                monthCustomizer?.monthWeekCustomizer?.weekList;
            if (userWeek != null) {
              assert(!(userWeek.length < 7),
                  '\n\nERROR ( Calendar Builder ):\n---------\nInside MonthWeekCustomizer()\nWeekList length Should be equal to 7\n---------\n_');
            }

            ///if empty
            var ifWeekBuilderEmpty = SizedBox(
              height: weekHeight,
              width: buttonWidth,
              child: Align(
                child: Text(
                  userWeek?[weekIndex] ??
                      CalendarGlobals.weeksStarter[weekSelectedIndex]
                          [weekIndex],
                  style: textStyleWeeek ??
                      const TextStyle(
                          fontSize: 14,
                          color: Colors.grey,
                          fontWeight: FontWeight.w500),
                  overflow: TextOverflow.ellipsis,
                  maxLines: 1,
                ),
              ),
            );
            return monthCustomizer?.monthWeekBuilder == null
                ? ifWeekBuilderEmpty
                : monthCustomizer!.monthWeekBuilder!(
                    weekIndex,
                    CalendarGlobals.weeksStarter[weekSelectedIndex]
                        [weekIndex],
                    weekHeight,
                    buttonWidth,
                  );
          }));
        },
      ),
      SizedBox(
        height: monthHeight,
        width: monthWidth,
        child: _MonthButtonGrid(
          matchId: matchId,
          uiStateTag: uiStateTag,
          monthHeight: monthHeight,
          monthWidth: monthWidth,
          monthIndex: monthIndex,
          buttonChildWidth: buttonChildWidth,
          buttonChildHeight: buttonChildHeight,
          monthCustomizer: monthCustomizer,
          thisSelectedYear: thisSelectedYear,
        ),
      ),
    ],
  );
}