summaryrefslogtreecommitdiff
path: root/lib/screens/popular_screen.dart
blob: 07ca70edaf46c6a0e0d797c02ccd87d63bb2a0eb (plain)
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import 'package:flutter/material.dart';
import 'package:pmsna1/models/popular.dart';
import 'package:pmsna1/network/popular_api.dart';
import 'package:pmsna1/widgets/loading_modal_widget.dart';
import 'package:pmsna1/widgets/popular_item.dart';

class PopularScreen extends StatefulWidget {
  const PopularScreen({super.key});

  @override
  State<PopularScreen> createState() => _PopularScreenState();
}

class _PopularScreenState extends State<PopularScreen> {
  PopularApi? popularApi;

  @override
  void initState() {
    super.initState();
    popularApi = PopularApi();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Películas populares')),
      body: FutureBuilder(
        future: popularApi!.getAllPopular(),
        builder: (context, AsyncSnapshot<List<Popular>?> snapshot) {
          if (snapshot.hasData) {
            return GridView.builder(
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                childAspectRatio: .9,
                mainAxisSpacing: 10,
                crossAxisSpacing: 10,
              ),
              itemCount: snapshot.data != null ? snapshot.data!.length : 0,
              itemBuilder: (context, index) {
                return PopularItem(snapshot.data![index]);
              },
            );
          } else if (snapshot.hasError) {
            print(snapshot.error);
            return const Center(
              child: Text('Ocurrió un error'),
            );
          } else {
            return const LoadingModal();
          }
        },
      ),
    );
  }
}