Advertisement
ZergRushA

dart_task

Oct 18th, 2024 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 13.67 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: 'Book Catalog',
  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('Genres'),
  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('Author: ', 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('Pages: ${book.pages}', style: TextStyle(fontSize: 18)),
  120.             SizedBox(height: 8),
  121.             Text('Edition: ${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.     final genreBooks = books.where((book) => book.genreId == genre.id).toList();
  138.     return Scaffold(
  139.       appBar: AppBar(
  140.         title: Text(author.fullname),
  141.       ),
  142.       body: Padding(
  143.         padding: const EdgeInsets.all(16.0),
  144.         child: Column(
  145.           crossAxisAlignment: CrossAxisAlignment.start,
  146.           children: [
  147.             Image.network(author.photo, height: 200, fit: BoxFit.cover),
  148.             SizedBox(height: 16),
  149.             Text('Name: ${author.fullname}', style: TextStyle(fontSize: 24)),
  150.             SizedBox(height: 8),
  151.             Text('Birth Year: ${author.birthyear}', style: TextStyle(fontSize: 18)),
  152.             SizedBox(height: 16),
  153.             Text('Books in ${genre.name}:', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
  154.             Expanded(
  155.               child: ListView.builder(
  156.                 itemCount: genreBooks.length,
  157.                 itemBuilder: (context, index) {
  158.                   final book = genreBooks[index];
  159.                   return ListTile(
  160.                     title: Text(book.name),
  161.                     subtitle: Text(book.author),
  162.                     onTap: () {
  163.                       Navigator.push(
  164.                         context,
  165.                         MaterialPageRoute(
  166.                           builder: (context) => BookDetailScreen(book: book, genre: genre),
  167.                         ),
  168.                       );
  169.                     },
  170.                   );
  171.                 },
  172.               ),
  173.             ),
  174.             ElevatedButton(
  175.               onPressed: () {
  176.                 Navigator.pushAndRemoveUntil(
  177.                   context,
  178.                   MaterialPageRoute(
  179.                     builder: (context) => BooksScreen(genre: genre),
  180.                   ),
  181.                   ModalRoute.withName('/'),
  182.                 );
  183.               },
  184.               child: Text('Back to ${genre.name} Genre'),
  185.             ),
  186.           ],
  187.         ),
  188.       ),
  189.     );
  190.   }
  191. }
  192.  
  193. // Dummy data models
  194. class Book {
  195.   final int id;
  196.   final String name;
  197.   final String author;
  198.   final int pages;
  199.   final String edition;
  200.   final String previewUrl;
  201.   final int genreId;
  202.  
  203.   Book({
  204.     required this.id,
  205.     required this.name,
  206.     required this.author,
  207.     required this.pages,
  208.     required this.edition,
  209.     required this.previewUrl,
  210.     required this.genreId,
  211.   });
  212. }
  213.  
  214. class Author {
  215.   final int id;
  216.   final String fullname;
  217.   final String photo;
  218.   final int birthyear;
  219.  
  220.   Author({
  221.     required this.id,
  222.     required this.fullname,
  223.     required this.photo,
  224.     required this.birthyear,
  225.   });
  226. }
  227.  
  228. class Genre {
  229.   final int id;
  230.   final String name;
  231.  
  232.   Genre({required this.id, required this.name});
  233. }
  234.  
  235. // Dummy data
  236. List<Book> books = [
  237.   Book(id: 1, name: 'Cool Detective', author: 'Author1', pages: 165, edition: 'Edition1', previewUrl: 'https://example.com/book.jpg', genreId: 1),
  238.   Book(id: 2, name: 'Fairy Tale for Kids', author: 'Author2', pages: 15, edition: 'Edition2', previewUrl: 'https://example.com/book.jpg', genreId: 2),
  239.   // Add more books as needed
  240. ];
  241.  
  242. List<Author> authors = [
  243.   Author(id: 1, fullname: 'Author1', photo: 'https://example.com/person.jpg', birthyear: 1970),
  244.   Author(id: 2, fullname: 'Author2', photo: 'https://example.com/person2.jpg', birthyear: 1980),
  245.   // Add more authors as needed
  246. ];
  247.  
  248. List<Genre> genres = [
  249.   Genre(id: 1, name: 'Detective'),
  250.   Genre(id: 2, name: 'Fairy Tale'),
  251.   // Add more genres as needed
  252. ];
  253.  
  254. /*
  255. SECOND VERSION
  256. import 'package:flutter/material.dart';
  257.  
  258. void main() {
  259.   runApp(BooksApp());
  260. }
  261.  
  262. class BooksApp extends StatelessWidget {
  263.   @override
  264.   Widget build(BuildContext context) {
  265.     return MaterialApp(
  266.       title: 'Book Catalog',
  267.       theme: ThemeData(primarySwatch: Colors.blue),
  268.       home: CategoryScreen(),
  269.     );
  270.   }
  271. }
  272.  
  273. class CategoryScreen extends StatelessWidget {
  274.   @override
  275.   Widget build(BuildContext context) {
  276.     return Scaffold(
  277.       appBar: AppBar(
  278.         title: Text('Genres'),
  279.       ),
  280.       body: ListView.builder(
  281.         itemCount: genres.length,
  282.         itemBuilder: (context, index) {
  283.           final genre = genres[index];
  284.           return ListTile(
  285.             title: Text(genre.name),
  286.             onTap: () {
  287.               Navigator.push(
  288.                 context,
  289.                 MaterialPageRoute(
  290.                   builder: (context) => BooksScreen(genre: genre),
  291.                 ),
  292.               );
  293.             },
  294.           );
  295.         },
  296.       ),
  297.     );
  298.   }
  299. }
  300.  
  301. class BooksScreen extends StatelessWidget {
  302.   final Genre genre;
  303.  
  304.   BooksScreen({required this.genre});
  305.  
  306.   @override
  307.   Widget build(BuildContext context) {
  308.     final genreBooks = books.where((book) => book.genreId == genre.id).toList();
  309.     return Scaffold(
  310.       appBar: AppBar(
  311.         title: Text(genre.name),
  312.       ),
  313.       body: ListView.builder(
  314.         itemCount: genreBooks.length,
  315.         itemBuilder: (context, index) {
  316.           final book = genreBooks[index];
  317.           return ListTile(
  318.             leading: Image.network(book.previewUrl, width: 50, height: 50, fit: BoxFit.cover),
  319.             title: Text(book.name),
  320.             subtitle: Text(book.author),
  321.             onTap: () {
  322.               Navigator.push(
  323.                 context,
  324.                 MaterialPageRoute(
  325.                   builder: (context) => BookDetailScreen(book: book, genre: genre),
  326.                 ),
  327.               );
  328.             },
  329.           );
  330.         },
  331.       ),
  332.     );
  333.   }
  334. }
  335.  
  336. class BookDetailScreen extends StatelessWidget {
  337.   final Book book;
  338.   final Genre genre;
  339.  
  340.   BookDetailScreen({required this.book, required this.genre});
  341.  
  342.   @override
  343.   Widget build(BuildContext context) {
  344.     final author = authors.firstWhere((author) => author.fullname == book.author);
  345.     return Scaffold(
  346.       appBar: AppBar(
  347.         title: Text(book.name),
  348.       ),
  349.       body: Padding(
  350.         padding: const EdgeInsets.all(16.0),
  351.         child: Column(
  352.           crossAxisAlignment: CrossAxisAlignment.start,
  353.           children: [
  354.             Image.network(book.previewUrl, height: 200, fit: BoxFit.cover),
  355.             SizedBox(height: 16),
  356.             Text(book.name, style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold)),
  357.             SizedBox(height: 8),
  358.             Text('Author: ', style: TextStyle(fontSize: 18)),
  359.             GestureDetector(
  360.               onTap: () {
  361.                 Navigator.push(
  362.                   context,
  363.                   MaterialPageRoute(
  364.                     builder: (context) => AuthorDetailScreen(author: author, genre: genre),
  365.                   ),
  366.                 );
  367.               },
  368.               child: Text(
  369.                 author.fullname,
  370.                 style: TextStyle(fontSize: 18, color: Colors.blue),
  371.               ),
  372.             ),
  373.             SizedBox(height: 8),
  374.             Text('Pages: ${book.pages}', style: TextStyle(fontSize: 18)),
  375.             SizedBox(height: 8),
  376.             Text('Edition: ${book.edition}', style: TextStyle(fontSize: 18)),
  377.           ],
  378.         ),
  379.       ),
  380.     );
  381.   }
  382. }
  383.  
  384. class AuthorDetailScreen extends StatelessWidget {
  385.   final Author author;
  386.   final Genre genre;
  387.  
  388.   AuthorDetailScreen({required this.author, required this.genre});
  389.  
  390.   @override
  391.   Widget build(BuildContext context) {
  392.     final genreBooks = books.where((book) => book.genreId == genre.id).toList();
  393.     return Scaffold(
  394.       appBar: AppBar(
  395.         title: Text(author.fullname),
  396.       ),
  397.       body: Padding(
  398.         padding: const EdgeInsets.all(16.0),
  399.         child: Column(
  400.           crossAxisAlignment: CrossAxisAlignment.start,
  401.           children: [
  402.             Image.network(author.photo, height: 200, fit: BoxFit.cover),
  403.             SizedBox(height: 16),
  404.             Text('Name: ${author.fullname}', style: TextStyle(fontSize: 24)),
  405.             SizedBox(height: 8),
  406.             Text('Birth Year: ${author.birthyear}', style: TextStyle(fontSize: 18)),
  407.             SizedBox(height: 16),
  408.             ElevatedButton(
  409.               onPressed: () {
  410.                 Navigator.pushAndRemoveUntil(
  411.                   context,
  412.                   MaterialPageRoute(
  413.                     builder: (context) => BooksScreen(genre: genre),
  414.                   ),
  415.                   ModalRoute.withName('/'),
  416.                 );
  417.               },
  418.               child: Text('Back to ${genre.name} Genre'),
  419.             ),
  420.           ],
  421.         ),
  422.       ),
  423.     );
  424.   }
  425. }
  426.  
  427. // Dummy data models
  428. class Book {
  429.   final int id;
  430.   final String name;
  431.   final String author;
  432.   final int pages;
  433.   final String edition;
  434.   final String previewUrl;
  435.   final int genreId;
  436.  
  437.   Book({
  438.     required this.id,
  439.     required this.name,
  440.     required this.author,
  441.     required this.pages,
  442.     required this.edition,
  443.     required this.previewUrl,
  444.     required this.genreId,
  445.   });
  446. }
  447.  
  448. class Author {
  449.   final int id;
  450.   final String fullname;
  451.   final String photo;
  452.   final int birthyear;
  453.  
  454.   Author({
  455.     required this.id,
  456.     required this.fullname,
  457.     required this.photo,
  458.     required this.birthyear,
  459.   });
  460. }
  461.  
  462. class Genre {
  463.   final int id;
  464.   final String name;
  465.  
  466.   Genre({required this.id, required this.name});
  467. }
  468.  
  469. // Dummy data
  470. List<Book> books = [
  471.   Book(id: 1, name: 'Cool Detective', author: 'Author1', pages: 165, edition: 'Edition1', previewUrl: 'https://example.com/book.jpg', genreId: 1),
  472.   Book(id: 2, name: 'Fairy Tale for Kids', author: 'Author2', pages: 15, edition: 'Edition2', previewUrl: 'https://example.com/book.jpg', genreId: 2),
  473.   // Add more books as needed
  474. ];
  475.  
  476. List<Author> authors = [
  477.   Author(id: 1, fullname: 'Author1', photo: 'https://example.com/person.jpg', birthyear: 1970),
  478.   Author(id: 2, fullname: 'Author2', photo: 'https://example.com/person2.jpg', birthyear: 1980),
  479.   // Add more authors as needed
  480. ];
  481.  
  482. List<Genre> genres = [
  483.   Genre(id: 1, name: 'Detective'),
  484.   Genre(id: 2, name: 'Fairy Tale'),
  485.   // Add more genres as needed
  486. ];
  487.  
  488.  
  489.  
  490. */
  491.  
  492.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement