articles_page.dart 3.03 KB
Newer Older
1
import 'package:flutter/material.dart';
yenisleydi committed
2 3
import 'package:primer_practica/src/controllers/articles_controller.dart';
import 'package:primer_practica/src/models/articles_model.dart';
yenisleydi committed
4
import 'package:primer_practica/src/pages/formulario_articulos.dart';
5

yenisleydi committed
6 7 8 9
class ArticlePage extends StatefulWidget {
  final int categoryId;

  const ArticlePage({Key? key, required this.categoryId}) : super(key: key);
yenisleydi committed
10

yenisleydi committed
11 12 13 14 15 16 17 18 19 20 21 22 23
  @override
  _ArticlePageState createState() => _ArticlePageState();
}

class _ArticlePageState extends State<ArticlePage> {
  final ArticleController _articleController = ArticleController();
  late Future<Map<String, dynamic>> _articlesFuture;

  @override
  void initState() {
    super.initState();
    _articlesFuture = _articleController.getArticles(widget.categoryId);
  }
24 25 26 27 28

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
yenisleydi committed
29 30 31
        title: const Text('Artículos'),
        backgroundColor: Colors.indigoAccent,
        foregroundColor: Colors.white,
32
      ),
yenisleydi committed
33 34 35 36 37 38
      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) {
yenisleydi committed
39 40
                List<ArticlesModel> articles = (snapshot.data!['data'] as List).cast<ArticlesModel>();
                // Lista de artículos
yenisleydi committed
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
                return ListView.builder(
                  itemCount: articles.length,
                  itemBuilder: (context, index) {
                    final article = articles[index];
                    return Card(
                      child: ListTile(
                        title: Text(article.nombre),
                        subtitle: Text('id :${article.categoriaId}'),
                        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());
          }
        },
66
      ),
yenisleydi committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          final result = await Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => FormularioArticulos()),
          );
          if (result == true) {
            setState(() {
              _articlesFuture = _articleController.getArticles(widget.categoryId);
            });
          }
        },
        child: const Icon(Icons.add),
        tooltip: 'Agregar artículo',
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
83 84
    );
  }
yenisleydi committed
85
}