embalses visibles, lógica adecuada, necesidad de cambios en interfaz

This commit is contained in:
2024-05-13 11:05:49 +02:00
parent 66b4708d63
commit 4dfb710eba
2 changed files with 139 additions and 91 deletions

View File

@ -15,6 +15,26 @@ class MyBloc extends Cubit<MyState> {
MyBloc() : super(MyState()); MyBloc() : super(MyState());
} }
//mejorar código
Future<List<String>> obtenerDatos(String url) async {
final response = await http.get(Uri.parse('http://www.crcivan.com/escaparate/noticias.cgi?idnoticias=192683'));
if (response.statusCode == 200) {
final document = htmlParser.parse(response.body);
final elementosSpan = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > table > tbody > tr > td > span');
List<String> embalsesData = [];
for (var elemento in elementosSpan) {
final style = elemento.attributes['style'];
if (style != null && style.contains('color: #000000; font-size: 12pt;')) {
embalsesData.add(elemento.text);
}
}
return embalsesData;
} else {
throw Exception('Error al cargar los datos de embalses');
}
}
Future<void> launchAemetURL() async { Future<void> launchAemetURL() async {
const urlString = 'https://www.aemet.es/es/eltiempo/prediccion/municipios/caspe-id50074'; const urlString = 'https://www.aemet.es/es/eltiempo/prediccion/municipios/caspe-id50074';

View File

@ -1,7 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:html/parser.dart' as htmlParser; import 'package:html/parser.dart' as htmlParser;
import 'package:html/dom.dart' as htmlDom;
import 'package:flutter_project/bars/app_bar'; import 'package:flutter_project/bars/app_bar';
import 'package:flutter_project/bars/bottom_bar'; import 'package:flutter_project/bars/bottom_bar';
@ -13,8 +12,7 @@ class EmbalsesPage extends StatefulWidget {
} }
class _EmbalsesPageState extends State<EmbalsesPage> { class _EmbalsesPageState extends State<EmbalsesPage> {
List<String> embalsesData = []; List<Map<String, dynamic>> embalsesData = [];
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -28,108 +26,138 @@ class _EmbalsesPageState extends State<EmbalsesPage> {
// Buscar los elementos <span> dentro de la estructura específica // Buscar los elementos <span> dentro de la estructura específica
final elementosSpan = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > table > tbody > tr > td > span'); final elementosSpan = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > table > tbody > tr > td > span');
final nombreEmbalses = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > table > tbody > tr > td > p > span');
final filaTabla = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > table > tbody > tr');
// Limpiar la lista de datos antes de actualizarla // Limpiar la lista de datos antes de actualizarla
setState(() { setState(() {
embalsesData.clear(); embalsesData.clear();
}); });
/* for (var i = 0; i < filaTabla.length; i++){
var nombre[i] = filaTabla.child('td > p > span');
List elemento[i] = filaTabla.child('td > span');
embalseData.add(nombre, elemento);
}*/
// Agregar textos de los elementos <span> a la lista // Agregar textos de los elementos <span> a la lista
for (var elemento in elementosSpan) { /* for (var i = 0; i < elementosSpan.length; i++) {
final elemento = elementosSpan[i];
final style = elemento.attributes['style']; final style = elemento.attributes['style'];
if (style != null && style.contains('color: #000000; font-size: 12pt;')) { if (style != null && style.contains('font-size: 12pt;')) {
setState(() { setState(() {
embalsesData.add(elemento.text); // Verificar si el índice es válido antes de agregar los datos a embalsesData
if (i < nombreEmbalses.length) {
embalsesData.add({
'nombre': nombreEmbalses[i].text,
'otroTexto': elemento.text,
});
}
}); });
} }
} } */
for (var fila in filaTabla) {
var nombreElemento = fila.querySelector('td > p > span');
var elementos = fila.querySelectorAll('td > span');
if (nombreElemento != null && elementos.isNotEmpty) {
var nombre = nombreElemento.text;
var listaElementos = elementos.map<String>((elemento) => elemento.text).toList();
// Crear un mapa con el nombre del embalse y sus elementos
var embalseMap = {'nombre': nombre, 'elementos': listaElementos};
// Agregar el mapa a la lista de embalsesData
setState(() {
embalsesData.add(embalseMap);
});
}
}
} else { } else {
throw Exception('Error al cargar los datos de embalses'); throw Exception('Error al cargar los datos de embalses');
} }
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: const CustomAppBar(), appBar: const CustomAppBar(),
body: Stack( body: Stack(
children: [ children: [
// Fondo con la imagen // Fondo con la imagen
Positioned.fill( Positioned.fill(
child: Image( child: Image(
image: AssetImage('assets/logo-fondo.png'), image: AssetImage('assets/logo-fondo.png'),
fit: BoxFit.cover, fit: BoxFit.cover,
),
), ),
), Padding(
Padding( padding: const EdgeInsets.all(8.0),
padding: const EdgeInsets.all(8.0), child: ListView.builder(
child: ListView.builder( itemCount: embalsesData.length,
itemCount: embalsesData.length, itemBuilder: (context, index) {
itemBuilder: (context, index) { final embalseData = embalsesData[index];
final embalse = embalsesData[index]; final nombreEmbalse = embalseData['nombre'];
return Padding( final listaElementos = embalseData['elementos'];
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
),
child: Column(
children: [
Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
embalse,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
),
],
),
Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Text(
'Valor 1',
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Text(
'Valor 2',
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Text(
'Valor 3',
),
),
),
],
),
],
),
),
);
},
),
),
],
),
bottomNavigationBar: const CustomBottomBar(),
);
}
return Padding(
padding: const EdgeInsets.only(bottom: 8.0),
child: Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10.0),
),
child: Column(
children: [
Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
nombreEmbalse!,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
),
),
],
),
Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: listaElementos.map<Widget>((elemento) {
return Text(
elemento,
style: TextStyle(
fontWeight: FontWeight.normal,
),
);
}).toList(),
),
),
),
],
),
],
),
),
);
},
),),
],
),
bottomNavigationBar: const CustomBottomBar(),
);
}
} }