作为Flutter的初学者,我正在尝试构建一个日历应用程序,在该应用程序中我想从Firestore数据库中获取特定日期的数据。现在有一个_onDaySelected函数,可在flutter控制台中从日历中打印选定的日期,例如“ flutter:2019-11-04”。我希望将此String传递给MessagesStream类,这样我可以调用.document('$ currentDay')而不是像这样对日期进行硬编码:.document('2019-11-04')。我已包含以下代码。
有人知道怎么做吗?任何帮助将不胜感激!
class RoosterTest extends StatefulWidget {
@override
_RoosterTestState createState() => _RoosterTestState();
}
class _RoosterTestState extends State<RoosterTest>
with TickerProviderStateMixin {
Map<DateTime,List> _events;
List _selectedEvents;
AnimationController _animationController;
CalendarController _calendarController;
final messageTextController = TextEditingController();
String messageText;
static final now = DateTime.now();
static final formatter = DateFormat('yyyy-MM-dd');
static final formatted = formatter.format(now);
@override
void initState() {
super.initState();
final _selectedDay = DateTime.now();
_events = {};
_selectedEvents = _events[_selectedDay] ?? [];
_calendarController = CalendarController();
_animationController = AnimationController(
vsync: this,duration: const Duration(milliseconds: 400),);
_animationController.forward();
}
@override
void dispose() {
_animationController.dispose();
_calendarController.dispose();
super.dispose();
}
void _onDaySelected(DateTime day,List events) {
String currentDay = formatter.format(day).toString();
print('$currentDay');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisSize: MainAxisSize.max,children: <Widget>[
_buildTableCalendar(),const SizedBox(
height: 8.0,),MessagesStream(),],);
}
Widget _buildTableCalendar() {
return TableCalendar(
calendarController: _calendarController,events: _events,startingDayOfWeek: StartingDayOfWeek.monday,calendarStyle: CalendarStyle(
selectedColor: Colors.red,todayColor: Colors.blue,markersColor: Colors.green,outsideDaysVisible: false,weekendStyle: TextStyle().copyWith(color: Colors.red),headerStyle: HeaderStyle(
formatButtonTextStyle:
TextStyle().copyWith(color: Colors.white,fontSize: 15.0),formatButtonDecoration: BoxDecoration(
color: Colors.red,borderRadius: BorderRadius.circular(16.0),onDaySelected: _onDaySelected,onVisibleDaysChanged: _onVisibleDaysChanged,);
}
class MessagesStream extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<Querysnapshot>(
stream: _firestore
.collection('days')
.document('2019-11-04')
.collection('hours')
.snapshots(),builder: (context,snapshot) {
if (!snapshot.hasData)
return CircularProgressIndicator(
backgroundColor: Colors.lightBlueaccent,);
return _buildList(context,snapshot.data.documents);
},);
}