Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- void main() {
- runApp(BooksApp());
- }
- class BooksApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Каталог книг',
- theme: ThemeData(primarySwatch: Colors.blue),
- home: CategoryScreen(),
- );
- }
- }
- class CategoryScreen extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Жанры'),
- ),
- body: ListView.builder(
- itemCount: genres.length,
- itemBuilder: (context, index) {
- final genre = genres[index];
- return ListTile(
- title: Text(genre.name),
- onTap: () {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => BooksScreen(genre: genre),
- ),
- );
- },
- );
- },
- ),
- );
- }
- }
- class BooksScreen extends StatelessWidget {
- final Genre genre;
- BooksScreen({required this.genre});
- @override
- Widget build(BuildContext context) {
- final genreBooks = books.where((book) => book.genreId == genre.id).toList();
- return Scaffold(
- appBar: AppBar(
- title: Text(genre.name),
- ),
- body: ListView.builder(
- itemCount: genreBooks.length,
- itemBuilder: (context, index) {
- final book = genreBooks[index];
- return ListTile(
- leading: Image.network(book.previewUrl, width: 50, height: 50, fit: BoxFit.cover),
- title: Text(book.name),
- subtitle: Text(book.author),
- onTap: () {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => BookDetailScreen(book: book, genre: genre),
- ),
- );
- },
- );
- },
- ),
- );
- }
- }
- class BookDetailScreen extends StatelessWidget {
- final Book book;
- final Genre genre;
- BookDetailScreen({required this.book, required this.genre});
- @override
- Widget build(BuildContext context) {
- final author = authors.firstWhere((author) => author.fullname == book.author);
- return Scaffold(
- appBar: AppBar(
- title: Text(book.name),
- ),
- body: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Image.network(book.previewUrl, height: 200, fit: BoxFit.cover),
- SizedBox(height: 16),
- Text(book.name, style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
- SizedBox(height: 8),
- Text('Автор: ', style: TextStyle(fontSize: 18)),
- GestureDetector(
- onTap: () {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => AuthorDetailScreen(author: author, genre: genre),
- ),
- );
- },
- child: Text(
- author.fullname,
- style: TextStyle(fontSize: 18, color: Colors.blue),
- ),
- ),
- SizedBox(height: 8),
- Text('Количество страниц: ${book.pages}', style: TextStyle(fontSize: 18)),
- SizedBox(height: 8),
- Text('Название издания: ${book.edition}', style: TextStyle(fontSize: 18)),
- ],
- ),
- ),
- );
- }
- }
- class AuthorDetailScreen extends StatelessWidget {
- final Author author;
- final Genre genre;
- AuthorDetailScreen({required this.author, required this.genre});
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text(author.fullname),
- ),
- body: Padding(
- padding: const EdgeInsets.all(16.0),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Image.network(author.photo, height: 200, fit: BoxFit.cover),
- SizedBox(height: 16),
- Text('ФИО: ${author.fullname}', style: TextStyle(fontSize: 24)),
- SizedBox(height: 8),
- Text('Дата рождения: ${author.birthyear}', style: TextStyle(fontSize: 18)),
- SizedBox(height: 16),
- ElevatedButton(
- onPressed: () {
- Navigator.pushAndRemoveUntil(
- context,
- MaterialPageRoute(
- builder: (context) => BooksScreen(genre: genre),
- ),
- ModalRoute.withName('/'),
- );
- },
- child: Text('Вернутся к жанру: ${genre.name}'),
- ),
- ],
- ),
- ),
- );
- }
- }
- class Book {
- final int id;
- final String name;
- final String author;
- final int pages;
- final String edition;
- final String previewUrl;
- final int genreId;
- Book({
- required this.id,
- required this.name,
- required this.author,
- required this.pages,
- required this.edition,
- required this.previewUrl,
- required this.genreId,
- });
- }
- class Author {
- final int id;
- final String fullname;
- final String photo;
- final int birthyear;
- Author({
- required this.id,
- required this.fullname,
- required this.photo,
- required this.birthyear,
- });
- }
- class Genre {
- final int id;
- final String name;
- Genre({required this.id, required this.name});
- }
- List<Book> books = [
- Book(id: 1, name: 'Да не умер он в конце драйва', author: 'Райан Гослинг', pages: 198, edition: 'Легендарное издание', previewUrl: 'https://example.com/book.jpg', genreId: 1),
- Book(id: 2, name: 'Тест книга', author: 'Тест автор', pages: 15, edition: 'тестовое издание', previewUrl: 'https://example.com/book.jpg', genreId: 2),
- ];
- List<Author> authors = [
- Author(id: 1, fullname: 'Райан Гослинг', photo: 'https://cdn.britannica.com/93/215393-050-E428CADE/Canadian-actor-musician-Ryan-Gosling-2016.jpg?w=300', birthyear: 1970),
- Author(id: 2, fullname: 'Тест автор', photo: 'https://example.com/person2.jpg', birthyear: 1980),
- ];
- List<Genre> genres = [
- Genre(id: 1, name: 'Триллер'),
- Genre(id: 2, name: 'Тестовый жанр'),
- ];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement