aplicacion terminada, clase widget creada y separacion bloc
This commit is contained in:
31
lib/BloC/DataService.dart
Normal file
31
lib/BloC/DataService.dart
Normal file
@ -0,0 +1,31 @@
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:html/parser.dart' as htmlParser;
|
||||
|
||||
class DataService {
|
||||
Future<List<Map<String, dynamic>>> obtenerDatos() 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 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');
|
||||
|
||||
List<Map<String, dynamic>> embalsesData = [];
|
||||
|
||||
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();
|
||||
|
||||
var embalseMap = {'nombre': nombre, 'elementos': listaElementos};
|
||||
embalsesData.add(embalseMap);
|
||||
}
|
||||
}
|
||||
return embalsesData;
|
||||
} else {
|
||||
throw Exception('Error al obtener datos');
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -16,7 +16,7 @@ class MyBloc extends Cubit<MyState> {
|
||||
|
||||
}
|
||||
//mejorar código
|
||||
Future<List<String>> obtenerDatos(String url) async {
|
||||
Future<List<String>> obtenerDatos() 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);
|
||||
|
||||
@ -1,7 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:flutter_project/BloC/DataService.dart';
|
||||
import 'package:html/parser.dart' as htmlParser;
|
||||
import 'package:flutter_project/BloC/contenedores_event.dart';
|
||||
import 'package:flutter_project/bars/app_bar';
|
||||
import 'package:flutter_project/widgets/background_widget.dart';
|
||||
import 'package:flutter_project/bars/bottom_bar';
|
||||
|
||||
class EmbalsesPage extends StatefulWidget {
|
||||
@ -12,6 +15,7 @@ class EmbalsesPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _EmbalsesPageState extends State<EmbalsesPage> {
|
||||
final DataService dataService = DataService();
|
||||
List<Map<String, dynamic>> embalsesData = [];
|
||||
List<String> descripciones = ['Cota (m)', 'Volumen (Hm3)', 'Entrada (l/s)', 'Salida (l/s)'];
|
||||
|
||||
@ -22,82 +26,22 @@ List<Map<String, dynamic>> embalsesData = [];
|
||||
}
|
||||
|
||||
Future<void> obtenerDatos() 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);
|
||||
|
||||
// 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 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
|
||||
try {
|
||||
final data = await dataService.obtenerDatos();
|
||||
setState(() {
|
||||
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
|
||||
/* for (var i = 0; i < elementosSpan.length; i++) {
|
||||
final elemento = elementosSpan[i];
|
||||
final style = elemento.attributes['style'];
|
||||
if (style != null && style.contains('font-size: 12pt;')) {
|
||||
setState(() {
|
||||
// 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,
|
||||
embalsesData = data;
|
||||
});
|
||||
} catch (e) {
|
||||
print('Error: $e');
|
||||
}
|
||||
});
|
||||
}
|
||||
} */
|
||||
|
||||
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 {
|
||||
throw Exception('Error al cargar los datos de embalses');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: const CustomAppBar(),
|
||||
body: Stack(
|
||||
children: [
|
||||
// Fondo con la imagen
|
||||
Positioned.fill(
|
||||
child: Image(
|
||||
image: AssetImage('assets/logo-fondo-verde.png'),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
body: BackgroundWidget(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: ListView.builder(
|
||||
itemCount: embalsesData.length,
|
||||
@ -122,7 +66,7 @@ List<Map<String, dynamic>> embalsesData = [];
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text(
|
||||
nombreEmbalse!,
|
||||
style: TextStyle(
|
||||
style: const TextStyle(
|
||||
fontSize: 25.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
@ -142,8 +86,9 @@ List<Map<String, dynamic>> embalsesData = [];
|
||||
listaElementos.length,
|
||||
(index) => Text(
|
||||
'${descripciones[index]}: ${listaElementos[index]}',
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.normal, fontSize : 20.0
|
||||
style: const TextStyle(
|
||||
fontWeight: FontWeight.normal,
|
||||
fontSize: 20.0,
|
||||
),
|
||||
),
|
||||
),
|
||||
@ -159,7 +104,6 @@ List<Map<String, dynamic>> embalsesData = [];
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: const CustomBottomBar(),
|
||||
);
|
||||
|
||||
@ -5,8 +5,7 @@ import 'package:flutter_project/BloC/contenedores_event.dart';
|
||||
import 'package:flutter_project/pages/noticias';
|
||||
import 'package:flutter_project/pages/pregon';
|
||||
import 'package:flutter_project/pages/embalses';
|
||||
|
||||
|
||||
import 'package:flutter_project/widgets/background_widget.dart';
|
||||
|
||||
class MyHomePage extends StatelessWidget {
|
||||
const MyHomePage({Key? key, required this.title}) : super(key: key);
|
||||
@ -16,15 +15,7 @@ class MyHomePage extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: const CustomAppBar(),
|
||||
body: Container(
|
||||
|
||||
decoration: const BoxDecoration(
|
||||
image: DecorationImage(
|
||||
image: AssetImage('assets/logo-fondo-verde.png'),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
|
||||
),
|
||||
body: BackgroundWidget(
|
||||
child: Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
@ -41,14 +32,14 @@ class MyHomePage extends StatelessWidget {
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
decoration: BoxDecoration(
|
||||
color: Color.fromARGB(255, 78, 169, 6),
|
||||
color: const Color.fromARGB(255, 78, 169, 6),
|
||||
borderRadius: BorderRadius.circular(7.0),
|
||||
),
|
||||
child: const Text(
|
||||
'Noticias',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(
|
||||
color: Color.fromARGB(255, 255, 255, 255),
|
||||
color: Colors.white,
|
||||
fontSize: 32.0,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
@ -71,7 +62,7 @@ class MyHomePage extends StatelessWidget {
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
decoration: BoxDecoration(
|
||||
color: Color.fromARGB(255, 78, 169, 6),
|
||||
color: const Color.fromARGB(255, 78, 169, 6),
|
||||
borderRadius: BorderRadius.circular(7.0),
|
||||
),
|
||||
child: const Text(
|
||||
@ -101,7 +92,7 @@ class MyHomePage extends StatelessWidget {
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
decoration: BoxDecoration(
|
||||
color: Color.fromARGB(255, 78, 169, 6),
|
||||
color: const Color.fromARGB(255, 78, 169, 6),
|
||||
borderRadius: BorderRadius.circular(7.0),
|
||||
),
|
||||
child: const Text(
|
||||
@ -128,7 +119,7 @@ class MyHomePage extends StatelessWidget {
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
decoration: BoxDecoration(
|
||||
color: Color.fromARGB(255, 78, 169, 6),
|
||||
color: const Color.fromARGB(255, 78, 169, 6),
|
||||
borderRadius: BorderRadius.circular(7.0),
|
||||
),
|
||||
child: const Text(
|
||||
|
||||
@ -3,6 +3,8 @@ import 'package:http/http.dart' as http;
|
||||
import 'package:flutter_project/bars/app_bar';
|
||||
import 'package:flutter_project/bars/bottom_bar';
|
||||
import 'package:html/parser.dart' as htmlParser;
|
||||
import 'package:flutter_project/widgets/background_widget.dart';
|
||||
|
||||
|
||||
class NoticiasPage extends StatefulWidget {
|
||||
const NoticiasPage({Key? key}) : super(key: key);
|
||||
@ -84,20 +86,12 @@ class _NoticiasPageState extends State<NoticiasPage> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: const CustomAppBar(),
|
||||
body: Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: Image(
|
||||
image: AssetImage('assets/logo-fondo-verde.png'),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
ListView.builder(
|
||||
body: BackgroundWidget(
|
||||
child: ListView.builder(
|
||||
itemCount: noticiasPorTitulo.length,
|
||||
itemBuilder: (context, index) {
|
||||
String titulo = noticiasPorTitulo.keys.elementAt(index);
|
||||
@ -138,7 +132,6 @@ class _NoticiasPageState extends State<NoticiasPage> {
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: const CustomBottomBar(),
|
||||
);
|
||||
|
||||
@ -4,6 +4,9 @@ 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/bottom_bar';
|
||||
import 'package:flutter_project/widgets/background_widget.dart';
|
||||
|
||||
|
||||
|
||||
class Pregon extends StatefulWidget {
|
||||
const Pregon({Key? key}) : super(key: key);
|
||||
@ -59,17 +62,8 @@ Widget build(BuildContext context) {
|
||||
|
||||
return Scaffold(
|
||||
appBar: const CustomAppBar(),
|
||||
body: Stack(
|
||||
children: [
|
||||
// Fondo con la imagen
|
||||
Positioned.fill(
|
||||
child: Image(
|
||||
image: AssetImage('assets/logo-fondo-verde.png'),
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
// Contenedor para los párrafos
|
||||
Center(
|
||||
body: BackgroundWidget(
|
||||
child: Center(
|
||||
child: ListView.builder(
|
||||
itemCount: datos.length,
|
||||
itemBuilder: (context, index) {
|
||||
@ -106,10 +100,8 @@ Widget build(BuildContext context) {
|
||||
shrinkWrap: true, // Ajusta el tamaño de ListView al contenido
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
bottomNavigationBar: const CustomBottomBar(),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
22
lib/widgets/background_widget.dart
Normal file
22
lib/widgets/background_widget.dart
Normal file
@ -0,0 +1,22 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class BackgroundWidget extends StatelessWidget {
|
||||
final Widget child;
|
||||
|
||||
const BackgroundWidget({required this.child, Key? key}) : super(key: key);
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Stack(
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: Image.asset(
|
||||
'assets/logo-fondo-verde.png',
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
),
|
||||
child,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user