|
@@ -3,7 +3,8 @@ import 'package:flutter/foundation.dart';
|
3
|
3
|
import 'package:flutter/material.dart';
|
4
|
4
|
import 'package:http/http.dart' as http;
|
5
|
5
|
import 'package:shared_preferences/shared_preferences.dart';
|
6
|
|
-import 'custom_info_card.dart'; // Ensure this path is correct
|
|
6
|
+import 'custom_info_card.dart';
|
|
7
|
+import 'favorites_page.dart';
|
7
|
8
|
|
8
|
9
|
void main() => runApp(const MyApp());
|
9
|
10
|
|
|
@@ -14,20 +15,26 @@ class MyApp extends StatelessWidget {
|
14
|
15
|
static const Color secondaryColor = Colors.orange;
|
15
|
16
|
|
16
|
17
|
@override
|
17
|
|
- Widget build(BuildContext context) {
|
18
|
|
- return MaterialApp(
|
19
|
|
- title: 'Custom Info Cards',
|
20
|
|
- theme: ThemeData(
|
21
|
|
- primaryColor: primaryColor,
|
22
|
|
- appBarTheme: const AppBarTheme(
|
23
|
|
- backgroundColor: primaryColor,
|
24
|
|
- elevation: 0,
|
25
|
|
- ),
|
|
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,
|
26
|
25
|
),
|
27
|
|
- home: const HomePage(),
|
28
|
|
- debugShowCheckedModeBanner: false,
|
29
|
|
- );
|
30
|
|
- }
|
|
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
|
+ );
|
31
|
38
|
}
|
32
|
39
|
|
33
|
40
|
class HomePage extends StatefulWidget {
|
|
@@ -38,190 +45,179 @@ class HomePage extends StatefulWidget {
|
38
|
45
|
}
|
39
|
46
|
|
40
|
47
|
class _HomePageState extends State<HomePage> {
|
41
|
|
- List<Map<String, dynamic>> infoCards = [];
|
42
|
|
- static const String googleSheetUrl = 'https://script.google.com/macros/s/AKfycbw1htmk7rlwLMOkOrpZ9ED-7ErWgMlYNtLKxoQ9QO-FopqTAhJuQkR7Gs1LxTJakbMT/exec';
|
|
48
|
+ List<Map<String, dynamic>> allCards = [];
|
|
49
|
+ List<Map<String, dynamic>> filteredCards = [];
|
|
50
|
+ List<Map<String, dynamic>> favoriteCards = [];
|
|
51
|
+ int _currentIndex = 0;
|
|
52
|
+ bool _isLoading = false;
|
|
53
|
+ static const String _sheetUrl = 'https://script.google.com/macros/s/AKfycbw1htmk7rlwLMOkOrpZ9ED-7ErWgMlYNtLKxoQ9QO-FopqTAhJuQkR7Gs1LxTJakbMT/exec';
|
43
|
54
|
|
44
|
55
|
@override
|
45
|
56
|
void initState() {
|
46
|
57
|
super.initState();
|
47
|
|
- _loadInfoCards();
|
|
58
|
+ _loadData();
|
48
|
59
|
}
|
49
|
60
|
|
50
|
|
- Future<void> _loadInfoCards() async {
|
|
61
|
+ Future<void> _loadData() async {
|
51
|
62
|
final prefs = await SharedPreferences.getInstance();
|
52
|
|
-
|
53
|
|
- // Load default data if no saved data
|
54
|
|
- String? savedData = prefs.getString('infoCardsData');
|
55
|
63
|
setState(() {
|
56
|
|
- if (savedData != null) {
|
57
|
|
- try {
|
58
|
|
- final decoded = json.decode(savedData);
|
59
|
|
- infoCards = List<Map<String, dynamic>>.from(decoded);
|
60
|
|
- } catch (e) {
|
61
|
|
- if (kDebugMode) {
|
62
|
|
- print('Error decoding saved data: $e');
|
63
|
|
- }
|
64
|
|
- infoCards = _defaultInfoCards();
|
65
|
|
- }
|
66
|
|
- } else {
|
67
|
|
- infoCards = _defaultInfoCards();
|
68
|
|
- }
|
|
64
|
+ allCards = prefs.getString('infoCardsData') != null
|
|
65
|
+ ? List<Map<String, dynamic>>.from(json.decode(prefs.getString('infoCardsData')!))
|
|
66
|
+ : [_defaultCard()];
|
|
67
|
+ filteredCards = allCards;
|
69
|
68
|
});
|
70
|
|
-
|
71
|
|
- await _fetchUpdatedData();
|
72
|
|
- }
|
73
|
|
-
|
74
|
|
- List<Map<String, dynamic>> _defaultInfoCards() {
|
75
|
|
- return [
|
76
|
|
- {
|
77
|
|
- 'image': 'assets/icons/icono1.jpg',
|
78
|
|
- 'title': 'Tutorias de programación',
|
79
|
|
- 'subtitle': '8:00am a 11:30am En biblioteca Lázaro',
|
80
|
|
- },
|
81
|
|
- ];
|
|
69
|
+ await _fetchData();
|
82
|
70
|
}
|
83
|
71
|
|
84
|
|
- Future<void> _fetchUpdatedData() async {
|
|
72
|
+ Future<void> _fetchData() async {
|
85
|
73
|
try {
|
86
|
|
- final response = await http.get(Uri.parse(googleSheetUrl));
|
87
|
|
- if (response.statusCode == 200 && response.body.isNotEmpty) {
|
88
|
|
- try {
|
89
|
|
- final dynamic decodedData = json.decode(response.body);
|
90
|
|
- List<Map<String, dynamic>> newData = [];
|
91
|
|
-
|
92
|
|
- if (decodedData is Map && decodedData.containsKey('feed')) {
|
93
|
|
- final entries = decodedData['feed']['entry'] as List?;
|
94
|
|
- if (entries != null) {
|
95
|
|
- for (var entry in entries) {
|
96
|
|
- final Map<String, dynamic> card = {
|
97
|
|
- 'image': entry['gsx\$image']?['\$t'] ?? '',
|
98
|
|
- 'title': entry['gsx\$title']?['\$t'] ?? '',
|
99
|
|
- 'subtitle': entry['gsx\$subtitle']?['\$t'] ?? '',
|
100
|
|
- 'texto': entry['gsx\$texto']?['\$t'] ?? '',
|
101
|
|
- };
|
102
|
|
- newData.add(card);
|
103
|
|
- }
|
104
|
|
- }
|
105
|
|
- } else if (decodedData is List) {
|
106
|
|
- newData = List<Map<String, dynamic>>.from(decodedData);
|
107
|
|
- } else {
|
108
|
|
- throw Exception('Unexpected JSON format');
|
109
|
|
- }
|
110
|
|
-
|
111
|
|
- if (json.encode(newData) != json.encode(infoCards)) {
|
112
|
|
- setState(() {
|
113
|
|
- infoCards = newData;
|
114
|
|
- });
|
115
|
|
- final prefs = await SharedPreferences.getInstance();
|
116
|
|
- prefs.setString('infoCardsData', json.encode(newData));
|
117
|
|
-
|
118
|
|
- if (kDebugMode) {
|
119
|
|
- print('Updated with new data from Google Sheet');
|
120
|
|
- }
|
121
|
|
- }
|
122
|
|
- } catch (e) {
|
123
|
|
- if (kDebugMode) {
|
124
|
|
- print('Error parsing response data: $e');
|
125
|
|
- }
|
126
|
|
- }
|
127
|
|
- } else {
|
128
|
|
- if (kDebugMode) {
|
129
|
|
- print('Failed to fetch updated data: ${response.statusCode}');
|
|
74
|
+ setState(() => _isLoading = true);
|
|
75
|
+ final response = await http.get(Uri.parse(_sheetUrl)).timeout(const Duration(seconds: 10));
|
|
76
|
+ if (response.statusCode == 200) {
|
|
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);
|
130
|
84
|
}
|
131
|
85
|
}
|
132
|
86
|
} catch (e) {
|
133
|
|
- if (kDebugMode) {
|
134
|
|
- print('Error fetching data: $e');
|
135
|
|
- }
|
|
87
|
+ if (kDebugMode) print('Fetch error: $e');
|
|
88
|
+ } finally {
|
|
89
|
+ if (mounted) setState(() => _isLoading = false);
|
|
90
|
+ }
|
|
91
|
+ }
|
|
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;
|
136
|
107
|
}
|
137
|
108
|
}
|
138
|
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
|
+
|
139
|
118
|
@override
|
140
|
|
- Widget build(BuildContext context) {
|
141
|
|
- return Scaffold(
|
142
|
|
- appBar: AppBar(
|
143
|
|
- flexibleSpace: Padding(
|
144
|
|
- padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
|
145
|
|
- child: Image.asset(
|
146
|
|
- 'assets/header_image.png',
|
147
|
|
- fit: BoxFit.cover,
|
148
|
|
- ),
|
|
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,
|
149
|
127
|
),
|
150
|
|
- toolbarHeight: MediaQuery.of(context).size.height * 0.19,
|
151
|
128
|
),
|
152
|
|
- body: Container(
|
153
|
|
- decoration: const BoxDecoration(
|
154
|
|
- gradient: LinearGradient(
|
155
|
|
- colors: [MyApp.primaryColor, MyApp.secondaryColor],
|
156
|
|
- begin: Alignment.topCenter,
|
157
|
|
- end: Alignment.bottomCenter,
|
158
|
|
- ),
|
159
|
|
- ),
|
160
|
|
- child: SafeArea(
|
161
|
|
- child: Column(
|
162
|
|
- children: [
|
163
|
|
- Expanded(
|
164
|
|
- child: RefreshIndicator(
|
165
|
|
- onRefresh: _fetchUpdatedData,
|
166
|
|
- color: MyApp.primaryColor,
|
167
|
|
- child: ListView.builder(
|
168
|
|
- padding: const EdgeInsets.all(0),
|
169
|
|
- itemCount: infoCards.isEmpty ? 1 : infoCards.length,
|
170
|
|
- itemBuilder: (context, index) {
|
171
|
|
- // Show error widget if no data is available
|
172
|
|
- if (infoCards.isEmpty) {
|
173
|
|
- return Card(
|
174
|
|
- margin: const EdgeInsets.all(16),
|
175
|
|
- color: Colors.white,
|
176
|
|
- child: Padding(
|
177
|
|
- padding: const EdgeInsets.all(16),
|
178
|
|
- child: Column(
|
179
|
|
- children: [
|
180
|
|
- const Icon(Icons.warning_amber_rounded,
|
181
|
|
- size: 48, color: Colors.orange),
|
182
|
|
- const SizedBox(height: 16),
|
183
|
|
- const Text(
|
184
|
|
- 'No se pudo cargar la información',
|
185
|
|
- style: TextStyle(
|
186
|
|
- fontSize: 18,
|
187
|
|
- fontWeight: FontWeight.bold,
|
188
|
|
- ),
|
189
|
|
- ),
|
190
|
|
- const SizedBox(height: 8),
|
191
|
|
- const Text(
|
192
|
|
- 'Desliza hacia abajo para intentar de nuevo',
|
193
|
|
- textAlign: TextAlign.center,
|
194
|
|
- ),
|
195
|
|
- const SizedBox(height: 16),
|
196
|
|
- ElevatedButton(
|
197
|
|
- onPressed: () => _fetchUpdatedData(),
|
198
|
|
- style: ElevatedButton.styleFrom(
|
199
|
|
- backgroundColor: MyApp.primaryColor,
|
200
|
|
- ),
|
201
|
|
- child: const Text('Reintentar'),
|
202
|
|
- ),
|
203
|
|
- ],
|
204
|
|
- ),
|
205
|
|
- ),
|
206
|
|
- );
|
207
|
|
- }
|
208
|
|
-
|
209
|
|
- // Show data card
|
210
|
|
- final card = infoCards[index];
|
211
|
|
- return CustomInfoCard(
|
212
|
|
- imagePath: "assets/icons/${card['image']}",
|
213
|
|
- title: card['title'],
|
214
|
|
- subtitle: card['subtitle'],
|
215
|
|
- texto: card['texto'],
|
216
|
|
- );
|
217
|
|
- },
|
218
|
|
- ),
|
219
|
|
- ),
|
220
|
|
- ),
|
221
|
|
- ],
|
|
129
|
+ child: _currentIndex == 4
|
|
130
|
+ ? FavoritesPage(favoriteCards: favoriteCards, onRefresh: _loadFavorites)
|
|
131
|
+ : _buildHome(),
|
|
132
|
+ ),
|
|
133
|
+ bottomNavigationBar: _buildBottomNavBar(),
|
|
134
|
+ );
|
|
135
|
+
|
|
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',
|
|
158
|
+ ),
|
|
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
|
+ );
|
|
181
|
+
|
|
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'],
|
222
|
194
|
),
|
223
|
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),
|
224
|
210
|
),
|
225
|
|
- );
|
|
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
|
+ }
|
226
|
222
|
}
|
227
|
|
-}
|
|
223
|
+}
|