@@ -7,137 +7,149 @@ import 'package:eventide_example/calendar/ui/calendar_form.dart';
77import 'package:eventide_example/event_list/logic/event_list_cubit.dart' ;
88import 'package:value_state/value_state.dart' ;
99
10- class CalendarScreen extends StatelessWidget {
10+ class CalendarScreen extends StatefulWidget {
1111 const CalendarScreen ({super .key});
1212
13+ @override
14+ State <CalendarScreen > createState () => _CalendarScreenState ();
15+ }
16+
17+ class _CalendarScreenState extends State <CalendarScreen > {
18+ bool onlyWritableCalendars = true ;
19+
1320 @override
1421 Widget build (BuildContext context) {
15- return SafeArea (
16- child: BlocBuilder <CalendarCubit , Value <List <ETCalendar >>>(builder: (context, state) {
17- return Stack (
18- children: [
19- CustomScrollView (slivers: [
20- SliverAppBar (
21- pinned: true ,
22- title: const Text ('Calendar plugin example app' ),
23- actions: [
24- IconButton (
25- icon: const Icon (Icons .add),
26- onPressed: () {
27- showDialog (
28- context: context,
29- builder: (context) => AlertDialog (
30- title: const Text ('Create calendar' ),
31- content: Padding (
32- padding: const EdgeInsets .symmetric (horizontal: 16.0 ),
33- child: CalendarForm (
34- onSubmit: (title, color) async {
35- await BlocProvider .of <CalendarCubit >(context)
36- .createCalendar (title: title, color: color);
37- },
38- ),
22+ return Scaffold (
23+ body: SafeArea (
24+ child: BlocBuilder <CalendarCubit , Value <List <ETCalendar >>>(
25+ builder: (context, state) => Stack (
26+ children: [
27+ CustomScrollView (
28+ slivers: [
29+ SliverAppBar (
30+ pinned: true ,
31+ title: const Text ('Eventide' ),
32+ actions: [
33+ Row (
34+ children: [
35+ const Text ('writable only' ),
36+ const SizedBox (width: 8 ),
37+ Switch (
38+ value: onlyWritableCalendars,
39+ onChanged: (value) {
40+ setState (() {
41+ onlyWritableCalendars = value;
42+ });
43+ BlocProvider .of <CalendarCubit >(context).fetchCalendars (onlyWritable: value);
44+ },
3945 ),
40- ),
41- );
42- },
46+ ],
47+ ),
48+ IconButton (
49+ icon: const Icon (Icons .refresh),
50+ onPressed: () =>
51+ BlocProvider .of <CalendarCubit >(context).fetchCalendars (onlyWritable: onlyWritableCalendars),
52+ ),
53+ ],
4354 ),
44- ],
45- ),
46- if (state case Value (: final data? ))
47- SliverList (
48- delegate: SliverChildListDelegate (
49- data
50- .map ((calendar) => SizedBox (
51- height: 50 ,
52- child: Padding (
53- padding: const EdgeInsets .symmetric (horizontal: 16 ),
54- child: InkWell (
55- onTap: () async {
56- try {
57- await BlocProvider .of <EventListCubit >(context).selectCalendar (calendar);
58- if (context.mounted) {
59- Navigator .of (context).push (
60- MaterialPageRoute (builder: (context) => const EventList ()),
61- );
62- }
63- } catch (error) {
64- if (context.mounted) {
65- ScaffoldMessenger .of (context)
66- .showSnackBar (SnackBar (content: Text ('Error: ${error .toString ()}' )));
67- }
68- }
69- },
70- child: Row (
71- children: [
72- Container (
73- color: calendar.color,
74- width: 16 ,
75- height: 16 ,
55+ if (state case Value (: final data? ))
56+ SliverList (
57+ delegate: SliverChildListDelegate (
58+ data
59+ .map ((calendar) => SizedBox (
60+ height: 50 ,
61+ child: Padding (
62+ padding: const EdgeInsets .symmetric (horizontal: 16 ),
63+ child: InkWell (
64+ onTap: () async {
65+ try {
66+ await BlocProvider .of <EventListCubit >(context).selectCalendar (calendar);
67+ if (context.mounted) {
68+ Navigator .of (context).push (
69+ MaterialPageRoute (builder: (context) => const EventList ()),
70+ );
71+ }
72+ } catch (error) {
73+ if (context.mounted) {
74+ ScaffoldMessenger .of (context)
75+ .showSnackBar (SnackBar (content: Text ('Error: ${error .toString ()}' )));
76+ }
77+ }
78+ },
79+ child: Row (
80+ children: [
81+ Container (
82+ color: calendar.color,
83+ width: 16 ,
84+ height: 16 ,
85+ ),
86+ const SizedBox (width: 16 ),
87+ Expanded (
88+ child: Text (
89+ calendar.title,
90+ maxLines: 3 ,
91+ overflow: TextOverflow .fade,
92+ ),
93+ ),
94+ const SizedBox (width: 16 ),
95+ if (calendar.isWritable)
96+ IconButton (
97+ icon: const Icon (Icons .delete),
98+ onPressed: () {
99+ BlocProvider .of <CalendarCubit >(context).deleteCalendar (calendar.id);
100+ },
101+ ),
102+ const SizedBox (width: 16 ),
103+ const Icon (Icons .arrow_right),
104+ ],
76105 ),
77- const SizedBox (width: 16 ),
78- Expanded (
79- child: Text (
80- calendar.title,
81- maxLines: 3 ,
82- overflow: TextOverflow .fade,
83- ),
84- ),
85- const SizedBox (width: 16 ),
86- if (calendar.isWritable)
87- IconButton (
88- icon: const Icon (Icons .delete),
89- onPressed: () {
90- BlocProvider .of <CalendarCubit >(context).deleteCalendar (calendar.id);
91- },
92- ),
93- const SizedBox (width: 16 ),
94- const Icon (Icons .arrow_right),
95- ],
106+ ),
96107 ),
97- ),
98- ),
99- ))
100- .toList (),
101- ),
102- ),
103- SliverToBoxAdapter (
104- child: Column (
105- crossAxisAlignment: CrossAxisAlignment .center,
106- mainAxisAlignment: MainAxisAlignment .center,
107- children: [
108- if (state case Value (: final data? ) when data.isEmpty) ...[
109- const Text ('No calendars found' ),
110- const SizedBox (height: 16 ),
111- ],
112- if (state case Value (: final error? )) ...[
113- Text ('Error: ${error .toString ()}' ),
114- const SizedBox (height: 16 ),
115- ],
116- ],
117- ),
118- ),
119- ]),
120- Positioned (
121- right: 16 ,
122- bottom: 16 ,
123- child: Column (
124- crossAxisAlignment: CrossAxisAlignment .end,
125- children: [
126- ElevatedButton (
127- onPressed: () => BlocProvider .of <CalendarCubit >(context).fetchCalendars (onlyWritable: true ),
128- child: const Text ('Writable calendars' ),
129- ),
130- const SizedBox (height: 8 ),
131- ElevatedButton (
132- onPressed: () => BlocProvider .of <CalendarCubit >(context).fetchCalendars (onlyWritable: false ),
133- child: const Text ('All calendars' ),
108+ ))
109+ .toList (),
110+ ),
111+ ),
112+ SliverToBoxAdapter (
113+ child: Column (
114+ crossAxisAlignment: CrossAxisAlignment .center,
115+ mainAxisAlignment: MainAxisAlignment .center,
116+ children: [
117+ if (state case Value (: final data? ) when data.isEmpty) ...[
118+ const Text ('No calendars found' ),
119+ const SizedBox (height: 16 ),
120+ ],
121+ if (state case Value (: final error? )) ...[
122+ Text ('Error: ${error .toString ()}' ),
123+ const SizedBox (height: 16 ),
124+ ],
125+ ],
126+ ),
134127 ),
135128 ],
136129 ),
130+ ],
131+ ),
132+ ),
133+ ),
134+ floatingActionButton: FloatingActionButton (
135+ child: const Icon (Icons .add),
136+ onPressed: () {
137+ showDialog (
138+ context: context,
139+ builder: (context) => AlertDialog (
140+ title: const Text ('Create calendar' ),
141+ content: Padding (
142+ padding: const EdgeInsets .symmetric (horizontal: 16.0 ),
143+ child: CalendarForm (
144+ onSubmit: (title, color) async {
145+ await BlocProvider .of <CalendarCubit >(context).createCalendar (title: title, color: color);
146+ },
147+ ),
148+ ),
137149 ),
138- ],
139- );
140- } ),
150+ );
151+ },
152+ ),
141153 );
142154 }
143155}
0 commit comments