Files
app_notificaciones_ador/lib/pages/noticias

118 lines
3.7 KiB
Plaintext

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:crcivan/bars/app_bar';
import 'package:crcivan/bars/bottom_bar';
import 'package:html/parser.dart' as htmlParser;
import 'package:crcivan/widgets/background_widget.dart';
import 'package:crcivan/pages/utils.dart';
class NoticiasPage extends StatefulWidget {
const NoticiasPage({Key? key}) : super(key: key);
@override
_NoticiasPageState createState() => _NoticiasPageState();
}
class _NoticiasPageState extends State<NoticiasPage> {
List<Map<String, String>> noticias = [];
bool _isLoading = true;
static const String url = 'https://crcivan.asociacionador.es/noticias.html';
@override
void initState() {
super.initState();
obtenerNoticias();
}
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'
);
var noticiasTemp = <Map<String, String>>[];
for (var element in noticiasElements) {
var tituloElement = element.querySelector('b > a');
if (tituloElement != null) {
var titulo = tituloElement.text.trim();
var href = tituloElement.attributes['href'];
var contenido = '';
var fontElements = element.querySelectorAll('font');
for (var font in fontElements) {
contenido += '${font.text.trim()}\n';
}
noticiasTemp.add({
'titulo': titulo,
'contenido': contenido.trim(),
'href': href ?? ''
});
}
}
setState(() {
noticias = noticiasTemp;
_isLoading = false;
});
} else {
throw Exception('Fallo al cargar noticias');
}
} catch (e) {
print('Error al obtener noticias: $e');
setState(() {
_isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const CustomAppBar(),
body: _isLoading
? Center(child: CircularProgressIndicator())
: BackgroundWidget(
child: ListView.builder(
itemCount: noticias.length,
itemBuilder: (context, index) {
var titulo = noticias[index]['titulo'];
var contenido = noticias[index]['contenido'];
var href = noticias[index]['href'];
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
fontSize: getResponsiveFontSize(context, 18.0),
),
),
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
contenido!,
style: TextStyle(
fontSize: getResponsiveFontSize(context, 16.0), // Mantener el texto del contenido en estilo regular
),
),
),
],
),
);
},
),
),
bottomNavigationBar: const CustomBottomBar(),
);
}
}