Advertisement
mrblab

drawer

Mar 8th, 2024
781
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 10.74 KB | None | 0 0
  1. import 'package:cached_network_image/cached_network_image.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_icons/flutter_icons.dart';
  4. import 'package:wordpress_app/blocs/config_bloc.dart';
  5. import 'package:wordpress_app/blocs/settings_bloc.dart';
  6. import 'package:wordpress_app/config/wp_config.dart';
  7. import 'package:wordpress_app/pages/category_based_articles.dart';
  8. import 'package:wordpress_app/services/app_service.dart';
  9. import 'package:wordpress_app/utils/next_screen.dart';
  10. import 'package:wordpress_app/widgets/app_logo.dart';
  11. import '../blocs/category_bloc.dart';
  12. import '../models/category.dart';
  13. import 'package:provider/provider.dart';
  14. import 'package:easy_localization/easy_localization.dart';
  15.  
  16. import '../pages/sub_categories.dart';
  17.  
  18. class CustomDrawer extends StatelessWidget {
  19.   const CustomDrawer({super.key});
  20.  
  21.   @override
  22.   Widget build(BuildContext context) {
  23.     final categories = context.watch<CategoryBloc>().categoryData;
  24.     final configs = context.read<ConfigBloc>().configs!;
  25.  
  26.     final titleTextStyle = Theme.of(context).textTheme.titleMedium;
  27.  
  28.     return Drawer(
  29.       backgroundColor: Theme.of(context).colorScheme.background,
  30.       surfaceTintColor: Theme.of(context).colorScheme.background,
  31.       child: SingleChildScrollView(
  32.         child: Column(
  33.           crossAxisAlignment: CrossAxisAlignment.start,
  34.           children: [
  35.             // Header
  36.             Container(
  37.               height: 250,
  38.               width: double.infinity,
  39.               color: Theme.of(context).colorScheme.primaryContainer,
  40.               child: Column(
  41.                 mainAxisAlignment: MainAxisAlignment.center,
  42.                 crossAxisAlignment: CrossAxisAlignment.center,
  43.                 children: [
  44.                   Container(
  45.                     margin: const EdgeInsets.only(bottom: 8),
  46.                     child: const AppLogo(height: 35),
  47.                   ),
  48.                   Text(
  49.                     'app-version',
  50.                     style: Theme.of(context).textTheme.titleMedium,
  51.                   ).tr(args: [context.read<SettingsBloc>().appVersion])
  52.                 ],
  53.               ),
  54.             ),
  55.  
  56.             // Social Info
  57.             Container(
  58.               padding: const EdgeInsets.all(15),
  59.               child: Column(
  60.                 children: [
  61.                   ListTile(
  62.                     contentPadding: const EdgeInsets.all(0),
  63.                     leading: const Icon(
  64.                       Icons.email_outlined,
  65.                       size: 22,
  66.                     ),
  67.                     horizontalTitleGap: 10,
  68.                     title: Text('contact-us', style: titleTextStyle).tr(),
  69.                     onTap: () {
  70.                       Navigator.pop(context);
  71.                       AppService().openEmailSupport(context, configs.supportEmail);
  72.                     },
  73.                   ),
  74.                   ListTile(
  75.                     contentPadding: const EdgeInsets.all(0),
  76.                     leading: const Icon(
  77.                       Icons.link_outlined,
  78.                       size: 22,
  79.                     ),
  80.                     horizontalTitleGap: 10,
  81.                     title: Text('our-website', style: titleTextStyle).tr(),
  82.                     onTap: () {
  83.                       Navigator.pop(context);
  84.                       AppService().openLinkWithCustomTab(context, WpConfig.baseURL);
  85.                     },
  86.                   ),
  87.                   Visibility(
  88.                     visible: configs.fbUrl != '',
  89.                     child: ListTile(
  90.                       contentPadding: const EdgeInsets.all(0),
  91.                       leading: const Icon(
  92.                         Feather.facebook,
  93.                         size: 22,
  94.                       ),
  95.                       horizontalTitleGap: 10,
  96.                       title: Text('facebook', style: titleTextStyle).tr(),
  97.                       onTap: () {
  98.                         Navigator.pop(context);
  99.                         AppService().openLink(context, configs.fbUrl);
  100.                       },
  101.                     ),
  102.                   ),
  103.                   Visibility(
  104.                     visible: configs.youtubeUrl != '',
  105.                     child: ListTile(
  106.                       contentPadding: const EdgeInsets.all(0),
  107.                       leading: const Icon(
  108.                         Feather.youtube,
  109.                         size: 22,
  110.                       ),
  111.                       horizontalTitleGap: 10,
  112.                       title: Text('youtube', style: titleTextStyle).tr(),
  113.                       onTap: () {
  114.                         Navigator.pop(context);
  115.                         AppService().openLink(context, configs.youtubeUrl);
  116.                       },
  117.                     ),
  118.                   ),
  119.                   Visibility(
  120.                     visible: configs.twitterUrl != '',
  121.                     child: ListTile(
  122.                       contentPadding: const EdgeInsets.all(0),
  123.                       leading: const Icon(
  124.                         Feather.twitter,
  125.                         size: 22,
  126.                       ),
  127.                       horizontalTitleGap: 10,
  128.                       title: Text('twitter', style: titleTextStyle).tr(),
  129.                       onTap: () {
  130.                         Navigator.pop(context);
  131.                         AppService().openLink(context, configs.twitterUrl);
  132.                       },
  133.                     ),
  134.                   ),
  135.                   Visibility(
  136.                     visible: configs.instagramUrl != '',
  137.                     child: ListTile(
  138.                       contentPadding: const EdgeInsets.all(0),
  139.                       leading: const Icon(
  140.                         Feather.instagram,
  141.                         size: 22,
  142.                       ),
  143.                       horizontalTitleGap: 10,
  144.                       title: Text('instagram', style: titleTextStyle).tr(),
  145.                       onTap: () {
  146.                         Navigator.pop(context);
  147.                         AppService().openLink(context, configs.instagramUrl);
  148.                       },
  149.                     ),
  150.                   ),
  151.                   Visibility(
  152.                     visible: configs.threadsUrl != '',
  153.                     child: ListTile(
  154.                       contentPadding: const EdgeInsets.all(0),
  155.                       leading: const Icon(
  156.                         Feather.at_sign,
  157.                         size: 22,
  158.                       ),
  159.                       horizontalTitleGap: 10,
  160.                       title: Text('threads', style: titleTextStyle).tr(),
  161.                       onTap: () {
  162.                         Navigator.pop(context);
  163.                         AppService().openLink(context, configs.threadsUrl);
  164.                       },
  165.                     ),
  166.                   )
  167.                 ],
  168.               ),
  169.             ),
  170.             Divider(
  171.               color: Colors.grey[500],
  172.               thickness: 0.6,
  173.             ),
  174.             Padding(
  175.               padding: const EdgeInsets.only(left: 15, top: 15),
  176.               child: Text('categories', style: Theme.of(context).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.w600)).tr(),
  177.             ),
  178.  
  179.             // Categories
  180.             _Categories(
  181.               categories: categories,
  182.             ),
  183.           ],
  184.         ),
  185.       ),
  186.     );
  187.   }
  188. }
  189.  
  190. class _Categories extends StatelessWidget {
  191.   const _Categories({
  192.     required this.categories,
  193.   });
  194.  
  195.   final List<Category> categories;
  196.  
  197.   @override
  198.   Widget build(BuildContext context) {
  199.     return ListView.builder(
  200.       shrinkWrap: true,
  201.       physics: const NeverScrollableScrollPhysics(),
  202.       padding: const EdgeInsets.only(top: 10, bottom: 30),
  203.       itemCount: categories.length,
  204.       itemBuilder: (BuildContext context, int index) {
  205.         final Category category = categories[index];
  206.         final List<Category> subCategories = categories.where((element) => element.parent == category.id).toList();
  207.         final bool hasSubCategories = subCategories.isEmpty ? false : true;
  208.  
  209.         //subcategories removed from the category list
  210.         if (category.parent != 0) {
  211.           return const SizedBox.shrink();
  212.         }
  213.  
  214.         return ExpansionTile(
  215.             tilePadding: const EdgeInsets.only(left: 20, right: 15),
  216.             trailing: hasSubCategories ? null : const SizedBox.shrink(),
  217.             leading: CircleAvatar(
  218.               radius: 15,
  219.               backgroundImage: CachedNetworkImageProvider(category.categoryThumbnail!),
  220.             ),
  221.             title: InkWell(
  222.               child: Text(
  223.                 category.name.toString().toUpperCase(),
  224.                 style: Theme.of(context).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.w600, color: Theme.of(context).colorScheme.secondary),
  225.               ),
  226.               onTap: () {
  227.                 Navigator.pop(context);
  228.                 if (hasSubCategories) {
  229.                   nextScreeniOS(context, SubCategories(category: category, subCategories: subCategories));
  230.                 } else {
  231.                   nextScreeniOS(context, CategoryBasedArticles(category: category));
  232.                 }
  233.               },
  234.             ),
  235.             initiallyExpanded: false,
  236.             childrenPadding: const EdgeInsets.only(left: 20, right: 15),
  237.             children: <Widget>[
  238.               ListView.builder(
  239.                 shrinkWrap: true,
  240.                 itemCount: categories.length,
  241.                 padding: const EdgeInsets.all(0),
  242.                 itemBuilder: (BuildContext context1, int index1) {
  243.                   Category subCategory = categories[index1];
  244.                   if (subCategory.parent == categories[index].id) {
  245.                     return ListTile(
  246.                       title: Text(
  247.                         subCategory.name!,
  248.                         style: Theme.of(context).textTheme.titleMedium?.copyWith(
  249.                               fontWeight: FontWeight.w600,
  250.                               color: Theme.of(context).colorScheme.secondary,
  251.                             ),
  252.                       ),
  253.                       horizontalTitleGap: 20,
  254.                       leading: CircleAvatar(
  255.                         radius: 12,
  256.                         backgroundImage: CachedNetworkImageProvider(subCategory.categoryThumbnail!),
  257.                       ),
  258.                       trailing: const Icon(Icons.chevron_right),
  259.                       onTap: () {
  260.                         Navigator.pop(context);
  261.                         nextScreen(context, CategoryBasedArticles(category: subCategory));
  262.                       },
  263.                     );
  264.                   }
  265.  
  266.                   return Container();
  267.                 },
  268.               ),
  269.             ]);
  270.       },
  271.     );
  272.   }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement