Advertisement
mrblab24

newshour_bookmarks.dart

May 25th, 2022
655
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_icons/flutter_icons.dart';
  3. import 'package:news_app/blocs/bookmark_bloc.dart';
  4. import 'package:news_app/blocs/sign_in_bloc.dart';
  5. import 'package:news_app/cards/card4.dart';
  6. import 'package:news_app/utils/empty.dart';
  7. import 'package:news_app/utils/loading_cards.dart';
  8. import 'package:provider/provider.dart';
  9. import 'package:easy_localization/easy_localization.dart';
  10.  
  11. class BookmarkPage extends StatefulWidget {
  12.   const BookmarkPage({Key? key}) : super(key: key);
  13.  
  14.   @override
  15.   _BookmarkPageState createState() => _BookmarkPageState();
  16. }
  17.  
  18. class _BookmarkPageState extends State<BookmarkPage>
  19.     with AutomaticKeepAliveClientMixin {
  20.   @override
  21.   Widget build(BuildContext context) {
  22.     super.build(context);
  23.     final SignInBloc sb = context.watch<SignInBloc>();
  24.  
  25.     return DefaultTabController(
  26.       length: 2,
  27.       initialIndex: 0,
  28.       child: Scaffold(
  29.         appBar: AppBar(
  30.           title: Text('bookmarks').tr(),
  31.           centerTitle: false,
  32.         ),
  33.         body: sb.guestUser
  34.             ? EmptyPage(
  35.                 icon: Feather.user_plus,
  36.                 message: 'sign in first'.tr(),
  37.                 message1: "sign in to save your favourite articles here".tr(),
  38.               )
  39.             : BookmarkedArticles(),
  40.       ),
  41.     );
  42.   }
  43.  
  44.   @override
  45.   bool get wantKeepAlive => true;
  46. }
  47.  
  48. class BookmarkedArticles extends StatefulWidget {
  49.   const BookmarkedArticles({Key? key}) : super(key: key);
  50.  
  51.   @override
  52.   _BookmarkedArticlesState createState() => _BookmarkedArticlesState();
  53. }
  54.  
  55. class _BookmarkedArticlesState extends State<BookmarkedArticles> {
  56.   @override
  57.   Widget build(BuildContext context) {
  58.     return Container(
  59.       child: FutureBuilder(
  60.         future: context.watch<BookmarkBloc>().getArticles(),
  61.         builder: (BuildContext context, AsyncSnapshot snapshot) {
  62.           print('sanpshot: ${snapshot.data}');
  63.  
  64.           switch (snapshot.connectionState) {
  65.             case ConnectionState.none:
  66.             case ConnectionState.active:
  67.             case ConnectionState.waiting:
  68.               return _LoadingWIdget();
  69.             case ConnectionState.done:
  70.             default:
  71.               if (snapshot.hasError || snapshot.data == null) {
  72.                 return EmptyPage(
  73.                   icon: Feather.bookmark,
  74.                   message: 'no articles found'.tr(),
  75.                   message1: 'save your favourite articles here'.tr(),
  76.                 );
  77.               } else if (snapshot.data.isEmpty) {
  78.                 return EmptyPage(
  79.                   icon: Feather.bookmark,
  80.                   message: 'no articles found'.tr(),
  81.                   message1: 'save your favourite articles here'.tr(),
  82.                 );
  83.               }
  84.  
  85.               return ListView.separated(
  86.                 padding: EdgeInsets.all(15),
  87.                 itemCount: snapshot.data.length,
  88.                 separatorBuilder: (context, index) => SizedBox(
  89.                   height: 15,
  90.                 ),
  91.                 itemBuilder: (BuildContext context, int index) {
  92.                   return Card4(
  93.                     d: snapshot.data[index],
  94.                     heroTag: 'bookmarks$index',
  95.                   );
  96.                 },
  97.               );
  98.           }
  99.         },
  100.       ),
  101.     );
  102.   }
  103. }
  104.  
  105. class _LoadingWIdget extends StatelessWidget {
  106.   const _LoadingWIdget({Key? key}) : super(key: key);
  107.  
  108.   @override
  109.   Widget build(BuildContext context) {
  110.     return ListView.separated(
  111.       padding: EdgeInsets.all(15),
  112.       itemCount: 8,
  113.       separatorBuilder: (BuildContext context, int index) => SizedBox(
  114.         height: 15,
  115.       ),
  116.       itemBuilder: (BuildContext context, int index) {
  117.         return LoadingCard(height: 160);
  118.       },
  119.     );
  120.   }
  121. }
Advertisement
RAW Paste Data Copied
Advertisement