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 { Map> noticiasPorTitulo = {}; @override void initState() { super.initState(); obtenerDatos(); } Future 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'); 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(); String noticia = dataElements[i].text.trim(); if (!noticiasPorTitulo.containsKey(titulo)) { noticiasPorTitulo[titulo] = []; } noticiasPorTitulo[titulo]?.add(noticia); } setState(() {}); } else { throw Exception('fallo al cargar noticias'); } } //Función para desplegar noticias desde los contenedores /*Future obtenerDatosNoticias() async { var linkElements = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > a'); String? link = linkElements[i].attributes['href']; if (response.statusCode == 200) { for (int i = 0; i < tituloElements.length; i++) { String titulo = tituloElements[i].text.trim(); String noticia = dataElements[i].text.trim(); if (!noticiasPorTitulo.containsKey(titulo)) { noticiasPorTitulo[titulo] = []; } noticiasPorTitulo[titulo]?.add(noticia); } setState(() {}); } else { throw Exception('fallo al cargar noticias'); } }*/ @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: noticiasPorTitulo.length, itemBuilder: (context, index) { String titulo = noticiasPorTitulo.keys.elementAt(index); List? noticias = noticiasPorTitulo[titulo]; return ExpansionTile( title: Text( titulo, style: TextStyle(fontWeight: FontWeight.bold), ), children: noticias != null ? noticias.map((noticia) { return Padding( padding: const EdgeInsets.all(8.0), child: Text( noticia, style: TextStyle(fontSize: 16.0), ), ); }).toList() : [], ); }, ), ], ), bottomNavigationBar: const CustomBottomBar(), ); } }