139 lines
5.0 KiB
Plaintext
139 lines
5.0 KiB
Plaintext
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;
|
|
import 'package:flutter_project/widgets/background_widget.dart';
|
|
|
|
|
|
class NoticiasPage extends StatefulWidget {
|
|
const NoticiasPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
_NoticiasPageState createState() => _NoticiasPageState();
|
|
}
|
|
|
|
class _NoticiasPageState extends State<NoticiasPage> {
|
|
Map<String, List<Future<String>>> noticiasPorTitulo = {};
|
|
|
|
@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 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'];
|
|
|
|
if (link != null) {
|
|
noticiasPorTitulo[titulo] = [cargarContenidoUrl(link)];
|
|
}
|
|
}
|
|
|
|
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';
|
|
}
|
|
}
|
|
}
|
|
|
|
if (currentParagraphContent.isNotEmpty) {
|
|
content += currentParagraphContent + '\n';
|
|
}
|
|
|
|
return content;
|
|
} else {
|
|
throw Exception('Fallo al cargar contenido de URL: $url');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: const CustomAppBar(),
|
|
body: BackgroundWidget(
|
|
child: ListView.builder(
|
|
itemCount: noticiasPorTitulo.length,
|
|
itemBuilder: (context, index) {
|
|
String titulo = noticiasPorTitulo.keys.elementAt(index);
|
|
List<Future<String>>? noticias = noticiasPorTitulo[titulo];
|
|
|
|
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),
|
|
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(),
|
|
),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
bottomNavigationBar: const CustomBottomBar(),
|
|
);
|
|
}
|
|
} |