summaryrefslogtreecommitdiff
path: root/lib/screens/album_detail_screen.dart
blob: 478c0b61c78eaf5bc6da8fb5e1e5c3c38a57e74c (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import 'package:flutter/material.dart';
import 'package:pmsna1/models/album.dart';
import 'package:pmsna1/models/artist.dart';
import 'package:pmsna1/network/music_api.dart';
import 'package:pmsna1/widgets/album_placeholder.dart';
import 'package:pmsna1/widgets/responsive.dart';

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

  @override
  State<AlbumDetailScreen> createState() => _AlbumDetailScreenState();
}

class _AlbumDetailScreenState extends State<AlbumDetailScreen> {
  MusicApi? api;
  Album? album;

  @override
  void initState() {
    super.initState();
    api = MusicApi();
  }

  Widget getArtistCard(Artist artist, BuildContext context) => Card(
        margin: const EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 2.0),
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                artist.name,
                style: Theme.of(context)
                    .typography
                    .englishLike
                    .bodyLarge
                    ?.copyWith(color: Theme.of(context).colorScheme.onSurface),
              ),
              const SizedBox(height: 4.0),
              Text(artist.disambiguation),
            ],
          ),
        ),
      );

  // Widget getLeft() => Image.network(album!.coverUri.toString());
  Widget getLeft() => const SizedBox.shrink();

  Widget getRight() => Column(
        children: [
          ListTile(
            title: const Text('Fecha de lanzamiento'),
            subtitle: Text(album!.firstReleaseDate),
          ),
          ListTile(
            title: const Text('Tipo'),
            subtitle: Text(album!.primaryType),
          ),
          ListTile(
            title: const Text('Artistas'),
            subtitle: FutureBuilder(
              future: api!.getAlbumArtists(album!.id),
              initialData: const [],
              builder: (context, snapshot) => snapshot.hasData
                  ? ListView.builder(
                      shrinkWrap: true,
                      physics: const NeverScrollableScrollPhysics(),
                      itemCount: snapshot.data?.length,
                      itemBuilder: (context, index) {
                        Artist artist = snapshot.data![index];
                        return getArtistCard(artist, context);
                      })
                  : snapshot.hasError
                      ? const Text('Ocurrió un error')
                      : const CircularProgressIndicator(),
            ),
          )
        ],
      );

  @override
  Widget build(BuildContext context) {
    album = ModalRoute.of(context)?.settings.arguments as Album?;
    if (album == null) return const Placeholder();

    Image cover = Image.network(
      album!.coverUri.toString(),
      fit: BoxFit.cover,
      alignment: Alignment.center,
      errorBuilder: (context, error, stackTrace) => const AlbumPlaceholder(400),
      loadingBuilder: (context, child, loadingProgress) =>
          loadingProgress == null ? child : const AlbumPlaceholder(400),
    );

    return Scaffold(
      body: NestedScrollView(
        headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) =>
            <Widget>[
          SliverAppBar(
            expandedHeight: 400.0,
            pinned: true,
            flexibleSpace: FlexibleSpaceBar(
              title: Text(album!.title,
                  style: TextStyle(
                    backgroundColor:
                        Theme.of(context).colorScheme.primaryContainer,
                    color: Theme.of(context).colorScheme.onPrimaryContainer,
                  )),
              background: cover,
            ),
          )
        ],
        body: SingleChildScrollView(
          child: Responsive(
            mobile: Column(children: [getLeft(), getRight()]),
            tablet: Row(children: [
              Expanded(child: getLeft()),
              Expanded(child: getRight()),
            ]),
            desktop: Row(children: [
              Expanded(child: getLeft()),
              Expanded(child: getRight()),
            ]),
          ),
        ),
      ),
    );
  }
}