import 'package:flutter/material.dart'; import '../controllers/ArticuloController.dart'; import '../models/ArticuloModel.dart'; class ArticlePage extends StatefulWidget { final int categoryId; const ArticlePage({Key? key, required this.categoryId}) : super(key: key); @override _ArticlePageState createState() => _ArticlePageState(); } class _ArticlePageState extends State { final ArticuloController _articleController = ArticuloController(); late Future> _articlesFuture; @override void initState() { super.initState(); _articlesFuture = _articleController.getArticles(widget.categoryId); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Artículos'), backgroundColor: Colors.indigoAccent, foregroundColor: Colors.white, ), body: FutureBuilder>( future: _articlesFuture, builder: (context, snapshot) { if (snapshot.hasData) { if (snapshot.data!['ok']) { if (snapshot.data!['data'] != null && (snapshot.data!['data'] as List).isNotEmpty) { List articles = snapshot.data!['data'] as List; return ListView.builder( itemCount: articles.length, itemBuilder: (context, index) { final article = articles[index]; return Card( child: ListTile( title: Text(article.nombre), subtitle: Text('Clave: ${article.clave}'), trailing: Text('\$${article.precios.isNotEmpty ? article.precios.first.precio.toStringAsFixed(2) : 'N/A'}'), ), ); }, ); } else { return Center(child: Text('No se encontraron artículos.')); } } else { return Center(child: Text('Error: ${snapshot.data!['message']}')); } } else if (snapshot.hasError) { return Center(child: Text('Error: ${snapshot.error}')); } else { return const Center(child: CircularProgressIndicator()); } }, ), ); } }