No Description

calendario.dart 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import 'package:flutter/material.dart';
  2. import 'package:fast_med_flutter/ui/pages/add_event.dart';
  3. import 'package:fast_med_flutter/ui/pages/view_event.dart';
  4. import 'package:table_calendar/table_calendar.dart';
  5. import 'package:firebase_core/firebase_core.dart';
  6. import 'model/event.dart';
  7. void main() async{
  8. WidgetsFlutterBinding.ensureInitialized();
  9. await Firebase.initializeApp();
  10. runApp(Calendario());
  11. }
  12. class Calendario extends StatelessWidget {
  13. @override
  14. Widget build(BuildContext context) {
  15. return MaterialApp(
  16. title: 'Citas Disponibles',
  17. theme: ThemeData(
  18. primarySwatch: Colors.blue,
  19. ),
  20. home: MyHomePage(),
  21. routes: {
  22. "add_event": (_) => AddEventPage(),
  23. },
  24. );
  25. }
  26. }
  27. class MyHomePage extends StatefulWidget {
  28. @override
  29. _MyHomePageState createState() => _MyHomePageState();
  30. }
  31. class _MyHomePageState extends State<MyHomePage> {
  32. CalendarController _controller;
  33. Map<DateTime, List<dynamic>> _events;
  34. List<dynamic> _selectedEvents;
  35. @override
  36. void initState() {
  37. super.initState();
  38. _controller = CalendarController();
  39. _events = {};
  40. _selectedEvents = [];
  41. }
  42. Map<DateTime, List<dynamic>> _groupEvents(List<EventModel> events) {
  43. Map<DateTime,List<dynamic>> data = {};
  44. events.forEach((event) {
  45. DateTime date = DateTime(event.eventDate.year, event.eventDate.month,
  46. event.eventDate.day, 12);
  47. if(data[date] == null) data[date] = [];
  48. data[date].add(event);
  49. });
  50. return data;
  51. }
  52. @override
  53. Widget build(BuildContext context) {
  54. return Scaffold(
  55. appBar: AppBar(
  56. title: Text('Citas Disponibles'),
  57. ),
  58. body: StreamBuilder<List<EventModel>>(
  59. // stream: eventDBS.streamList(),
  60. builder: (context, snapshot) {
  61. if (snapshot.hasData) {
  62. List<EventModel> allEvents = snapshot.data;
  63. if (allEvents.isNotEmpty) {
  64. _events = _groupEvents(allEvents);
  65. }
  66. }
  67. return SingleChildScrollView(
  68. child: Column(
  69. crossAxisAlignment: CrossAxisAlignment.start,
  70. children: <Widget>[
  71. TableCalendar(
  72. events: _events,
  73. initialCalendarFormat: CalendarFormat.week,
  74. calendarStyle: CalendarStyle(
  75. canEventMarkersOverflow: true,
  76. todayColor: Colors.orange,
  77. selectedColor: Theme
  78. .of(context)
  79. .primaryColor,
  80. todayStyle: TextStyle(
  81. fontWeight: FontWeight.bold,
  82. fontSize: 18.0,
  83. color: Colors.white)),
  84. headerStyle: HeaderStyle(
  85. centerHeaderTitle: true,
  86. formatButtonDecoration: BoxDecoration(
  87. color: Colors.orange,
  88. borderRadius: BorderRadius.circular(20.0),
  89. ),
  90. formatButtonTextStyle: TextStyle(color: Colors.white),
  91. formatButtonShowsNext: false,
  92. ),
  93. startingDayOfWeek: StartingDayOfWeek.monday,
  94. builders: CalendarBuilders(
  95. selectedDayBuilder: (context, date, events) =>
  96. Container(
  97. margin: const EdgeInsets.all(4.0),
  98. alignment: Alignment.center,
  99. decoration: BoxDecoration(
  100. color: Theme
  101. .of(context)
  102. .primaryColor,
  103. borderRadius: BorderRadius.circular(10.0)),
  104. child: Text(
  105. date.day.toString(),
  106. style: TextStyle(color: Colors.white),
  107. )),
  108. todayDayBuilder: (context, date, events) =>
  109. Container(
  110. margin: const EdgeInsets.all(4.0),
  111. alignment: Alignment.center,
  112. decoration: BoxDecoration(
  113. color: Colors.orange,
  114. borderRadius: BorderRadius.circular(10.0)),
  115. child: Text(
  116. date.day.toString(),
  117. style: TextStyle(color: Colors.white),
  118. )),
  119. ),
  120. calendarController: _controller,
  121. ),
  122. ..._selectedEvents.map((event) =>
  123. ListTile(
  124. title: Text(event.title),
  125. onTap: () {
  126. Navigator.push(
  127. context,
  128. MaterialPageRoute(
  129. builder: (_) =>
  130. EventDetailsPage(
  131. event: event,
  132. )));
  133. },
  134. )),
  135. ],
  136. ),
  137. );
  138. }
  139. ),
  140. floatingActionButton: FloatingActionButton(
  141. child: Icon(Icons.add),
  142. onPressed: () => Navigator.pushNamed(context, 'add_event'),
  143. ),
  144. );
  145. }
  146. }