Skip to content

Commit d52979e

Browse files
Update main.dart
Sample updated.
1 parent 42219a7 commit d52979e

File tree

1 file changed

+70
-36
lines changed

1 file changed

+70
-36
lines changed

lib/main.dart

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class LoadDataFromFireBase extends StatelessWidget {
1414
@override
1515
Widget build(BuildContext context) {
1616
return MaterialApp(
17+
debugShowCheckedModeBanner: false,
1718
title: 'FireBase',
1819
home: LoadDataFromFireStore(),
1920
);
@@ -29,6 +30,7 @@ class LoadDataFromFireStoreState extends State<LoadDataFromFireStore> {
2930
DataSnapshot querySnapshot;
3031
dynamic data;
3132
List<Color> _colorCollection;
33+
final List<String> options = <String>['Add', 'Delete'];
3234

3335
@override
3436
void initState() {
@@ -46,7 +48,38 @@ class LoadDataFromFireStoreState extends State<LoadDataFromFireStore> {
4648
@override
4749
Widget build(BuildContext context) {
4850
return Scaffold(
49-
51+
appBar: AppBar(
52+
leading: PopupMenuButton<String>(
53+
icon: Icon(Icons.settings),
54+
itemBuilder: (BuildContext context) => options.map((String choice) {
55+
return PopupMenuItem<String>(
56+
value: choice,
57+
child: Text(choice),
58+
);
59+
}).toList(),
60+
onSelected: (String value) {
61+
if (value == 'Add') {
62+
final dbRef =
63+
FirebaseDatabase.instance.reference().child("CalendarData");
64+
dbRef.push().set({
65+
"StartTime": '07/04/2020 07:00:00',
66+
"EndTime": '07/04/2020 08:00:00',
67+
"Subject": 'NewMeeting',
68+
"ResourceId": '0001'
69+
}).then((_) {
70+
Scaffold.of(context).showSnackBar(
71+
SnackBar(content: Text('Successfully Added')));
72+
}).catchError((onError) {
73+
print(onError);
74+
});
75+
} else if (value == 'Delete') {
76+
final dbRef =
77+
FirebaseDatabase.instance.reference().child("CalendarData");
78+
dbRef.remove();
79+
}
80+
},
81+
),
82+
),
5083
body: _showCalendar(),
5184
);
5285
}
@@ -67,45 +100,27 @@ class LoadDataFromFireStoreState extends State<LoadDataFromFireStore> {
67100
isAllDay: false,
68101
from: DateFormat('dd/MM/yyyy HH:mm:ss').parse(data['StartTime']),
69102
to: DateFormat('dd/MM/yyyy HH:mm:ss').parse(data['EndTime']),
70-
background: _colorCollection[random.nextInt(9)]));
103+
background: _colorCollection[random.nextInt(9)],
104+
resourceId: data['ResourceId']));
71105
}
72106
} else {
73107
return Center(
74108
child: CircularProgressIndicator(),
75109
);
76110
}
77111

78-
return SafeArea(
79-
child: Column(
80-
children: [
81-
Container(
82-
height: 400,
83-
child: SfCalendar(
84-
view: CalendarView.month,
85-
initialDisplayDate: DateTime(2020, 4, 5, 9, 0, 0),
86-
dataSource: _getCalendarDataSource(collection),
87-
monthViewSettings: MonthViewSettings(showAgenda: true),
88-
),
89-
),
90-
RaisedButton(onPressed: () {
91-
final dbRef = FirebaseDatabase.instance.reference().child("CalendarData");
92-
dbRef.push().set({
93-
"StartTime": '07/04/2020 07:00:00',
94-
"EndTime": '07/04/2020 08:00:00',
95-
"Subject":'NewMeeting',
96-
}).then((_) {
97-
Scaffold.of(context).showSnackBar(
98-
SnackBar(content: Text('Successfully Added')));
99-
}).catchError((onError) {
100-
print(onError);
101-
});
102-
}, child: Text("Add")),
103-
RaisedButton(onPressed: () {
104-
final dbRef = FirebaseDatabase.instance.reference().child("CalendarData");
105-
dbRef.remove();
106-
}, child: Text("Delete")),
107-
],
108-
));
112+
return SfCalendar(
113+
view: CalendarView.timelineDay,
114+
allowedViews: [
115+
CalendarView.timelineDay,
116+
CalendarView.timelineWeek,
117+
CalendarView.timelineWorkWeek,
118+
CalendarView.timelineMonth,
119+
],
120+
initialDisplayDate: DateTime(2020, 4, 5, 9, 0, 0),
121+
dataSource: _getCalendarDataSource(collection),
122+
monthViewSettings: MonthViewSettings(showAgenda: true),
123+
);
109124
}
110125
}
111126

@@ -126,12 +141,19 @@ class LoadDataFromFireStoreState extends State<LoadDataFromFireStore> {
126141

127142
MeetingDataSource _getCalendarDataSource([List<Meeting> collection]) {
128143
List<Meeting> meetings = collection ?? <Meeting>[];
129-
return MeetingDataSource(meetings);
144+
List<CalendarResource> resourceColl = <CalendarResource>[];
145+
resourceColl.add(CalendarResource(
146+
displayName: 'John',
147+
id: '0001',
148+
color: Colors.red,
149+
));
150+
return MeetingDataSource(meetings, resourceColl);
130151
}
131152

132153
class MeetingDataSource extends CalendarDataSource {
133-
MeetingDataSource(List<Meeting> source) {
154+
MeetingDataSource(List<Meeting> source, List<CalendarResource> resourceColl) {
134155
appointments = source;
156+
resources = resourceColl;
135157
}
136158

137159
@override
@@ -158,6 +180,11 @@ class MeetingDataSource extends CalendarDataSource {
158180
Color getColor(int index) {
159181
return appointments[index].background;
160182
}
183+
184+
@override
185+
List<Object> getResourceIds(int index) {
186+
return [appointments[index].resourceId];
187+
}
161188
}
162189

163190
getDataFromDatabase() async {
@@ -167,11 +194,18 @@ getDataFromDatabase() async {
167194
}
168195

169196
class Meeting {
170-
Meeting({this.eventName, this.from, this.to, this.background, this.isAllDay});
197+
Meeting(
198+
{this.eventName,
199+
this.from,
200+
this.to,
201+
this.background,
202+
this.isAllDay,
203+
this.resourceId});
171204

172205
String eventName;
173206
DateTime from;
174207
DateTime to;
175208
Color background;
176209
bool isAllDay;
210+
String resourceId;
177211
}

0 commit comments

Comments
 (0)