date_picker_mixin.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * @Author : WuWei
  3. * @LastEditors : WuWei
  4. * @Date : 2022-08-17 23:52:35
  5. * @LastEditTime : 2023-02-08 09:08:33
  6. * @Description : Do not edit
  7. */
  8. import 'package:flutter/material.dart';
  9. import 'styles/date_picker_styles.dart';
  10. ///
  11. mixin CommonDatePickerFunctions {
  12. /// Builds widgets showing abbreviated days of week. The first widget in the
  13. /// returned list corresponds to the first day of week for the current locale.
  14. ///
  15. /// Examples:
  16. ///
  17. /// ```
  18. /// ┌ Sunday is the first day of week in the US (en_US)
  19. /// |
  20. /// S M T W T F S <-- the returned list contains these widgets
  21. /// _ _ _ _ _ 1 2
  22. /// 3 4 5 6 7 8 9
  23. ///
  24. /// ┌ But it's Monday in the UK (en_GB)
  25. /// |
  26. /// M T W T F S S <-- the returned list contains these widgets
  27. /// _ _ _ _ 1 2 3
  28. /// 4 5 6 7 8 9 10
  29. /// ```
  30. List<Widget> getDayHeaders(DayHeaderStyleBuilder headerStyleBuilder,
  31. List<String> narrowWeekdays, int firstDayOfWeekIndex) {
  32. final List<Widget> result = <Widget>[];
  33. for (int i = firstDayOfWeekIndex; true; i = (i + 1) % 7) {
  34. DayHeaderStyle headerStyle = headerStyleBuilder(i);
  35. final String weekday = narrowWeekdays[i];
  36. Widget header = ExcludeSemantics(
  37. child: Container(
  38. decoration: headerStyle.decoration,
  39. child: Center(child: Text(weekday, style: headerStyle.textStyle)),
  40. ),
  41. );
  42. result.add(header);
  43. if (i == (firstDayOfWeekIndex - 1) % 7) {
  44. break;
  45. }
  46. }
  47. return result;
  48. }
  49. /// Computes the offset from the first day of week that the first day of the
  50. /// [month] falls on.
  51. ///
  52. /// For example, September 1, 2017 falls on a Friday, which in the calendar
  53. /// localized for United States English appears as:
  54. ///
  55. /// ```
  56. /// S M T W T F S
  57. /// _ _ _ _ _ 1 2
  58. /// ```
  59. ///
  60. /// The offset for the first day of the months is the number of leading blanks
  61. /// in the calendar, i.e. 5.
  62. ///
  63. /// The same date localized for the Russian calendar has a different offset,
  64. /// because the first day of week is Monday rather than Sunday:
  65. ///
  66. /// ```
  67. /// M T W T F S S
  68. /// _ _ _ _ 1 2 3
  69. /// ```
  70. ///
  71. /// So the offset is 4, rather than 5.
  72. ///
  73. /// This code consolidates the following:
  74. ///
  75. /// - [DateTime.weekday] provides a 1-based index into days of week, with 1
  76. /// falling on Monday.
  77. /// - [MaterialLocalizations.firstDayOfWeekIndex] provides a 0-based index
  78. /// into the [MaterialLocalizations.narrowWeekdays] list.
  79. /// - [MaterialLocalizations.narrowWeekdays] list provides localized names of
  80. /// days of week, always starting with Sunday and ending with Saturday.
  81. int computeFirstDayOffset(int year, int month, int firstDayOfWeekFromSunday) {
  82. // 0-based day of week, with 0 representing Monday.
  83. final int weekdayFromMonday = DateTime(year, month).weekday - 1;
  84. // firstDayOfWeekFromSunday recomputed to be Monday-based
  85. final int firstDayOfWeekFromMonday = (firstDayOfWeekFromSunday - 1) % 7;
  86. // Number of days between the first day of week appearing on the calendar,
  87. // and the day corresponding to the 1-st of the month.
  88. return (weekdayFromMonday - firstDayOfWeekFromMonday) % 7;
  89. }
  90. }