aplicacion terminada, fondo verde

This commit is contained in:
2024-06-03 15:45:35 +02:00
parent 20a4cb7ab2
commit be701d6e50
7 changed files with 70 additions and 105 deletions

View File

@ -5,7 +5,6 @@ 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);
@ -14,75 +13,53 @@ class NoticiasPage extends StatefulWidget {
}
class _NoticiasPageState extends State<NoticiasPage> {
Map<String, List<Future<String>>> noticiasPorTitulo = {};
List<Map<String, String>> noticias = [];
bool _isLoading = true;
static const String url = 'http://www.crcivan.com/escaparate/noticias.cgi?idpadre=92776&idempresa=31637';
@override
void initState() {
super.initState();
obtenerDatos();
obtenerNoticias();
}
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 tituloElements = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > b > a');
for (int i = 0; i < tituloElements.length; i++) {
String titulo = tituloElements[i].text.trim();
// Extrayendo el enlace href del elemento <a>
var linkElement = tituloElements[i];
String? link = linkElement.attributes['href'];
Future<void> obtenerNoticias() async {
try {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
var document = htmlParser.parse(response.body);
var noticiasElements = document.querySelectorAll(
'html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td'
);
if (link != null) {
noticiasPorTitulo[titulo] = [cargarContenidoUrl(link)];
}
}
var noticiasTemp = <Map<String, String>>[];
await Future.wait(noticiasPorTitulo.values.expand((element) => element).toList());
setState(() {});
} else {
throw Exception('Fallo al cargar noticias');
}
}
Future<String> cargarContenidoUrl(String url) async {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
var document = htmlParser.parse(response.body);
var paragraphAndLists = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > p, html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > ul');
String content = '';
String currentParagraphContent = '';
for (var element in paragraphAndLists) {
if (element.localName == 'p') {
if (currentParagraphContent.isNotEmpty) {
content += currentParagraphContent + '\n';
currentParagraphContent = '';
}
currentParagraphContent += element.text.trim() + '\n';
} else if (element.localName == 'ul') {
if (currentParagraphContent.isNotEmpty) {
content += currentParagraphContent + '\n';
currentParagraphContent = '';
}
var listItems = element.querySelectorAll('li');
for (var listItem in listItems) {
content += '- ${listItem.text.trim()}\n';
for (var element in noticiasElements) {
var tituloElement = element.querySelector('b > a');
if (tituloElement != null) {
var titulo = tituloElement.text.trim();
var contenido = '';
var fontElements = element.querySelectorAll('font');
for (var font in fontElements) {
contenido += '${font.text.trim()}\n';
}
noticiasTemp.add({'titulo': titulo, 'contenido': contenido.trim()});
}
}
}
if (currentParagraphContent.isNotEmpty) {
content += currentParagraphContent + '\n';
setState(() {
noticias = noticiasTemp;
_isLoading = false;
});
} else {
throw Exception('Fallo al cargar noticias');
}
return content;
} else {
throw Exception('Fallo al cargar contenido de URL: $url');
} catch (e) {
print('Error al obtener noticias: $e');
setState(() {
_isLoading = false;
});
}
}
@ -90,45 +67,34 @@ class _NoticiasPageState extends State<NoticiasPage> {
Widget build(BuildContext context) {
return Scaffold(
appBar: const CustomAppBar(),
body: BackgroundWidget(
body: _isLoading
? Center(child: CircularProgressIndicator())
: BackgroundWidget(
child: ListView.builder(
itemCount: noticiasPorTitulo.length,
itemCount: noticias.length,
itemBuilder: (context, index) {
String titulo = noticiasPorTitulo.keys.elementAt(index);
List<Future<String>>? noticias = noticiasPorTitulo[titulo];
var titulo = noticias[index]['titulo'];
var contenido = noticias[index]['contenido'];
return FutureBuilder<List<String>>(
future: Future.wait(noticias ?? []),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return CircularProgressIndicator();
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else if (!snapshot.hasData) {
return Text('No hay datos');
} else {
return Container(
color: Colors.white.withOpacity(0.5), // Fondo blanco con 50% de opacidad
margin: const EdgeInsets.all(8.0),
return Container(
color: Colors.white.withOpacity(0.5), // Fondo blanco con 50% de opacidad
margin: const EdgeInsets.all(8.0),
padding: const EdgeInsets.all(8.0),
child: ExpansionTile(
title: Text(
titulo!,
style: TextStyle(fontWeight: FontWeight.bold), // Establecer negrita para el título
),
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: ExpansionTile(
title: Text(
titulo,
style: TextStyle(fontWeight: FontWeight.bold),
),
children: snapshot.data!.map((noticia) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
noticia,
style: TextStyle(fontSize: 16.0),
),
);
}).toList(),
child: Text(
contenido!,
style: TextStyle(fontSize: 16.0), // Mantener el texto del contenido en estilo regular
),
);
}
},
),
],
),
);
},
),
@ -136,4 +102,5 @@ class _NoticiasPageState extends State<NoticiasPage> {
bottomNavigationBar: const CustomBottomBar(),
);
}
}
}