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: 'Book Catalog',
- theme: ThemeData(primarySwatch: Colors.blue),
- home: CategoryScreen(),
- );
- }
- }
- class CategoryScreen extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Genres'),
- ),
- 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('Author: ', 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('Pages: ${book.pages}', style: TextStyle(fontSize: 18)),
- SizedBox(height: 8),
- Text('Edition: ${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) {
- final genreBooks = books.where((book) => book.genreId == genre.id).toList();
- 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('Name: ${author.fullname}', style: TextStyle(fontSize: 24)),
- SizedBox(height: 8),
- Text('Birth Year: ${author.birthyear}', style: TextStyle(fontSize: 18)),
- SizedBox(height: 16),
- Text('Books in ${genre.name}:', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
- Expanded(
- child: ListView.builder(
- itemCount: genreBooks.length,
- itemBuilder: (context, index) {
- final book = genreBooks[index];
- return ListTile(
- title: Text(book.name),
- subtitle: Text(book.author),
- onTap: () {
- Navigator.push(
- context,
- MaterialPageRoute(
- builder: (context) => BookDetailScreen(book: book, genre: genre),
- ),
- );
- },
- );
- },
- ),
- ),
- ElevatedButton(
- onPressed: () {
- Navigator.pushAndRemoveUntil(
- context,
- MaterialPageRoute(
- builder: (context) => BooksScreen(genre: genre),
- ),
- ModalRoute.withName('/'),
- );
- },
- child: Text('Back to ${genre.name} Genre'),
- ),
- ],
- ),
- ),
- );
- }
- }
- // Dummy data models
- 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});
- }
- // Dummy data
- List<Book> books = [
- Book(id: 1, name: 'Cool Detective', author: 'Author1', pages: 165, edition: 'Edition1', previewUrl: 'https://example.com/book.jpg', genreId: 1),
- Book(id: 2, name: 'Fairy Tale for Kids', author: 'Author2', pages: 15, edition: 'Edition2', previewUrl: 'https://example.com/book.jpg', genreId: 2),
- // Add more books as needed
- ];
- List<Author> authors = [
- Author(id: 1, fullname: 'Author1', photo: 'https://example.com/person.jpg', birthyear: 1970),
- Author(id: 2, fullname: 'Author2', photo: 'https://example.com/person2.jpg', birthyear: 1980),
- // Add more authors as needed
- ];
- List<Genre> genres = [
- Genre(id: 1, name: 'Detective'),
- Genre(id: 2, name: 'Fairy Tale'),
- // Add more genres as needed
- ];
- /*
- SECOND VERSION
- import 'package:flutter/material.dart';
- void main() {
- runApp(BooksApp());
- }
- class BooksApp extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- title: 'Book Catalog',
- theme: ThemeData(primarySwatch: Colors.blue),
- home: CategoryScreen(),
- );
- }
- }
- class CategoryScreen extends StatelessWidget {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('Genres'),
- ),
- 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('Author: ', 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('Pages: ${book.pages}', style: TextStyle(fontSize: 18)),
- SizedBox(height: 8),
- Text('Edition: ${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) {
- final genreBooks = books.where((book) => book.genreId == genre.id).toList();
- 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('Name: ${author.fullname}', style: TextStyle(fontSize: 24)),
- SizedBox(height: 8),
- Text('Birth Year: ${author.birthyear}', style: TextStyle(fontSize: 18)),
- SizedBox(height: 16),
- ElevatedButton(
- onPressed: () {
- Navigator.pushAndRemoveUntil(
- context,
- MaterialPageRoute(
- builder: (context) => BooksScreen(genre: genre),
- ),
- ModalRoute.withName('/'),
- );
- },
- child: Text('Back to ${genre.name} Genre'),
- ),
- ],
- ),
- ),
- );
- }
- }
- // Dummy data models
- 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});
- }
- // Dummy data
- List<Book> books = [
- Book(id: 1, name: 'Cool Detective', author: 'Author1', pages: 165, edition: 'Edition1', previewUrl: 'https://example.com/book.jpg', genreId: 1),
- Book(id: 2, name: 'Fairy Tale for Kids', author: 'Author2', pages: 15, edition: 'Edition2', previewUrl: 'https://example.com/book.jpg', genreId: 2),
- // Add more books as needed
- ];
- List<Author> authors = [
- Author(id: 1, fullname: 'Author1', photo: 'https://example.com/person.jpg', birthyear: 1970),
- Author(id: 2, fullname: 'Author2', photo: 'https://example.com/person2.jpg', birthyear: 1980),
- // Add more authors as needed
- ];
- List<Genre> genres = [
- Genre(id: 1, name: 'Detective'),
- Genre(id: 2, name: 'Fairy Tale'),
- // Add more genres as needed
- ];
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement