YSDatePicker.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'YSTools.dart';
  4. class YSDatePicker extends StatefulWidget {
  5. final ValueSetter<String> choose;
  6. final int type;
  7. const YSDatePicker({Key? key, required this.choose, this.type = 1}) : super(key: key);
  8. @override
  9. YSDatePickerState createState() => YSDatePickerState();
  10. }
  11. class YSDatePickerState extends State<YSDatePicker> {
  12. String birthday = '';
  13. @override
  14. void initState() {
  15. DateTime now = DateTime.now();
  16. if(widget.type==1){
  17. birthday = '${now.year}-${now.month.toString().padLeft(2,'0')}';
  18. }else if(widget.type==2){
  19. birthday = '${now.year}-${now.month.toString().padLeft(2,'0')}-${now.day.toString().padLeft(2,'0')}';
  20. }else if(widget.type==3){
  21. birthday = '${now.year}-${now.month.toString().padLeft(2,'0')}-${now.day.toString().padLeft(2,'0')} ${now.hour.toString().padLeft(2,'0')}:${now.minute.toString().padLeft(2,'0')}';
  22. }
  23. super.initState();
  24. }
  25. @override
  26. Widget build(BuildContext context) {
  27. return Container(
  28. color: Colors.transparent,
  29. height: 280,
  30. child: Column(
  31. crossAxisAlignment: CrossAxisAlignment.end,
  32. children: [
  33. Container(
  34. height: 40,
  35. padding: const EdgeInsets.only(left: 20,right: 20),
  36. decoration: const BoxDecoration(
  37. color: Colors.white,
  38. border: Border(bottom: BorderSide(width: 0.5,color: Color(0xFFEEEEEE)))
  39. ),
  40. child: Row(
  41. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  42. children: [
  43. GestureDetector(
  44. child: const Text('取消',style: TextStyle(fontSize: 15,color: Color(0xFF999999)),),
  45. onTap: (){
  46. Navigator.pop(context);
  47. },
  48. ),
  49. GestureDetector(
  50. child: const Text('确定',style: TextStyle(fontSize: 15,color: Color(0xFF108DE9)),),
  51. onTap: (){
  52. widget.choose(birthday);
  53. Navigator.pop(context);
  54. },
  55. )
  56. ],
  57. ),
  58. ),
  59. Container(
  60. height: 240,
  61. padding: const EdgeInsets.all(10),
  62. decoration: const BoxDecoration(
  63. color: Colors.white,
  64. ),
  65. child: CupertinoDatePicker(
  66. initialDateTime: DateTime.now(),
  67. onDateTimeChanged: (date) {//+' '+date.hour.toString().padLeft(2,'0')+':'+date.minute.toString().padLeft(2,'0')
  68. if(widget.type==1){
  69. birthday = '${date.year}-${date.month.toString().padLeft(2,'0')}';
  70. }else if(widget.type==2){
  71. birthday = '${date.year}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')}';
  72. }else if(widget.type==3){
  73. birthday = '${date.year}-${date.month.toString().padLeft(2,'0')}-${date.day.toString().padLeft(2,'0')} ${date.hour.toString().padLeft(2,'0')}:${date.minute.toString().padLeft(2,'0')}';
  74. }
  75. },
  76. mode: widget.type==3?CupertinoDatePickerMode.dateAndTime:CupertinoDatePickerMode.date,
  77. ),
  78. ),
  79. ],
  80. ),
  81. );
  82. }
  83. }
  84. class YSMonthPicker extends StatefulWidget {
  85. final ValueSetter valueSetter;
  86. const YSMonthPicker({Key? key, required this.valueSetter}) : super(key: key);
  87. @override
  88. YSMonthPickerState createState() => YSMonthPickerState();
  89. }
  90. class YSMonthPickerState extends State<YSMonthPicker> {
  91. late FixedExtentScrollController _yearController;
  92. late FixedExtentScrollController _monthController;
  93. final List _yearArray = [];
  94. String _yearStr = '';
  95. final List _monthArray = [];
  96. String _monthStr = '';
  97. @override
  98. void initState() {
  99. int yNumber= 2000;
  100. for(int i=0;i<40;i++){
  101. yNumber++;
  102. _yearArray.add({'title':'$yNumber年','value':'$yNumber'});
  103. }
  104. int mNumber= 0;
  105. for(int i=0;i<12;i++){
  106. mNumber++;
  107. _monthArray.add({'title':'$mNumber月','value':'$mNumber'});
  108. }
  109. DateTime now = DateTime.now();
  110. _yearStr = '${now.year}';
  111. _monthStr = '${now.month}'.padLeft(2,'0');
  112. int yearIndex = _yearArray.indexWhere((element) => element['value']=='${now.year}');
  113. int monthIndex = _monthArray.indexWhere((element) => element['value']=='${now.month}');
  114. _yearController = FixedExtentScrollController(initialItem: yearIndex);
  115. _monthController = FixedExtentScrollController(initialItem: monthIndex);
  116. super.initState();
  117. }
  118. @override
  119. void dispose() {
  120. _yearController.dispose();
  121. _monthController.dispose();
  122. super.dispose();
  123. }
  124. @override
  125. Widget build(BuildContext context) {
  126. return ClipRRect(
  127. borderRadius: const BorderRadius.only(topLeft: Radius.circular(5),topRight: Radius.circular(5)),
  128. child: Container(
  129. color: Colors.transparent,
  130. height: hsp(300),
  131. child: Column(
  132. crossAxisAlignment: CrossAxisAlignment.end,
  133. children: [
  134. Container(
  135. height: hsp(50),
  136. padding: const EdgeInsets.only(left: 20,right: 20),
  137. decoration: const BoxDecoration(
  138. color: Colors.white,
  139. border: Border(bottom: BorderSide(width: 0.5,color: Color(0xFFEEEEEE))),
  140. ),
  141. child: Row(
  142. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  143. children: [
  144. GestureDetector(
  145. child: const Text('取消',style: TextStyle(fontSize: 15,color: Color(0xFF999999)),),
  146. onTap: (){
  147. Navigator.pop(context);
  148. },
  149. ),
  150. Text('选择年月',style: TextStyle(fontSize: zsp(16),color: const Color(0xFF323233)),),
  151. GestureDetector(
  152. child: const Text('确定',style: TextStyle(fontSize: 15,color: Color(0xFF108DE9)),),
  153. onTap: (){
  154. Map value = {'title':'$_yearStr年$_monthStr月','value':'$_yearStr年$_monthStr月'};
  155. widget.valueSetter(value);
  156. Navigator.pop(context);
  157. },
  158. )
  159. ],
  160. ),
  161. ),
  162. Container(
  163. height: hsp(250),
  164. padding: const EdgeInsets.all(10),
  165. decoration: const BoxDecoration(
  166. color: Colors.white,
  167. ),
  168. child: LayoutBuilder(
  169. builder: (context,conSize){
  170. return Row(
  171. children: [
  172. SizedBox(
  173. width: conSize.maxWidth/2,
  174. child: CupertinoPicker(
  175. onSelectedItemChanged: (int value) {
  176. _yearStr = _yearArray[value]['value'];
  177. },
  178. itemExtent: hsp(50),
  179. scrollController: _yearController,
  180. children: _yearArray.map((e) => Container(
  181. alignment: Alignment.center,
  182. child: Text(e['title']),
  183. )).toList(),
  184. ),
  185. ),
  186. SizedBox(
  187. width: conSize.maxWidth/2,
  188. child: CupertinoPicker(
  189. onSelectedItemChanged: (int value) {
  190. _monthStr = '${_monthArray[value]['value']}'.padLeft(2,'0');
  191. },
  192. itemExtent: hsp(50),
  193. scrollController: _monthController,
  194. children: _monthArray.map((e) => Container(
  195. alignment: Alignment.center,
  196. child: Text(e['title']),
  197. )).toList(),
  198. ),
  199. ),
  200. ],
  201. );
  202. },
  203. ),
  204. ),
  205. ],
  206. ),
  207. ),
  208. );
  209. }
  210. }