создала вторую ветку

This commit is contained in:
bulatova_karina 2024-09-22 21:49:20 +04:00
parent 6e57eab74d
commit 7d6861624a
4 changed files with 131 additions and 28 deletions

6
lib/genre.dart Normal file
View File

@ -0,0 +1,6 @@
enum Genre {
action,
comedy,
drama,
thriller,
}

View File

@ -1,13 +1,35 @@
import 'package:flutter/material.dart';
import 'dart:async';
import 'movie.dart';
import 'genre.dart';
import 'movieGenerator.dart';
void main() {
runApp(const MyApp());
}
// Расширение для Genre, добавляющее метод для получения описания жанра
extension GenreExtension on Genre {
String get description {
switch (this) {
case Genre.action:
return 'Боевик!!!';
case Genre.comedy:
return 'Прикольная комедия';
case Genre.drama:
return 'Грустная драма';
case Genre.thriller:
return 'Психологический триллер';
}
}
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
@ -23,8 +45,6 @@ class MyApp extends StatelessWidget {
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
@ -32,39 +52,71 @@ class MyHomePage extends StatefulWidget {
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
List<Movie> _movies = [];
bool _isLoading = true;
@override
void initState() {
super.initState();
_loadMovies();
}
Future<void> _loadMovies() async {
final generator = MovieGenerator(count: 5);
final movies = await generator.generateMoviesWithDelay();
final sortedMovies = generator.sortMovies(movies);
void _incrementCounter() {
setState(() {
_counter++;
_movies = sortedMovies;
_isLoading = false;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
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,
),
],
),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: ListView.builder(
itemCount: _movies.length,
itemBuilder: (context, index) {
final movie = _movies[index];
return ListTile(
title: Text(movie.title),
subtitle: Text('Genre: ${movie.genre.description}, Rating: ${movie.rating.toStringAsFixed(1)}'),
onTap: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(movie.title),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Genre: ${movie.genre.description}'),
Text('Rating: ${movie.rating.toStringAsFixed(1)}'),
],
),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: const Text('Close'),
),
],
);
},
);
},
);
},
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), );
);
}
}
}

17
lib/movie.dart Normal file
View File

@ -0,0 +1,17 @@
import 'package:project1/main.dart';
import 'genre.dart';
class Movie {
final String title;
final Genre genre;
final double rating;
Movie({required this.title, required this.genre, required this.rating});
// Метод для вывода информации о фильме
void printDetails() {
print('Title: $title');
print('Genre: ${genre.description}');
print('Rating: $rating');
}
}

28
lib/movieGenerator.dart Normal file
View File

@ -0,0 +1,28 @@
import 'movie.dart';
import 'genre.dart';
import 'dart:math';
class MovieGenerator {
final int count;
MovieGenerator({required this.count});
// Метод для имитации задержки и генерации списка фильмов
Future<List<Movie>> generateMoviesWithDelay() async {
// Имитация задержки
await Future.delayed(Duration(seconds: 2));
final random = Random();
return List.generate(count, (index) {
final title = 'Movie ${index + 1}';
final genre = Genre.values[random.nextInt(Genre.values.length)];
final rating = random.nextDouble() * 10;
return Movie(title: title, genre: genre, rating: rating);
});
}
// Метод для сортировки списка фильмов по рейтингу
List<Movie> sortMovies(List<Movie> movies) {
return movies..sort((a, b) => b.rating.compareTo(a.rating));
}
}