Skip to content

Commit

Permalink
cleaner way of switching view
Browse files Browse the repository at this point in the history
  • Loading branch information
apoleon33 committed Jan 3, 2024
1 parent cb53848 commit 1ae0ee7
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 181 deletions.
15 changes: 15 additions & 0 deletions lib/album.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Album {
String cover = 'assets/default.png';

String name = "album";
String artist = "artist";
String genre = "genre";
double averageRating = 5.0;

Album(
{required this.cover,
required this.name,
required this.artist,
required this.genre,
required this.averageRating});
}
197 changes: 16 additions & 181 deletions lib/albumCard/albumCard.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'package:cradle/moreInfoMenu.dart';
import 'package:cradle/album.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:dio/dio.dart';

import 'display_as_card.dart';
import 'display_as_list.dart';

class AlbumCard extends StatefulWidget {
late DateTime date;
bool isCard;
Expand Down Expand Up @@ -64,185 +66,18 @@ class _AlbumCardState extends State<AlbumCard> {

@override
Widget build(BuildContext context) {
return widget.isCard ? displayAsCard(context) : displayAsList();
}

Widget displayAsList() {
return ListTile(
leading: cover == 'assets/default.png'
? Image.asset(
cover,
fit: BoxFit.fitWidth,
)
: Image.network(
cover,
fit: BoxFit.fitWidth,
),
title: Text(name),
subtitle: Text(artist),
trailing: Text(
(_dateIsToday(date))
? "today"
: (_dateIsYesterday(date))
? "yesterday"
: "${date.day}/${date.month}/${date.year}",
style: Theme.of(context).textTheme.labelSmall,
textAlign: TextAlign.right,
),
style: ListTileStyle.list,
isThreeLine: true,
final Album album = Album(
cover: cover,
name: name,
artist: artist,
genre: genre,
averageRating: averageRating,
);
}

Widget displayAsCard(BuildContext context) {
return AnimatedSwitcher(
duration: const Duration(milliseconds: 250),
child: Center(
child: SizedBox(
width: MediaQuery.of(context).size.width - 32,
height: MediaQuery.of(context).size.height / 2,
child: Card(
elevation: 0,
color: Theme.of(context).colorScheme.surfaceVariant,
child: Column(
children: [
Expanded(
flex: 55,
child: ClipRRect(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
child: SizedBox(
width: MediaQuery.of(context).size.width,
child: cover == 'assets/default.png'
? Image.asset(
cover,
fit: BoxFit.fitWidth,
)
: Image.network(
cover,
fit: BoxFit.fitWidth,
),
)),
),
Expanded(
flex: 45,
child: Column(
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Padding(
padding:
const EdgeInsets.only(left: 8, top: 8),
child: Text(
name.length > 18
? '${name.substring(0, 15)}...'
: name,
maxLines: 1,
style: Theme.of(context)
.textTheme
.headlineSmall),
),
Padding(
padding: const EdgeInsets.only(
right: 8, left: 8),
child: Text(
(_dateIsToday(date))
? "today"
: (_dateIsYesterday(date))
? "yesterday"
: "${date.day}/${date.month}/${date.year}",
style: Theme.of(context)
.textTheme
.bodyLarge,
textAlign: TextAlign.right,
))
],
),
Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 8),
child: Text(
"by $artist",
style: Theme.of(context)
.textTheme
.bodyMedium,
),
)
],
),
Row(
children: [
Padding(
padding: const EdgeInsets.only(left: 8),
child: ActionChip(
avatar: const Icon(Icons.music_note),
label: Text(
genre,
style: TextStyle(
color: Theme.of(context)
.colorScheme
.onSecondaryContainer),
),
onPressed: () {},
backgroundColor: Theme.of(context)
.colorScheme
.secondaryContainer,
),
),
Padding(
padding: const EdgeInsets.only(left: 8),
child: ActionChip(
avatar:
SvgPicture.asset("assets/rym.svg"),
label: Text(
"$averageRating/5",
style: TextStyle(
color: Theme.of(context)
.colorScheme
.onSecondaryContainer),
),
onPressed: () {},
backgroundColor: Theme.of(context)
.colorScheme
.secondaryContainer,
),
),
],
),
Padding(
padding: const EdgeInsets.only(
left: 8, top: 16, right: 8),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
const MoreInfoMenu(),

// new Spacer(),
FilledButton.icon(
icon: SvgPicture.asset(
"assets/spotify.svg",
color: Theme.of(context)
.colorScheme
.onPrimary,
width: 18,
height: 18,
),
onPressed: null,
label: const Text("Listen on Spotify"),
),
],
),
)
],
))
],
))),
));
return widget.isCard
? DisplayAlbumAsCard(
album: album,
date: date,
)
: DisplayAsList(album: album, date: date);
}
}
24 changes: 24 additions & 0 deletions lib/albumCard/display_album.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:flutter/material.dart';

abstract class DisplayAlbum extends StatelessWidget {
const DisplayAlbum({super.key});

bool dateIsToday(DateTime date) {
DateTime timeNow = DateTime.now();
return ((date.year == timeNow.year) &&
(date.month == timeNow.month) &&
(date.day == timeNow.day));
}

bool dateIsYesterday(DateTime date) {
DateTime timeNow = DateTime.now();
return ((date.year == timeNow.year) &&
(date.month == timeNow.month) &&
(date.day == (timeNow.day - 1)));
}

@override
Widget build(BuildContext context) => displayAlbum(context);

Widget displayAlbum(BuildContext context);
}
Loading

0 comments on commit 1ae0ee7

Please sign in to comment.