showMonthPicker function

void showMonthPicker(
  1. dynamic context, {
  2. required dynamic onSelected(
    1. int,
    2. int
    ),
  3. int? firstYear,
  4. int? initialSelectedMonth,
  5. int? initialSelectedYear,
  6. int? lastYear,
  7. int? firstEnabledMonth,
  8. int? lastEnabledMonth,
  9. String selectButtonText = "OK",
  10. String cancelButtonText = "Cancel",
  11. Color highlightColor = Colors.green,
  12. Color? contentBackgroundColor = Colors.white,
  13. Color? dialogBackgroundColor,
  14. Color? textColor,
})

public method to access the month picker dialog with the required parameters

Implementation

void showMonthPicker(context,
    {required Function(int, int) onSelected,
    int? firstYear,
    int? initialSelectedMonth,
    int? initialSelectedYear,
    int? lastYear,
    int? firstEnabledMonth,
    int? lastEnabledMonth,
    String selectButtonText = "OK",
    String cancelButtonText = "Cancel",
    Color highlightColor = Colors.green,
    Color? contentBackgroundColor = Colors.white,
    Color? dialogBackgroundColor,
    Color? textColor}) {
  // check if the parameters are valid
  try {
    // check if the first enabled month is valid
    if (firstEnabledMonth != null) {
      assert(firstEnabledMonth >= 1 && firstEnabledMonth <= 12);
    }

    // check if the last enabled month is valid
    if (lastEnabledMonth != null) {
      assert(lastEnabledMonth >= 1 && lastEnabledMonth <= 12);
    }

    firstEnabledMonth ??= 1;
    lastEnabledMonth ??= 12;
    firstYear ??= 1900;
    lastYear ??= DateTime.now().year;
    initialSelectedMonth ??= DateTime.now().month;
    initialSelectedYear ??= DateTime.now().year;

    // check if the first year is less than the last year
    assert(firstYear <= lastYear);

    // check if the initial selected year is between the first and last year
    assert(initialSelectedYear >= firstYear);
    assert(initialSelectedYear <= lastYear);

    if (initialSelectedYear == firstYear) {
      // check if the initial selected month is greater than the first enabled month
      assert(initialSelectedMonth >= firstEnabledMonth);
    }

    if (initialSelectedYear == lastYear) {
      // check if the initial selected month is less than the last enabled month
      assert(initialSelectedMonth <= lastEnabledMonth);
    }
  } catch (e) {
    // if not valid, log the error and return
    log(e.toString(), name: "flutter_custom_month_picker");
    return;
  }

  /// show the dialog
  showDialog(
      context: context,
      builder: (BuildContext ctx) {
        return _CustomMonthPicker(
            onSelected: onSelected,
            firstYear: firstYear,
            initialSelectedMonth: initialSelectedMonth,
            initialSelectedYear: initialSelectedYear,
            lastYear: lastYear,
            firstEnabledMonth: firstEnabledMonth,
            lastEnabledMonth: lastEnabledMonth,
            selectButtonText: selectButtonText,
            cancelButtonText: cancelButtonText,
            highlightColor: highlightColor,
            contentBackgroundColor: contentBackgroundColor,
            dialogBackgroundColor: dialogBackgroundColor,
            textColor: textColor);
      });
}