articles_page.dart 2.72 KB
Newer Older
1
import 'package:flutter/material.dart';
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import 'package:practica1_flutter/src/controllers/articles_controller.dart';
import 'package:practica1_flutter/src/models/article_model.dart';

class ArticlePage extends StatefulWidget {

  final int categoryId;

  // creamos un constructor de ArticlePage que recibe categoryId como parámetro.
  const ArticlePage({Key? key, required this.categoryId}) : super(key: key);
  @override
  _ArticlePageState createState() => _ArticlePageState();
}

class _ArticlePageState extends State<ArticlePage> {
  final ArticleController _articleController = ArticleController();

  late Future<Map<String, dynamic>> _articlesFuture;

  @override
  void initState() {
    super.initState();
    // Inicializa _articlesFuture llamando al método getArticles del controlador de
    // artículos con el categoryId.
    _articlesFuture = _articleController.getArticles(widget.categoryId);
  }
27 28 29

  @override
  Widget build(BuildContext context) {
30 31
    return Scaffold(
      appBar: AppBar(
32 33 34
        title: const Text('Artículos'),
        backgroundColor: Colors.indigoAccent,
        foregroundColor: Colors.white,
35
      ),
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

      body: FutureBuilder<Map<String, dynamic>>(
        future: _articlesFuture,
        builder: (context, snapshot) {
          if (snapshot.hasData) {
            if (snapshot.data!['ok']) {
              if (snapshot.data!['data'] != null && (snapshot.data!['data'] as List).isNotEmpty) {
                List<ArticleModel> articles = snapshot.data!['data'] as List<ArticleModel>;

               //Lista de articulos
                return ListView.builder(
                  // Definimos el número de elementos en la lista.
                  itemCount: articles.length,
                  itemBuilder: (context, index) {
                    final article = articles[index];
                    return Card(
                      child: ListTile(
                        title: Text(article.nombre),
                        subtitle: Text('id :${article.categoriaId}'),
                        //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());
          }
        },
73 74 75
      ),
    );
  }
76
}