lab2 + lab 3

This commit is contained in:
Галина Федоренко 2024-10-03 11:45:55 +04:00
parent cd57b60b42
commit 9a5abe22b9
8 changed files with 143 additions and 28 deletions

View File

@ -0,0 +1,6 @@
enum Genre {
rock,
pop,
jazz,
classical,
}

View File

@ -0,0 +1,7 @@
import '../models/album.dart';
extension AlbumListExtensions on List<Album> {
List<Album> sortByTitle() {
return this..sort((a, b) => a.title.compareTo(b.title));
}
}

View File

@ -1,4 +1,8 @@
import 'package:flutter/material.dart';
import 'models/album.dart';
import 'repositories/album_repository.dart';
import 'services/album_service.dart';
import 'widgets/album_card.dart';
void main() {
runApp(const MyApp());
@ -10,12 +14,11 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
title: 'Music Reviewer',
theme: ThemeData.dark().copyWith(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.purpleAccent)
),
home: const MyHomePage(title: 'Федоренко Галина Юрьевна'),
home: const MyHomePage(title: 'Music Reviewer'),
);
}
}
@ -30,12 +33,18 @@ class MyHomePage extends StatefulWidget {
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
final AlbumRepository _albumRepository = AlbumRepository();
List<Album> _albums = [];
void _incrementCounter() {
setState(() {
_counter++;
});
@override
void initState() {
super.initState();
_loadAlbums();
}
Future<void> _loadAlbums() async {
_albums = await fetchAlbums();
setState(() {});
}
@override
@ -45,24 +54,12 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
body: ListView.builder(
itemCount: _albums.length,
itemBuilder: (context, index) {
final album = _albums[index];
return AlbumCard(album: album);
},
),
);
}

View File

@ -0,0 +1,12 @@
import 'review.dart';
import '../enums/genre.dart';
class Album {
final String title;
final String artist;
final Genre genre;
final String imageUrl;
final List<Review> reviews = [];
Album(this.title, this.artist, this.genre, this.imageUrl);
}

View File

@ -0,0 +1,6 @@
class Review {
final String text;
final int rating;
Review(this.text, this.rating);
}

View File

@ -0,0 +1,20 @@
import '../models/album.dart';
import '../models/review.dart';
class AlbumRepository {
final List<Album> _albums = [];
void addAlbum(Album album) {
_albums.add(album);
}
void addReview(String albumTitle, Review review) {
final album = _albums.firstWhere((album) => album.title == albumTitle);
album.reviews.add(review);
}
List<Review> getReviews(String albumTitle) {
final album = _albums.firstWhere((album) => album.title == albumTitle);
return album.reviews;
}
}

View File

@ -0,0 +1,11 @@
import 'dart:async';
import '../models/album.dart';
import '../enums/genre.dart';
Future<List<Album>> fetchAlbums() async {
return [
Album('The Dark Side of the Moon', 'Pink Floyd', Genre.rock, 'https://upload.wikimedia.org/wikipedia/ru/1/15/The_Dark_Side_of_the_Moon.png'),
Album('Thriller', 'Michael Jackson', Genre.pop, 'https://upload.wikimedia.org/wikipedia/ru/5/59/Thriller_cover.jpg'),
Album('Back in Black', 'AC/DC', Genre.rock, 'https://www.castlerock.ru/upload/iblock/abe/kx6ghd078yfccf5vzaqc19zcv3lt96jc.jpg'),
];
}

View File

@ -0,0 +1,56 @@
import 'package:flutter/material.dart';
import '../models/album.dart';
class AlbumCard extends StatelessWidget {
final Album album;
const AlbumCard({super.key, required this.album});
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Image.network(
album.imageUrl,
width: double.infinity,
height: 150,
fit: BoxFit.cover,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
album.title,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
Text(
album.artist,
style: const TextStyle(
fontSize: 16,
color: Colors.grey,
),
),
Text(
'${album.reviews.length} reviews',
style: const TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
],
),
),
],
),
);
}
}