YSTableCalendar.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import 'package:flutter/material.dart';
  2. import 'package:table_calendar/table_calendar.dart';
  3. import 'package:ysairplane2/tools/YSNetWorking.dart';
  4. import 'package:ysairplane2/tools/YSTools.dart';
  5. class YSTableCalendar extends StatefulWidget {
  6. final orderSn;
  7. final ValueSetter postDate;
  8. const YSTableCalendar({Key key, this.orderSn, this.postDate}) : super(key: key);
  9. @override
  10. _YSTableCalendarState createState() => _YSTableCalendarState();
  11. }
  12. class _YSTableCalendarState extends State<YSTableCalendar> {
  13. CalendarController _calendarController = CalendarController();
  14. List _dataArray = [];
  15. String _chooseDate;
  16. @override
  17. void initState() {
  18. DateTime now = DateTime.now();
  19. _chooseDate = now.year.toString()+'-'+now.month.toString().padLeft(2,'0')+'-'+now.day.toString().padLeft(2,'0');
  20. Future.delayed(Duration(seconds: 0)).then((value){
  21. _getDatePriceData();
  22. });
  23. super.initState();
  24. }
  25. @override
  26. void dispose() {
  27. _calendarController.dispose();
  28. super.dispose();
  29. }
  30. _getDatePriceData() async{
  31. Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/airplaneorder/ticketChanging',
  32. parameter: {'orderSn':widget.orderSn},isLoading: false,isToken: true);
  33. if(dict!=null){
  34. _dataArray = dict['data'];
  35. setState(() {});
  36. }
  37. }
  38. @override
  39. Widget build(BuildContext context) {
  40. return Container(
  41. height: MediaQuery.of(context).size.height*0.6,
  42. decoration: BoxDecoration(
  43. color: Colors.white,
  44. borderRadius: BorderRadius.only(topRight: Radius.circular(10),topLeft: Radius.circular(10))
  45. ),
  46. child: Column(
  47. children: [
  48. Container(
  49. height: MediaQuery.of(context).size.height*0.6-hsp(100),
  50. child: TableCalendar(
  51. calendarController: _calendarController,
  52. locale: 'zh_CN',
  53. headerStyle: HeaderStyle(
  54. formatButtonVisible: false,
  55. centerHeaderTitle: true
  56. ),
  57. // startDay: DateTime.now(),
  58. // endDay: DateTime.now().add(Duration(days: _dataArray.length)),
  59. enabledDayPredicate: (date){
  60. String dateStr = date.year.toString()+'-'+date.month.toString().padLeft(2,'0')+'-'+date.day.toString().padLeft(2,'0');
  61. return _dataArray.where((element) => '${element['time']}'.contains(dateStr)).length>0;
  62. },
  63. builders: CalendarBuilders(
  64. dayBuilder: (context,date,dayBuilder){
  65. return Container(
  66. alignment: Alignment.center,
  67. child: Text('${date.day}'),
  68. );
  69. },
  70. selectedDayBuilder: (context,date,dayBuilder){
  71. _chooseDate = date.year.toString()+'-'+date.month.toString().padLeft(2,'0')+'-'+date.day.toString().padLeft(2,'0');
  72. Map item = _dataArray.firstWhere((element) => element['time']==_chooseDate,orElse: ()=>null);
  73. return Container(
  74. alignment: Alignment.center,
  75. decoration: BoxDecoration(
  76. color: Color(0xFF0677FF),
  77. borderRadius: BorderRadius.all(Radius.circular(5))
  78. ),
  79. child: Column(
  80. mainAxisSize: MainAxisSize.min,
  81. children: [
  82. Text('${date.day}',style: TextStyle(color: Colors.white),),
  83. if(item!=null)Text('¥${item['price']}',style: TextStyle(color: Colors.white.withOpacity(0.6),fontSize: zsp(20)),),
  84. ],
  85. ),
  86. );
  87. },
  88. unavailableDayBuilder: (context,date,dayBuilder){
  89. return Container(
  90. alignment: Alignment.center,
  91. child: Text('${date.day}',style: TextStyle(color: Color(0xFFC8C9CC)),),
  92. );
  93. },
  94. ),
  95. ),
  96. ),
  97. GestureDetector(
  98. onTap: (){
  99. Navigator.pop(context);
  100. widget.postDate(_chooseDate);
  101. },
  102. child: Container(
  103. height: hsp(100),
  104. margin: EdgeInsets.only(left: hsp(50),right: hsp(50)),
  105. decoration: BoxDecoration(
  106. color: Color(0xFF0677FF),
  107. borderRadius: BorderRadius.all(Radius.circular(50))
  108. ),
  109. alignment: Alignment.center,
  110. child: Text('确定',style: TextStyle(fontSize: zsp(30),color: Colors.white),),
  111. ),
  112. )
  113. ],
  114. ),
  115. );
  116. }
  117. }