|
| 1 | +abstract class BasePickerModel { |
| 2 | + String leftStringAtIndex(int index); |
| 3 | + String middleStringAtIndex(int index); |
| 4 | + String rightStringAtIndex(int index); |
| 5 | + void setLeftIndex(int index); |
| 6 | + void setMiddleIndex(int index); |
| 7 | + void setRightIndex(int index); |
| 8 | + int currentLeftIndex(); |
| 9 | + int currentMiddleIndex(); |
| 10 | + int currentRightIndex(); |
| 11 | + DateTime finalTime(); |
| 12 | +} |
| 13 | + |
| 14 | +class CommonPickerModel extends BasePickerModel { |
| 15 | + List<String> leftList; |
| 16 | + List<String> middleList; |
| 17 | + List<String> rightList; |
| 18 | + DateTime currentTime; |
| 19 | + int _currentLeftIndex; |
| 20 | + int _currentMiddleIndex; |
| 21 | + int _currentRightIndex; |
| 22 | + |
| 23 | + String locale; |
| 24 | + CommonPickerModel({this.currentTime, locale}) : this.locale = locale ?? 'cn'; |
| 25 | + |
| 26 | + @override |
| 27 | + String leftStringAtIndex(int index) { |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + @override |
| 32 | + String middleStringAtIndex(int index) { |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + @override |
| 37 | + String rightStringAtIndex(int index) { |
| 38 | + return null; |
| 39 | + } |
| 40 | + |
| 41 | + @override |
| 42 | + int currentLeftIndex() { |
| 43 | + return _currentLeftIndex; |
| 44 | + } |
| 45 | + |
| 46 | + @override |
| 47 | + int currentMiddleIndex() { |
| 48 | + return _currentMiddleIndex; |
| 49 | + } |
| 50 | + |
| 51 | + @override |
| 52 | + int currentRightIndex() { |
| 53 | + return _currentRightIndex; |
| 54 | + } |
| 55 | + |
| 56 | + @override |
| 57 | + void setLeftIndex(int index) { |
| 58 | + _currentLeftIndex = index; |
| 59 | + } |
| 60 | + |
| 61 | + @override |
| 62 | + void setMiddleIndex(int index) { |
| 63 | + _currentMiddleIndex = index; |
| 64 | + } |
| 65 | + |
| 66 | + @override |
| 67 | + void setRightIndex(int index) { |
| 68 | + _currentRightIndex = index; |
| 69 | + } |
| 70 | + |
| 71 | + @override |
| 72 | + DateTime finalTime() {} |
| 73 | +} |
| 74 | + |
| 75 | +class DatePickerModel extends CommonPickerModel { |
| 76 | + int maxYear; |
| 77 | + int minYear; |
| 78 | + |
| 79 | + List<int> _leapYearMonths = const <int>[1, 3, 5, 7, 8, 10, 12]; |
| 80 | + int _calcDateCount() { |
| 81 | + final currentYear = _currentLeftIndex + minYear; |
| 82 | + final currentMonth = _currentMiddleIndex + 1; |
| 83 | + |
| 84 | + if (_leapYearMonths.contains(currentMonth)) { |
| 85 | + return 31; |
| 86 | + } else if (currentMonth == 2) { |
| 87 | + if ((currentYear % 4 == 0 && currentYear % 100 != 0) || currentYear % 400 == 0) { |
| 88 | + return 29; |
| 89 | + } |
| 90 | + return 28; |
| 91 | + } |
| 92 | + return 30; |
| 93 | + } |
| 94 | + |
| 95 | + DatePickerModel({this.maxYear = 2050, this.minYear = 1970, DateTime currentTime, String locale}) |
| 96 | + : super(currentTime: currentTime ?? DateTime.now(), locale: locale) { |
| 97 | + _currentLeftIndex = this.currentTime.year - minYear; |
| 98 | + _currentMiddleIndex = this.currentTime.month - 1; |
| 99 | + _currentRightIndex = this.currentTime.day - 1; |
| 100 | + |
| 101 | + this.leftList = List.generate(maxYear - minYear + 1, (int index) { |
| 102 | + return '${minYear + index}${_localeYear()}'; |
| 103 | + }); |
| 104 | + this.middleList = List.generate(12, (int index) { |
| 105 | + return '${index + 1}${_localeMonth()}'; |
| 106 | + }); |
| 107 | + this.rightList = List.generate(_calcDateCount(), (int index) { |
| 108 | + return '${index + 1}${_localeDay()}'; |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + @override |
| 113 | + String leftStringAtIndex(int index) { |
| 114 | + if (index >= 0 && index < leftList.length) { |
| 115 | + return leftList[index]; |
| 116 | + } else { |
| 117 | + return null; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + @override |
| 122 | + String middleStringAtIndex(int index) { |
| 123 | + if (index >= 0 && index < middleList.length) { |
| 124 | + return middleList[index]; |
| 125 | + } else { |
| 126 | + return null; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + @override |
| 131 | + String rightStringAtIndex(int index) { |
| 132 | + if (index >= 0 && index < rightList.length) { |
| 133 | + return rightList[index]; |
| 134 | + } else { |
| 135 | + return null; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + String _localeYear() { |
| 140 | + if (locale.matchAsPrefix('cn') == null) { |
| 141 | + return ''; |
| 142 | + } else { |
| 143 | + return '年'; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + String _localeMonth() { |
| 148 | + if (locale.matchAsPrefix('cn') == null) { |
| 149 | + return ''; |
| 150 | + } else { |
| 151 | + return '月'; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + String _localeDay() { |
| 156 | + if (locale.matchAsPrefix('cn') == null) { |
| 157 | + return ''; |
| 158 | + } else { |
| 159 | + return '日'; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + @override |
| 164 | + DateTime finalTime() { |
| 165 | + final year = _currentLeftIndex + minYear; |
| 166 | + final month = _currentMiddleIndex + 1; |
| 167 | + final day = _currentRightIndex + 1; |
| 168 | + return DateTime(year, month, day); |
| 169 | + } |
| 170 | +} |
0 commit comments