Firebase 添加带有表日历抖动的侦听器
嗨,我对flifter(尤其是编码)是个新手,我正在尝试用它制作日历。我正在寻找一种在我的事件(保存在Firebase中)上有开始和结束日期的方法。小部件中内置的OnDaySelected函数似乎对此不起作用,但我认为解决方案是在_controller.selectedDay中添加一个侦听器,以便每次单击日期时都可以调用特定的函数 我尝试过值通知程序,但它被调用为null,我不知道为什么。我可以使用_controller.selectedDay将日期传递到AddSchedule页面,因此我不确定为什么实际日历上的值无法显示。我是否执行ValueNotifier错误,或者是否有其他方法来执行此操作Firebase 添加带有表日历抖动的侦听器,firebase,flutter,google-cloud-firestore,calendar,listener,Firebase,Flutter,Google Cloud Firestore,Calendar,Listener,嗨,我对flifter(尤其是编码)是个新手,我正在尝试用它制作日历。我正在寻找一种在我的事件(保存在Firebase中)上有开始和结束日期的方法。小部件中内置的OnDaySelected函数似乎对此不起作用,但我认为解决方案是在_controller.selectedDay中添加一个侦听器,以便每次单击日期时都可以调用特定的函数 我尝试过值通知程序,但它被调用为null,我不知道为什么。我可以使用_controller.selectedDay将日期传递到AddSchedule页面,因此我不确定
class SchedulePage extends StatefulWidget {
@override
_SchedulePageState createState() => _SchedulePageState();
}
class _SchedulePageState extends State<SchedulePage> {
CalendarController _controller;
List routes = [];
List locations = [];
List distinctLocations = [];
List filter = [];
List lastFilter = [];
List selectedEvents = [];
var daySelect;
@override
void initState() {
super.initState();
getRoutes();
_controller = CalendarController();
daySelect = ValueNotifier<DateTime>(_controller.selectedDay);
daySelect.value.notifyListeners();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
getRoutes() async {
final data = await FirebaseFirestore.instance
.collection("Routes")
.orderBy("driver")
.get();
List routeList = List();
for (int i = 0; i < data.docs.length; i++) {
routeList.add(data.docs[i]);
}
routes = routeList;
filter = routes
.where((element) => element['startDate'].toDate().isBefore(_controller.selectedDay)
&& element['endDate'].toDate().isAfter(_controller.selectedDay)).toList();
locations = filter.map((element) => element["locationName"].toString()).toList();
distinctLocations = locations.toSet().toList();
setState(() {});
}
getFilter(DateTime day) {
filter = routes
.where((element) => element['startDate'].toDate().isBefore(day)
&& element['endDate'].toDate().isAfter(day)).toList();
locations = filter.map((element) => element["locationName"].toString()).toList();
distinctLocations = locations.toSet().toList();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[900],
appBar: AppBar(...),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
height: 210,
decoration: BoxDecoration(color: Colors.grey[900], boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.8),
blurRadius: 10.0,
offset: Offset(0, 10),
),
]),
child: TableCalendar(
calendarController: _controller,
initialCalendarFormat: CalendarFormat.twoWeeks,
calendarStyle: CalendarStyle(
todayColor: Colors.red.withOpacity(0.7),
selectedColor: Color(0xFFF00F12),
weekendStyle: TextStyle(
color: Colors.red, fontWeight: FontWeight.w600),
markersColor: Colors.white,
markersMaxAmount: 1,
weekdayStyle: TextStyle(
color: Colors.white, fontWeight: FontWeight.w600)),
daysOfWeekStyle: DaysOfWeekStyle(
weekdayStyle: TextStyle(
color: Colors.white.withOpacity(0.5),
fontWeight: FontWeight.w600),
),
headerStyle: HeaderStyle(
formatButtonVisible: false,
centerHeaderTitle: true,
titleTextStyle: TextStyle(
color: Colors.red,
fontSize: 19.0,
),
leftChevronIcon:
Icon(Icons.chevron_left, color: Colors.white),
rightChevronIcon: Icon(
Icons.chevron_right,
color: Colors.white,
)),
),
),
SingleChildScrollView(
child: Container(
height: 800,
color: Colors.black38,
child: ValueListenableBuilder(
valueListenable: daySelect,
builder: (context, n, c){
return FutureBuilder(
future: getFilter(daySelect.value),
builder: (context, w) {
if(!w.hasData) {
return _buildSchedule();
} else {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.white,
));
}
});
},
),
),
),
],
),
),
floatingActionButton: Container(
height: 45,
width: 45,
child: FittedBox(
child: FloatingActionButton(
backgroundColor: Colors.white,
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(builder: (_) {
return addSchedule(_controller.selectedDay);
}));
},
child: Text(...),
),
),
),
);
}
class SchedulePage扩展StatefulWidget{
@凌驾
_SchedulePageState createState()=>SchedulePageState();
}
类(SchedulePageState扩展状态){
日历控制器\u控制器;
列出路由=[];
列出地点=[];
列表distinctlocation=[];
列表过滤器=[];
列表lastFilter=[];
列出所选事件=[];
var-daySelect;
@凌驾
void initState(){
super.initState();
getRoutes();
_控制器=日历控制器();
daySelect=ValueNotifier(\u controller.selectedDay);
daySelect.value.notifyListeners();
}
@凌驾
无效处置(){
_controller.dispose();
super.dispose();
}
getRoutes()异步{
最终数据=等待FirebaseFirestore.instance
.收集(“路线”)
.orderBy(“司机”)
.get();
List routeList=List();
对于(int i=0;ielement['startDate'].toDate().isBefore(_controller.selectedDay)
&&元素['endDate'].toDate().isAfter(_controller.selectedDay)).toList();
locations=filter.map((element)=>element[“locationName”].toString()).toList();
distinctLocations=locations.toSet().toList();
setState((){});
}
getFilter(日期时间日){
过滤器=路由
.where((element)=>element['startDate'].toDate().isBefore(day)
&&元素['endDate'].toDate().isAfter(day)).toList();
locations=filter.map((element)=>element[“locationName”].toString()).toList();
distinctLocations=locations.toSet().toList();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:颜色。灰色[900],
appBar:appBar(…),
正文:SingleChildScrollView(
子:列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
容器(
身高:210,
装饰:框装饰(颜色:Colors.灰色[900],框阴影:[
箱形阴影(
颜色:颜色。黑色。不透明度(0.8),
半径:10.0,
偏移量:偏移量(0,10),
),
]),
孩子:台历(
日历控制器:_控制器,
initialCalendarFormat:CalendarFormat.twoWeeks,
calendarStyle:calendarStyle(
今天的颜色:颜色。红色。不透明度(0.7),
selectedColor:Color(0xFFF00F12),
weekendStyle:TextStyle(
颜色:Colors.red,fontWeight:fontWeight.w600),
markersColor:Colors.white,
markersMaxAmount:1,
weekdayStyle:TextStyle(
颜色:Colors.white,fontWeight:fontWeight.w600),
daysOfWeekStyle:daysOfWeekStyle(
weekdayStyle:TextStyle(
颜色:颜色。白色。不透明度(0.5),
fontWeight:fontWeight.w600),
),
headerStyle:headerStyle(
formatButtonVisible:false,
是的,
titleTextStyle:TextStyle(
颜色:颜色,红色,
字体大小:19.0,
),
左雪佛龙:
图标(Icons.chevron_左,颜色:Colors.white),
右雪佛龙:图标(
Icons.chevron_右,
颜色:颜色,白色,
)),
),
),
SingleChildScrollView(
子:容器(
身高:800,
颜色:颜色。黑色38,
子项:ValueListenableBuilder(
valueListenable:daySelect,
生成器:(上下文,n,c){
回归未来建设者(
未来:getFilter(daySelect.value),
生成器:(上下文,w){
如果(!w.hasData){
返回_buildSchedule();
}否则{
返回中心(
子对象:循环压缩机指示器(
背景颜色:Colors.white,
));
}
});
},
),
),
),
],
),
),
浮动操作按钮:容器(
身高:45,
宽度:45,
孩子:FittedBox(
子:浮动操作按钮(
背景颜色:Colors.white,
已按下:(){
Navigator.of(context).push(MaterialPageRoute(builder:){
返回addSchedule(_controller.selectedDay);
}));
},
子项:文本(…),
),
),
),
);
}
您可以使用带有设置状态的onDaySelected
来更改变量内容
onDaySelected:(日期、事件){
设置状态(){
_今天=日期