import 'package:flutter/material.dart'; import 'package:table_calendar/table_calendar.dart'; import 'package:ysairplane2/tools/YSNetWorking.dart'; import 'package:ysairplane2/tools/YSTools.dart'; class YSTableCalendar extends StatefulWidget { final orderSn; final ValueSetter postDate; const YSTableCalendar({Key key, this.orderSn, this.postDate}) : super(key: key); @override _YSTableCalendarState createState() => _YSTableCalendarState(); } class _YSTableCalendarState extends State { CalendarController _calendarController = CalendarController(); List _dataArray = []; String _chooseDate; @override void initState() { DateTime now = DateTime.now(); _chooseDate = now.year.toString()+'-'+now.month.toString().padLeft(2,'0')+'-'+now.day.toString().padLeft(2,'0'); Future.delayed(Duration(seconds: 0)).then((value){ _getDatePriceData(); }); super.initState(); } @override void dispose() { _calendarController.dispose(); super.dispose(); } _getDatePriceData() async{ Map dict = await ysRequestHttp(context,type: requestType.get,api: '/app/applets/airplaneorder/ticketChanging', parameter: {'orderSn':widget.orderSn},isLoading: false,isToken: true); if(dict!=null){ _dataArray = dict['data']; setState(() {}); } } @override Widget build(BuildContext context) { return Container( height: MediaQuery.of(context).size.height*0.6, decoration: BoxDecoration( color: Colors.white, borderRadius: BorderRadius.only(topRight: Radius.circular(10),topLeft: Radius.circular(10)) ), child: Column( children: [ Container( height: MediaQuery.of(context).size.height*0.6-hsp(100), child: TableCalendar( calendarController: _calendarController, locale: 'zh_CN', headerStyle: HeaderStyle( formatButtonVisible: false, centerHeaderTitle: true ), // startDay: DateTime.now(), // endDay: DateTime.now().add(Duration(days: _dataArray.length)), enabledDayPredicate: (date){ String dateStr = date.year.toString()+'-'+date.month.toString().padLeft(2,'0')+'-'+date.day.toString().padLeft(2,'0'); return _dataArray.where((element) => '${element['time']}'.contains(dateStr)).length>0; }, builders: CalendarBuilders( dayBuilder: (context,date,dayBuilder){ return Container( alignment: Alignment.center, child: Text('${date.day}'), ); }, selectedDayBuilder: (context,date,dayBuilder){ _chooseDate = date.year.toString()+'-'+date.month.toString().padLeft(2,'0')+'-'+date.day.toString().padLeft(2,'0'); Map item = _dataArray.firstWhere((element) => element['time']==_chooseDate,orElse: ()=>null); return Container( alignment: Alignment.center, decoration: BoxDecoration( color: Color(0xFF0677FF), borderRadius: BorderRadius.all(Radius.circular(5)) ), child: Column( mainAxisSize: MainAxisSize.min, children: [ Text('${date.day}',style: TextStyle(color: Colors.white),), if(item!=null)Text('¥${item['price']}',style: TextStyle(color: Colors.white.withOpacity(0.6),fontSize: zsp(20)),), ], ), ); }, unavailableDayBuilder: (context,date,dayBuilder){ return Container( alignment: Alignment.center, child: Text('${date.day}',style: TextStyle(color: Color(0xFFC8C9CC)),), ); }, ), ), ), GestureDetector( onTap: (){ Navigator.pop(context); widget.postDate(_chooseDate); }, child: Container( height: hsp(100), margin: EdgeInsets.only(left: hsp(50),right: hsp(50)), decoration: BoxDecoration( color: Color(0xFF0677FF), borderRadius: BorderRadius.all(Radius.circular(50)) ), alignment: Alignment.center, child: Text('确定',style: TextStyle(fontSize: zsp(30),color: Colors.white),), ), ) ], ), ); } }