noticias creada, embalses necesita tabla y pregones separados por dividers

This commit is contained in:
2024-05-09 17:02:25 +02:00
parent 7c2577259e
commit 66b4708d63
11 changed files with 348 additions and 71 deletions

135
lib/pages/embalses Normal file
View File

@ -0,0 +1,135 @@
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
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';
class EmbalsesPage extends StatefulWidget {
const EmbalsesPage({Key? key}) : super(key: key);
@override
_EmbalsesPageState createState() => _EmbalsesPageState();
}
class _EmbalsesPageState extends State<EmbalsesPage> {
List<String> embalsesData = [];
@override
void initState() {
super.initState();
obtenerDatos();
}
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');
// Limpiar la lista de datos antes de actualizarla
setState(() {
embalsesData.clear();
});
// Agregar textos de los elementos <span> a la lista
for (var elemento in elementosSpan) {
final style = elemento.attributes['style'];
if (style != null && style.contains('color: #000000; font-size: 12pt;')) {
setState(() {
embalsesData.add(elemento.text);
});
}
}
} 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.png'),
fit: BoxFit.cover,
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ListView.builder(
itemCount: embalsesData.length,
itemBuilder: (context, index) {
final embalse = embalsesData[index];
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(
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(),
);
}
}

154
lib/pages/my_home_page.dart Normal file
View File

@ -0,0 +1,154 @@
import 'package:flutter/material.dart';
import 'package:flutter_project/bars/app_bar';
import 'package:flutter_project/bars/bottom_bar';
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';
class MyHomePage extends StatelessWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const CustomAppBar(),
body: Container(
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/logo-fondo.png'),
fit: BoxFit.cover,
),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
InkWell(
onTap: () { Navigator.push(
context,
MaterialPageRoute(builder: (context) => const NoticiasPage()),
);},
child: SizedBox(
width: 300.0,
child: Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Color.fromARGB(255, 43, 102, 150),
borderRadius: BorderRadius.circular(7.0),
),
child: const Text(
'Noticias',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const Pregon()),
);
},
child: SizedBox(
width: 300.0,
child: Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Color.fromARGB(255, 43, 102, 150),
borderRadius: BorderRadius.circular(7.0),
),
child: const Text(
'Pregón',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
InkWell(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const EmbalsesPage()),
);
},
child: SizedBox(
width: 300.0,
child: Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Color.fromARGB(255, 43, 102, 150),
borderRadius: BorderRadius.circular(7.0),
),
child: const Text(
'Embalses',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
InkWell(
onTap: () {
launchAemetURL();
},
child: SizedBox(
width: 300.0,
child: Padding(
padding: const EdgeInsets.only(bottom: 20.0),
child: Container(
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Color.fromARGB(255, 43, 102, 150),
borderRadius: BorderRadius.circular(7.0),
),
child: const Text(
'Tiempo',
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
fontSize: 32.0,
fontWeight: FontWeight.bold,
),
),
),
),
),
),
],
),
),
),
bottomNavigationBar: const CustomBottomBar(),
);
}
}

62
lib/pages/noticias Normal file
View File

@ -0,0 +1,62 @@
import 'package:flutter/material.dart';
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;
class NoticiasPage extends StatefulWidget {
const NoticiasPage({Key? key}) : super(key: key);
@override
_NoticiasPageState createState() => _NoticiasPageState();
}
class _NoticiasPageState extends State<NoticiasPage> {
List<String> noticiasData = [];
@override
void initState() {
super.initState();
obtenerDatos();
}
Future<void> obtenerDatos() async {
final response = await http.get(Uri.parse('http://www.crcivan.com/escaparate/noticias.cgi?idpadre=92776&idempresa=31637'));
if (response.statusCode == 200) {
var document = htmlParser.parse(response.body);
var dataElements = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font');
setState(() {
noticiasData = dataElements.map((element) => element.text.trim()).toList();
});
} else {
throw Exception('Failed to load noticias data');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const CustomAppBar(),
body: Stack(
children: [
Positioned.fill(
child: Image(
image: AssetImage('assets/logo-fondo.png'),
fit: BoxFit.cover,
),
),
ListView.builder(
itemCount: noticiasData.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(noticiasData[index]),
);
},
),
],
),
bottomNavigationBar: const CustomBottomBar(),
);
}
}

106
lib/pages/pregon Normal file
View File

@ -0,0 +1,106 @@
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
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';
class Pregon extends StatefulWidget {
const Pregon({Key? key}) : super(key: key);
@override
_PregonState createState() => _PregonState();
}
class _PregonState extends State<Pregon> {
List<String> datos = [];
@override
void initState() {
super.initState();
obtenerDatos();
}
Future<void> obtenerDatos() async {
final response = await http.get(Uri.parse('http://www.crcivan.com/escaparate/noticias.cgi?idnoticias=192680'));
if (response.statusCode == 200) {
final document = htmlParser.parse(response.body);
// Buscar los elementos <p> dentro de la estructura específica
final elementosParrafos = document.querySelectorAll('body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > p');
// Limpiar la lista de datos antes de actualizarla
setState(() {
datos.clear();
});
// Agregar textos de los elementos <p> a la lista, evitando los que contienen solo espacios
for (var elemento in elementosParrafos) {
final texto = elemento.text.trim(); // Eliminar espacios al inicio y al final del texto
if (texto.isNotEmpty) {
setState(() {
datos.add(texto);
});
}
}
} else {
throw Exception('Error al cargar los datos');
}
}
@override
Widget build(BuildContext context) {
// Lista de nombres de los meses del año
final meses = [
'enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio',
'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'
];
return Scaffold(
appBar: const CustomAppBar(),
body: Stack(
children: [
// Fondo con la imagen
Positioned.fill(
child: Image(
image: AssetImage('assets/logo-fondo.png'),
fit: BoxFit.cover,
),
),
// Contenedor para los párrafos
Center(
child: ListView.builder(
itemCount: datos.length,
itemBuilder: (context, index) {
final hasText = datos[index].isNotEmpty;
final hasMes = meses.any((mes) => datos[index].toLowerCase().contains(mes));
return Column(
children: [
if (hasMes) Divider(color: Colors.blue, thickness: 3.0), // Divider antes del párrafo
hasText
? ListTile(
title: Center(
child: Text(
datos[index],
style: TextStyle(
color: hasMes ? Color.fromARGB(255, 0, 0, 0) : Color.fromARGB(255, 0, 0, 0),
),
),
),
)
: SizedBox.shrink(),
if (hasMes) Divider(color: Colors.blue, thickness: 1.0), // Divider después del párrafo
],
);
},
shrinkWrap: true, // Ajusta el tamaño de ListView al contenido
),
),
],
),
bottomNavigationBar: const CustomBottomBar(),
);
}
}