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

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,9 +1,31 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'dart:async';
import 'movie.dart';
import 'genre.dart';
import 'movieGenerator.dart';
void main() { void main() {
runApp(const MyApp()); 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 { class MyApp extends StatelessWidget {
const MyApp({super.key}); const MyApp({super.key});
@ -23,8 +45,6 @@ class MyApp extends StatelessWidget {
class MyHomePage extends StatefulWidget { class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title}); const MyHomePage({super.key, required this.title});
final String title; final String title;
@override @override
@ -32,11 +52,23 @@ class MyHomePage extends StatefulWidget {
} }
class _MyHomePageState extends State<MyHomePage> { 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(() { setState(() {
_counter++; _movies = sortedMovies;
_isLoading = false;
}); });
} }
@ -47,24 +79,44 @@ class _MyHomePageState extends State<MyHomePage> {
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title), title: Text(widget.title),
), ),
body: Center( body: _isLoading
child: Column( ? const Center(child: CircularProgressIndicator())
mainAxisAlignment: MainAxisAlignment.center, : ListView.builder(
children: <Widget>[ itemCount: _movies.length,
const Text( itemBuilder: (context, index) {
'You have pushed the button this many times:', final movie = _movies[index];
), return ListTile(
Text( title: Text(movie.title),
'$_counter', subtitle: Text('Genre: ${movie.genre.description}, Rating: ${movie.rating.toStringAsFixed(1)}'),
style: Theme.of(context).textTheme.headlineMedium, 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));
}
}