|
@@ -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'; // Nuevo import
|
7
|
8
|
|
8
|
9
|
void main() => runApp(const MyApp());
|
9
|
10
|
|
|
@@ -39,6 +40,8 @@ class HomePage extends StatefulWidget {
|
39
|
40
|
|
40
|
41
|
class _HomePageState extends State<HomePage> {
|
41
|
42
|
List<Map<String, dynamic>> infoCards = [];
|
|
43
|
+ List<Map<String, dynamic>> favoriteCards = [];
|
|
44
|
+ int _currentIndex = 0;
|
42
|
45
|
static const String googleSheetUrl = 'https://script.google.com/macros/s/AKfycbw1htmk7rlwLMOkOrpZ9ED-7ErWgMlYNtLKxoQ9QO-FopqTAhJuQkR7Gs1LxTJakbMT/exec';
|
43
|
46
|
|
44
|
47
|
@override
|
|
@@ -49,106 +52,72 @@ class _HomePageState extends State<HomePage> {
|
49
|
52
|
|
50
|
53
|
Future<void> _loadInfoCards() async {
|
51
|
54
|
final prefs = await SharedPreferences.getInstance();
|
52
|
|
-
|
53
|
|
- // Load default data if no saved data
|
54
|
55
|
String? savedData = prefs.getString('infoCardsData');
|
|
56
|
+
|
55
|
57
|
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
|
|
- }
|
|
58
|
+ infoCards = savedData != null
|
|
59
|
+ ? List<Map<String, dynamic>>.from(json.decode(savedData))
|
|
60
|
+ : _defaultInfoCards();
|
69
|
61
|
});
|
70
|
|
-
|
|
62
|
+
|
71
|
63
|
await _fetchUpdatedData();
|
72
|
64
|
}
|
73
|
65
|
|
|
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
|
+
|
74
|
78
|
List<Map<String, dynamic>> _defaultInfoCards() {
|
75
|
79
|
return [
|
76
|
80
|
{
|
77
|
81
|
'image': 'assets/icons/icono1.jpg',
|
78
|
82
|
'title': 'Tutorias de programación',
|
79
|
83
|
'subtitle': '8:00am a 11:30am En biblioteca Lázaro',
|
|
84
|
+ 'texto': 'Detalles completos sobre tutorías...',
|
80
|
85
|
},
|
81
|
86
|
];
|
82
|
87
|
}
|
83
|
88
|
|
84
|
|
- Future<void> _fetchUpdatedData() async {
|
|
89
|
+ Future<void> _fetchUpdatedData() async {
|
85
|
90
|
try {
|
86
|
91
|
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}');
|
|
92
|
+ 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));
|
130
|
99
|
}
|
131
|
100
|
}
|
132
|
101
|
} catch (e) {
|
133
|
|
- if (kDebugMode) {
|
134
|
|
- print('Error fetching data: $e');
|
135
|
|
- }
|
|
102
|
+ if (kDebugMode) print('Error fetching data: $e');
|
136
|
103
|
}
|
137
|
104
|
}
|
138
|
105
|
|
139
|
106
|
@override
|
140
|
107
|
Widget build(BuildContext context) {
|
141
|
108
|
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
|
|
- ),
|
149
|
|
- ),
|
150
|
|
- toolbarHeight: MediaQuery.of(context).size.height * 0.19,
|
151
|
|
- ),
|
|
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,
|
152
|
121
|
body: Container(
|
153
|
122
|
decoration: const BoxDecoration(
|
154
|
123
|
gradient: LinearGradient(
|
|
@@ -157,71 +126,78 @@ class _HomePageState extends State<HomePage> {
|
157
|
126
|
end: Alignment.bottomCenter,
|
158
|
127
|
),
|
159
|
128
|
),
|
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 == 0
|
|
130
|
+ ? RefreshIndicator(
|
|
131
|
+ onRefresh: _fetchUpdatedData,
|
|
132
|
+ child: _buildMainContent(),
|
|
133
|
+ )
|
|
134
|
+ : FavoritesPage(favoriteCards: favoriteCards),
|
|
135
|
+ ),
|
|
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
|
+ }
|
|
152
|
+
|
|
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
|
+ ),
|
222
|
173
|
),
|
|
174
|
+ ],
|
|
175
|
+ ),
|
|
176
|
+ );
|
|
177
|
+ }
|
|
178
|
+
|
|
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
|
+ ],
|
223
|
199
|
),
|
224
|
200
|
),
|
225
|
201
|
);
|
226
|
202
|
}
|
227
|
|
-}
|
|
203
|
+}
|