|
@@ -4,7 +4,7 @@ import 'package:flutter/material.dart';
|
4
|
4
|
import 'package:http/http.dart' as http;
|
5
|
5
|
import 'package:shared_preferences/shared_preferences.dart';
|
6
|
6
|
import 'custom_info_card.dart';
|
7
|
|
-import 'favorites_page.dart'; // Nuevo import
|
|
7
|
+import 'favorites_page.dart';
|
8
|
8
|
|
9
|
9
|
void main() => runApp(const MyApp());
|
10
|
10
|
|
|
@@ -15,20 +15,26 @@ class MyApp extends StatelessWidget {
|
15
|
15
|
static const Color secondaryColor = Colors.orange;
|
16
|
16
|
|
17
|
17
|
@override
|
18
|
|
- Widget build(BuildContext context) {
|
19
|
|
- return MaterialApp(
|
20
|
|
- title: 'Custom Info Cards',
|
21
|
|
- theme: ThemeData(
|
22
|
|
- primaryColor: primaryColor,
|
23
|
|
- appBarTheme: const AppBarTheme(
|
24
|
|
- backgroundColor: primaryColor,
|
25
|
|
- elevation: 0,
|
26
|
|
- ),
|
|
18
|
+ Widget build(BuildContext context) => MaterialApp(
|
|
19
|
+ title: 'UPR App',
|
|
20
|
+ theme: ThemeData(
|
|
21
|
+ primaryColor: primaryColor,
|
|
22
|
+ appBarTheme: const AppBarTheme(
|
|
23
|
+ backgroundColor: primaryColor,
|
|
24
|
+ elevation: 0,
|
27
|
25
|
),
|
28
|
|
- home: const HomePage(),
|
29
|
|
- debugShowCheckedModeBanner: false,
|
30
|
|
- );
|
31
|
|
- }
|
|
26
|
+ bottomNavigationBarTheme: const BottomNavigationBarThemeData(
|
|
27
|
+ selectedItemColor: Colors.white,
|
|
28
|
+ unselectedItemColor: Colors.white70,
|
|
29
|
+ backgroundColor: primaryColor,
|
|
30
|
+ type: BottomNavigationBarType.fixed,
|
|
31
|
+ showSelectedLabels: true,
|
|
32
|
+ showUnselectedLabels: true,
|
|
33
|
+ ),
|
|
34
|
+ ),
|
|
35
|
+ home: const HomePage(),
|
|
36
|
+ debugShowCheckedModeBanner: false,
|
|
37
|
+ );
|
32
|
38
|
}
|
33
|
39
|
|
34
|
40
|
class HomePage extends StatefulWidget {
|
|
@@ -39,165 +45,179 @@ class HomePage extends StatefulWidget {
|
39
|
45
|
}
|
40
|
46
|
|
41
|
47
|
class _HomePageState extends State<HomePage> {
|
42
|
|
- List<Map<String, dynamic>> infoCards = [];
|
|
48
|
+ List<Map<String, dynamic>> allCards = [];
|
|
49
|
+ List<Map<String, dynamic>> filteredCards = [];
|
43
|
50
|
List<Map<String, dynamic>> favoriteCards = [];
|
44
|
51
|
int _currentIndex = 0;
|
45
|
|
- static const String googleSheetUrl = 'https://script.google.com/macros/s/AKfycbw1htmk7rlwLMOkOrpZ9ED-7ErWgMlYNtLKxoQ9QO-FopqTAhJuQkR7Gs1LxTJakbMT/exec';
|
|
52
|
+ bool _isLoading = false;
|
|
53
|
+ static const String _sheetUrl = 'https://script.google.com/macros/s/AKfycbw1htmk7rlwLMOkOrpZ9ED-7ErWgMlYNtLKxoQ9QO-FopqTAhJuQkR7Gs1LxTJakbMT/exec';
|
46
|
54
|
|
47
|
55
|
@override
|
48
|
56
|
void initState() {
|
49
|
57
|
super.initState();
|
50
|
|
- _loadInfoCards();
|
|
58
|
+ _loadData();
|
51
|
59
|
}
|
52
|
60
|
|
53
|
|
- Future<void> _loadInfoCards() async {
|
|
61
|
+ Future<void> _loadData() async {
|
54
|
62
|
final prefs = await SharedPreferences.getInstance();
|
55
|
|
- String? savedData = prefs.getString('infoCardsData');
|
56
|
|
-
|
57
|
63
|
setState(() {
|
58
|
|
- infoCards = savedData != null
|
59
|
|
- ? List<Map<String, dynamic>>.from(json.decode(savedData))
|
60
|
|
- : _defaultInfoCards();
|
|
64
|
+ allCards = prefs.getString('infoCardsData') != null
|
|
65
|
+ ? List<Map<String, dynamic>>.from(json.decode(prefs.getString('infoCardsData')!))
|
|
66
|
+ : [_defaultCard()];
|
|
67
|
+ filteredCards = allCards;
|
61
|
68
|
});
|
62
|
|
-
|
63
|
|
- await _fetchUpdatedData();
|
|
69
|
+ await _fetchData();
|
64
|
70
|
}
|
65
|
71
|
|
66
|
|
- Future<void> _loadFavorites() async {
|
67
|
|
- final prefs = await SharedPreferences.getInstance();
|
68
|
|
- final allKeys = prefs.getKeys().where((key) => key.startsWith('fav_')).toList();
|
69
|
|
-
|
70
|
|
- setState(() {
|
71
|
|
- favoriteCards = infoCards.where((card) {
|
72
|
|
- return allKeys.contains('fav_${card['title']}') &&
|
73
|
|
- (prefs.getBool('fav_${card['title']}') ?? false);
|
74
|
|
- }).toList();
|
75
|
|
- });
|
76
|
|
- }
|
77
|
|
-
|
78
|
|
- List<Map<String, dynamic>> _defaultInfoCards() {
|
79
|
|
- return [
|
80
|
|
- {
|
81
|
|
- 'image': 'assets/icons/icono1.jpg',
|
82
|
|
- 'title': 'Tutorias de programación',
|
83
|
|
- 'subtitle': '8:00am a 11:30am En biblioteca Lázaro',
|
84
|
|
- 'texto': 'Detalles completos sobre tutorías...',
|
85
|
|
- },
|
86
|
|
- ];
|
87
|
|
- }
|
88
|
|
-
|
89
|
|
- Future<void> _fetchUpdatedData() async {
|
|
72
|
+ Future<void> _fetchData() async {
|
90
|
73
|
try {
|
91
|
|
- final response = await http.get(Uri.parse(googleSheetUrl));
|
|
74
|
+ setState(() => _isLoading = true);
|
|
75
|
+ final response = await http.get(Uri.parse(_sheetUrl)).timeout(const Duration(seconds: 10));
|
92
|
76
|
if (response.statusCode == 200) {
|
93
|
|
- final List<Map<String, dynamic>> newData = List<Map<String, dynamic>>.from(json.decode(response.body));
|
94
|
|
-
|
95
|
|
- if (json.encode(newData) != json.encode(infoCards)) {
|
96
|
|
- setState(() => infoCards = newData);
|
97
|
|
- final prefs = await SharedPreferences.getInstance();
|
98
|
|
- await prefs.setString('infoCardsData', json.encode(newData));
|
|
77
|
+ final newData = List<Map<String, dynamic>>.from(json.decode(response.body));
|
|
78
|
+ if (json.encode(newData) != json.encode(allCards)) {
|
|
79
|
+ setState(() {
|
|
80
|
+ allCards = newData;
|
|
81
|
+ filteredCards = _filterCardsByCategory(_currentIndex);
|
|
82
|
+ });
|
|
83
|
+ (await SharedPreferences.getInstance()).setString('infoCardsData', response.body);
|
99
|
84
|
}
|
100
|
85
|
}
|
101
|
86
|
} catch (e) {
|
102
|
|
- if (kDebugMode) print('Error fetching data: $e');
|
|
87
|
+ if (kDebugMode) print('Fetch error: $e');
|
|
88
|
+ } finally {
|
|
89
|
+ if (mounted) setState(() => _isLoading = false);
|
103
|
90
|
}
|
104
|
91
|
}
|
105
|
92
|
|
|
93
|
+ Future<void> _loadFavorites() async {
|
|
94
|
+ final prefs = await SharedPreferences.getInstance();
|
|
95
|
+ final favKeys = prefs.getKeys().where((k) => k.startsWith('fav_') && (prefs.getBool(k) ?? false));
|
|
96
|
+ setState(() => favoriteCards = allCards.where((c) => favKeys.contains('fav_${c['title']}')).toList());
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ List<Map<String, dynamic>> _filterCardsByCategory(int index) {
|
|
100
|
+ switch (index) {
|
|
101
|
+ case 0: return allCards;
|
|
102
|
+ case 1: return allCards.where((card) => card['categoria'] == 'Actividades').toList();
|
|
103
|
+ case 2: return allCards.where((card) => card['categoria'] == 'Noticias UPR').toList();
|
|
104
|
+ case 3: return allCards.where((card) => card['categoria'] == 'Fechas Importantes').toList();
|
|
105
|
+ case 4: return favoriteCards;
|
|
106
|
+ default: return allCards;
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ Map<String, dynamic> _defaultCard() => {
|
|
111
|
+ 'image': 'assets/icons/icono1.jpg',
|
|
112
|
+ 'title': 'Tutorias de programación',
|
|
113
|
+ 'subtitle': '8:00am a 11:30am En biblioteca Lázaro',
|
|
114
|
+ 'texto': 'Detalles completos sobre tutorías...',
|
|
115
|
+ 'categoria': 'Actividades',
|
|
116
|
+ };
|
|
117
|
+
|
106
|
118
|
@override
|
107
|
|
- Widget build(BuildContext context) {
|
108
|
|
- return Scaffold(
|
109
|
|
- appBar: _currentIndex == 0
|
110
|
|
- ? AppBar(
|
111
|
|
- flexibleSpace: Padding(
|
112
|
|
- padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top - 20),
|
113
|
|
- child: Image.asset(
|
114
|
|
- 'assets/header_image.png',
|
115
|
|
- fit: BoxFit.cover,
|
116
|
|
- ),
|
117
|
|
- ),
|
118
|
|
- toolbarHeight: MediaQuery.of(context).size.height * 0.19,
|
119
|
|
- )
|
120
|
|
- : null,
|
121
|
|
- body: Container(
|
122
|
|
- decoration: const BoxDecoration(
|
123
|
|
- gradient: LinearGradient(
|
124
|
|
- colors: [MyApp.primaryColor, MyApp.secondaryColor],
|
125
|
|
- begin: Alignment.topCenter,
|
126
|
|
- end: Alignment.bottomCenter,
|
127
|
|
- ),
|
|
119
|
+ Widget build(BuildContext context) => Scaffold(
|
|
120
|
+ appBar: _currentIndex != 4 ? _buildAppBar(context) : null,
|
|
121
|
+ body: Container(
|
|
122
|
+ decoration: const BoxDecoration(
|
|
123
|
+ gradient: LinearGradient(
|
|
124
|
+ colors: [MyApp.primaryColor, MyApp.secondaryColor],
|
|
125
|
+ begin: Alignment.topCenter,
|
|
126
|
+ end: Alignment.bottomCenter,
|
128
|
127
|
),
|
129
|
|
- child: _currentIndex == 0
|
130
|
|
- ? RefreshIndicator(
|
131
|
|
- onRefresh: _fetchUpdatedData,
|
132
|
|
- child: _buildMainContent(),
|
133
|
|
- )
|
134
|
|
- : FavoritesPage(favoriteCards: favoriteCards),
|
135
|
128
|
),
|
136
|
|
- bottomNavigationBar: BottomNavigationBar(
|
137
|
|
- currentIndex: _currentIndex,
|
138
|
|
- selectedItemColor: Colors.white,
|
139
|
|
- unselectedItemColor: Colors.white70,
|
140
|
|
- backgroundColor: MyApp.primaryColor,
|
141
|
|
- items: const [
|
142
|
|
- BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Inicio'),
|
143
|
|
- BottomNavigationBarItem(icon: Icon(Icons.favorite), label: 'Favoritos'),
|
144
|
|
- ],
|
145
|
|
- onTap: (index) {
|
146
|
|
- if (index == 1) _loadFavorites();
|
147
|
|
- setState(() => _currentIndex = index);
|
148
|
|
- },
|
149
|
|
- ),
|
150
|
|
- );
|
151
|
|
- }
|
|
129
|
+ child: _currentIndex == 4
|
|
130
|
+ ? FavoritesPage(favoriteCards: favoriteCards, onRefresh: _loadFavorites)
|
|
131
|
+ : _buildHome(),
|
|
132
|
+ ),
|
|
133
|
+ bottomNavigationBar: _buildBottomNavBar(),
|
|
134
|
+ );
|
152
|
135
|
|
153
|
|
- Widget _buildMainContent() {
|
154
|
|
- return SafeArea(
|
155
|
|
- child: Column(
|
156
|
|
- children: [
|
157
|
|
- Expanded(
|
158
|
|
- child: ListView.builder(
|
159
|
|
- itemCount: infoCards.isEmpty ? 1 : infoCards.length,
|
160
|
|
- itemBuilder: (context, index) {
|
161
|
|
- if (infoCards.isEmpty) {
|
162
|
|
- return _buildErrorWidget();
|
163
|
|
- }
|
164
|
|
- final card = infoCards[index];
|
165
|
|
- return CustomInfoCard(
|
166
|
|
- imagePath: "assets/icons/${card['image']}",
|
167
|
|
- title: card['title'],
|
168
|
|
- subtitle: card['subtitle'],
|
169
|
|
- texto: card['texto'],
|
170
|
|
- );
|
171
|
|
- },
|
172
|
|
- ),
|
173
|
|
- ),
|
174
|
|
- ],
|
|
136
|
+ AppBar _buildAppBar(BuildContext context) => AppBar(
|
|
137
|
+ flexibleSpace: Padding(
|
|
138
|
+ padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top - 20),
|
|
139
|
+ child: Image.asset('assets/header_image.png', fit: BoxFit.cover),
|
|
140
|
+ ),
|
|
141
|
+ toolbarHeight: MediaQuery.of(context).size.height * 0.19,
|
|
142
|
+ );
|
|
143
|
+
|
|
144
|
+ Widget _buildBottomNavBar() => BottomNavigationBar(
|
|
145
|
+ currentIndex: _currentIndex,
|
|
146
|
+ onTap: (index) {
|
|
147
|
+ setState(() {
|
|
148
|
+ _currentIndex = index;
|
|
149
|
+ filteredCards = _filterCardsByCategory(index);
|
|
150
|
+ if (index == 4) _loadFavorites();
|
|
151
|
+ });
|
|
152
|
+ },
|
|
153
|
+ items: const [
|
|
154
|
+ BottomNavigationBarItem(
|
|
155
|
+ icon: Icon(Icons.home_outlined),
|
|
156
|
+ activeIcon: Icon(Icons.home),
|
|
157
|
+ label: 'Inicio',
|
175
|
158
|
),
|
176
|
|
- );
|
177
|
|
- }
|
|
159
|
+ BottomNavigationBarItem(
|
|
160
|
+ icon: Icon(Icons.event_outlined),
|
|
161
|
+ activeIcon: Icon(Icons.event),
|
|
162
|
+ label: 'Actividades',
|
|
163
|
+ ),
|
|
164
|
+ BottomNavigationBarItem(
|
|
165
|
+ icon: Icon(Icons.article_outlined),
|
|
166
|
+ activeIcon: Icon(Icons.article),
|
|
167
|
+ label: 'Noticias',
|
|
168
|
+ ),
|
|
169
|
+ BottomNavigationBarItem(
|
|
170
|
+ icon: Icon(Icons.calendar_today_outlined),
|
|
171
|
+ activeIcon: Icon(Icons.calendar_today),
|
|
172
|
+ label: 'Fechas',
|
|
173
|
+ ),
|
|
174
|
+ BottomNavigationBarItem(
|
|
175
|
+ icon: Icon(Icons.favorite_outline),
|
|
176
|
+ activeIcon: Icon(Icons.favorite),
|
|
177
|
+ label: 'Favoritos',
|
|
178
|
+ ),
|
|
179
|
+ ],
|
|
180
|
+ );
|
178
|
181
|
|
179
|
|
- Widget _buildErrorWidget() {
|
180
|
|
- return Card(
|
181
|
|
- margin: const EdgeInsets.all(16),
|
182
|
|
- color: Colors.white,
|
183
|
|
- child: Padding(
|
184
|
|
- padding: const EdgeInsets.all(16),
|
185
|
|
- child: Column(
|
186
|
|
- children: [
|
187
|
|
- const Icon(Icons.warning_amber_rounded, size: 48, color: Colors.orange),
|
188
|
|
- const SizedBox(height: 16),
|
189
|
|
- const Text('No se pudo cargar la información', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
|
190
|
|
- const SizedBox(height: 8),
|
191
|
|
- const Text('Desliza hacia abajo para intentar de nuevo', textAlign: TextAlign.center),
|
192
|
|
- const SizedBox(height: 16),
|
193
|
|
- ElevatedButton(
|
194
|
|
- onPressed: _fetchUpdatedData,
|
195
|
|
- style: ElevatedButton.styleFrom(backgroundColor: MyApp.primaryColor),
|
196
|
|
- child: const Text('Reintentar'),
|
197
|
|
- ),
|
198
|
|
- ],
|
|
182
|
+ Widget _buildHome() => SafeArea(
|
|
183
|
+ child: Column(children: [
|
|
184
|
+ Expanded(child: _isLoading ? _buildLoader() : RefreshIndicator(
|
|
185
|
+ onRefresh: _fetchData,
|
|
186
|
+ child: filteredCards.isEmpty ? _buildEmptyState() : ListView.builder(
|
|
187
|
+ itemCount: filteredCards.length,
|
|
188
|
+ itemBuilder: (ctx, i) => CustomInfoCard(
|
|
189
|
+ imagePath: "assets/icons/${filteredCards[i]['image']}",
|
|
190
|
+ title: filteredCards[i]['title'],
|
|
191
|
+ subtitle: filteredCards[i]['subtitle'],
|
|
192
|
+ texto: filteredCards[i]['texto'],
|
|
193
|
+ categoria: filteredCards[i]['categoria'],
|
|
194
|
+ ),
|
199
|
195
|
),
|
|
196
|
+ )),
|
|
197
|
+ ]),
|
|
198
|
+ );
|
|
199
|
+
|
|
200
|
+ Widget _buildLoader() => const Center(child: CircularProgressIndicator(color: Colors.white));
|
|
201
|
+
|
|
202
|
+ Widget _buildEmptyState() => Center(child: Column(
|
|
203
|
+ mainAxisSize: MainAxisSize.min,
|
|
204
|
+ children: [
|
|
205
|
+ const Icon(Icons.info_outline, size: 50, color: Colors.white),
|
|
206
|
+ const SizedBox(height: 16),
|
|
207
|
+ Text(
|
|
208
|
+ 'No hay ${_getCategoryName(_currentIndex)} disponibles',
|
|
209
|
+ style: const TextStyle(color: Colors.white, fontSize: 18),
|
200
|
210
|
),
|
201
|
|
- );
|
|
211
|
+ ],
|
|
212
|
+ ));
|
|
213
|
+
|
|
214
|
+ String _getCategoryName(int index) {
|
|
215
|
+ switch (index) {
|
|
216
|
+ case 0: return 'contenidos';
|
|
217
|
+ case 1: return 'actividades';
|
|
218
|
+ case 2: return 'noticias';
|
|
219
|
+ case 3: return 'fechas importantes';
|
|
220
|
+ default: return 'elementos';
|
|
221
|
+ }
|
202
|
222
|
}
|
203
|
223
|
}
|