Advertisement
ZergRushA

done_task

Oct 18th, 2024 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 6.54 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2.  
  3. void main() {
  4.   runApp(BooksApp());
  5. }
  6.  
  7. class BooksApp extends StatelessWidget {
  8.   @override
  9.   Widget build(BuildContext context) {
  10.     return MaterialApp(
  11.       title: 'Каталог книг',
  12.       theme: ThemeData(primarySwatch: Colors.blue),
  13.       home: CategoryScreen(),
  14.     );
  15.   }
  16. }
  17.  
  18. class CategoryScreen extends StatelessWidget {
  19.   @override
  20.   Widget build(BuildContext context) {
  21.     return Scaffold(
  22.       appBar: AppBar(
  23.         title: Text('Жанры'),
  24.       ),
  25.       body: ListView.builder(
  26.         itemCount: genres.length,
  27.         itemBuilder: (context, index) {
  28.           final genre = genres[index];
  29.           return ListTile(
  30.             title: Text(genre.name),
  31.             onTap: () {
  32.               Navigator.push(
  33.                 context,
  34.                 MaterialPageRoute(
  35.                   builder: (context) => BooksScreen(genre: genre),
  36.                 ),
  37.               );
  38.             },
  39.           );
  40.         },
  41.       ),
  42.     );
  43.   }
  44. }
  45.  
  46. class BooksScreen extends StatelessWidget {
  47.   final Genre genre;
  48.  
  49.   BooksScreen({required this.genre});
  50.  
  51.   @override
  52.   Widget build(BuildContext context) {
  53.     final genreBooks = books.where((book) => book.genreId == genre.id).toList();
  54.     return Scaffold(
  55.       appBar: AppBar(
  56.         title: Text(genre.name),
  57.       ),
  58.       body: ListView.builder(
  59.         itemCount: genreBooks.length,
  60.         itemBuilder: (context, index) {
  61.           final book = genreBooks[index];
  62.           return ListTile(
  63.             leading: Image.network(book.previewUrl, width: 50, height: 50, fit: BoxFit.cover),
  64.             title: Text(book.name),
  65.             subtitle: Text(book.author),
  66.             onTap: () {
  67.               Navigator.push(
  68.                 context,
  69.                 MaterialPageRoute(
  70.                   builder: (context) => BookDetailScreen(book: book, genre: genre),
  71.                 ),
  72.               );
  73.             },
  74.           );
  75.         },
  76.       ),
  77.     );
  78.   }
  79. }
  80.  
  81. class BookDetailScreen extends StatelessWidget {
  82.   final Book book;
  83.   final Genre genre;
  84.  
  85.   BookDetailScreen({required this.book, required this.genre});
  86.  
  87.   @override
  88.   Widget build(BuildContext context) {
  89.     final author = authors.firstWhere((author) => author.fullname == book.author);
  90.     return Scaffold(
  91.       appBar: AppBar(
  92.         title: Text(book.name),
  93.       ),
  94.       body: Padding(
  95.         padding: const EdgeInsets.all(16.0),
  96.         child: Column(
  97.           crossAxisAlignment: CrossAxisAlignment.start,
  98.           children: [
  99.             Image.network(book.previewUrl, height: 200, fit: BoxFit.cover),
  100.             SizedBox(height: 16),
  101.             Text(book.name, style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
  102.             SizedBox(height: 8),
  103.             Text('Автор: ', style: TextStyle(fontSize: 18)),
  104.             GestureDetector(
  105.               onTap: () {
  106.                 Navigator.push(
  107.                   context,
  108.                   MaterialPageRoute(
  109.                     builder: (context) => AuthorDetailScreen(author: author, genre: genre),
  110.                   ),
  111.                 );
  112.               },
  113.               child: Text(
  114.                 author.fullname,
  115.                 style: TextStyle(fontSize: 18, color: Colors.blue),
  116.               ),
  117.             ),
  118.             SizedBox(height: 8),
  119.             Text('Количество страниц: ${book.pages}', style: TextStyle(fontSize: 18)),
  120.             SizedBox(height: 8),
  121.             Text('Название издания: ${book.edition}', style: TextStyle(fontSize: 18)),
  122.           ],
  123.         ),
  124.       ),
  125.     );
  126.   }
  127. }
  128.  
  129. class AuthorDetailScreen extends StatelessWidget {
  130.   final Author author;
  131.   final Genre genre;
  132.  
  133.   AuthorDetailScreen({required this.author, required this.genre});
  134.  
  135.   @override
  136.   Widget build(BuildContext context) {
  137.     return Scaffold(
  138.       appBar: AppBar(
  139.         title: Text(author.fullname),
  140.       ),
  141.       body: Padding(
  142.         padding: const EdgeInsets.all(16.0),
  143.         child: Column(
  144.           crossAxisAlignment: CrossAxisAlignment.start,
  145.           children: [
  146.             Image.network(author.photo, height: 200, fit: BoxFit.cover),
  147.             SizedBox(height: 16),
  148.             Text('ФИО: ${author.fullname}', style: TextStyle(fontSize: 24)),
  149.             SizedBox(height: 8),
  150.             Text('Дата рождения: ${author.birthyear}', style: TextStyle(fontSize: 18)),
  151.             SizedBox(height: 16),
  152.             ElevatedButton(
  153.               onPressed: () {
  154.                 Navigator.pushAndRemoveUntil(
  155.                   context,
  156.                   MaterialPageRoute(
  157.                     builder: (context) => BooksScreen(genre: genre),
  158.                   ),
  159.                   ModalRoute.withName('/'),
  160.                 );
  161.               },
  162.               child: Text('Вернутся к жанру: ${genre.name}'),
  163.             ),
  164.           ],
  165.         ),
  166.       ),
  167.     );
  168.   }
  169. }
  170.  
  171.  
  172. class Book {
  173.   final int id;
  174.   final String name;
  175.   final String author;
  176.   final int pages;
  177.   final String edition;
  178.   final String previewUrl;
  179.   final int genreId;
  180.  
  181.   Book({
  182.     required this.id,
  183.     required this.name,
  184.     required this.author,
  185.     required this.pages,
  186.     required this.edition,
  187.     required this.previewUrl,
  188.     required this.genreId,
  189.   });
  190. }
  191.  
  192. class Author {
  193.   final int id;
  194.   final String fullname;
  195.   final String photo;
  196.   final int birthyear;
  197.  
  198.   Author({
  199.     required this.id,
  200.     required this.fullname,
  201.     required this.photo,
  202.     required this.birthyear,
  203.   });
  204. }
  205.  
  206. class Genre {
  207.   final int id;
  208.   final String name;
  209.  
  210.   Genre({required this.id, required this.name});
  211. }
  212.  
  213.  
  214. List<Book> books = [
  215.   Book(id: 1, name: 'Да не умер он в конце драйва', author: 'Райан Гослинг', pages: 198, edition: 'Легендарное издание', previewUrl: 'https://example.com/book.jpg', genreId: 1),
  216.   Book(id: 2, name: 'Тест книга', author: 'Тест автор', pages: 15, edition: 'тестовое издание', previewUrl: 'https://example.com/book.jpg', genreId: 2),
  217.  
  218. ];
  219.  
  220. List<Author> authors = [
  221.   Author(id: 1, fullname: 'Райан Гослинг', photo: 'https://cdn.britannica.com/93/215393-050-E428CADE/Canadian-actor-musician-Ryan-Gosling-2016.jpg?w=300', birthyear: 1970),
  222.   Author(id: 2, fullname: 'Тест автор', photo: 'https://example.com/person2.jpg', birthyear: 1980),
  223.  
  224. ];
  225.  
  226. List<Genre> genres = [
  227.   Genre(id: 1, name: 'Триллер'),
  228.   Genre(id: 2, name: 'Тестовый жанр'),
  229.  
  230. ];
  231.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement