|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 3 | +import 'package:ht_headlines_repository/ht_headlines_repository.dart'; |
| 4 | +import 'package:ht_main/headline-details/view/bloc/headline_details_bloc.dart'; |
| 5 | +import 'package:ht_main/shared/widgets/failure_state_widget.dart'; |
| 6 | +import 'package:ht_main/shared/widgets/initial_state_widget.dart'; |
| 7 | +import 'package:ht_main/shared/widgets/loading_state_widget.dart'; |
| 8 | +import 'package:url_launcher/url_launcher_string.dart'; |
| 9 | +import 'package:intl/intl.dart'; |
| 10 | + |
| 11 | +class HeadlineDetailsPage extends StatelessWidget { |
| 12 | + const HeadlineDetailsPage({required this.headlineId, super.key}); |
| 13 | + |
| 14 | + final String headlineId; |
| 15 | + |
| 16 | + static Route<void> route({required String headlineId}) { |
| 17 | + return MaterialPageRoute<void>( |
| 18 | + builder: (_) => HeadlineDetailsPage(headlineId: headlineId), |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + @override |
| 23 | + Widget build(BuildContext context) { |
| 24 | + return BlocProvider( |
| 25 | + create: (context) => HeadlineDetailsBloc( |
| 26 | + headlinesRepository: context.read<HtHeadlinesRepository>(), |
| 27 | + )..add(HeadlineDetailsRequested(headlineId: headlineId)), |
| 28 | + child: const _HeadlineDetailsView(), |
| 29 | + ); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class _HeadlineDetailsView extends StatelessWidget { |
| 34 | + const _HeadlineDetailsView(); |
| 35 | + |
| 36 | + @override |
| 37 | + Widget build(BuildContext context) { |
| 38 | + return Scaffold( |
| 39 | + appBar: AppBar( |
| 40 | + title: const Text('Headline Details'), |
| 41 | + leading: IconButton( |
| 42 | + icon: const Icon(Icons.arrow_back), |
| 43 | + onPressed: () => Navigator.of(context).pop(), |
| 44 | + ), |
| 45 | + actions: [ |
| 46 | + IconButton( |
| 47 | + icon: const Icon(Icons.bookmark_border), |
| 48 | + onPressed: () {}, // Placeholder |
| 49 | + ), |
| 50 | + IconButton( |
| 51 | + icon: const Icon(Icons.share), |
| 52 | + onPressed: () {}, // Placeholder |
| 53 | + ), |
| 54 | + ], |
| 55 | + ), |
| 56 | + body: BlocBuilder<HeadlineDetailsBloc, HeadlineDetailsState>( |
| 57 | + builder: (context, state) { |
| 58 | + return switch (state) { |
| 59 | + HeadlineDetailsInitial _ => const InitialStateWidget( |
| 60 | + icon: Icons.article, |
| 61 | + headline: 'Waiting for Headline', |
| 62 | + subheadline: 'Please wait...', |
| 63 | + ), |
| 64 | + HeadlineDetailsLoading _ => const LoadingStateWidget( |
| 65 | + icon: Icons.downloading, |
| 66 | + headline: 'Loading Headline', |
| 67 | + subheadline: 'Fetching data...', |
| 68 | + ), |
| 69 | + final HeadlineDetailsFailure state => FailureStateWidget( |
| 70 | + message: state.message, |
| 71 | + onRetry: () { |
| 72 | + context |
| 73 | + .read<HeadlineDetailsBloc>() |
| 74 | + .add(HeadlineDetailsRequested(headlineId: '1')); |
| 75 | + }, |
| 76 | + ), |
| 77 | + final HeadlineDetailsLoaded state => |
| 78 | + _buildLoaded(context, state.headline), |
| 79 | + _ => const SizedBox.shrink(), |
| 80 | + }; |
| 81 | + }, |
| 82 | + ), |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + Widget _buildLoaded(BuildContext context, Headline headline) { |
| 87 | + return SingleChildScrollView( |
| 88 | + child: Padding( |
| 89 | + padding: const EdgeInsets.all(16), |
| 90 | + child: Column( |
| 91 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 92 | + children: [ |
| 93 | + if (headline.imageUrl != null) |
| 94 | + Image.network( |
| 95 | + headline.imageUrl!, |
| 96 | + width: double.infinity, |
| 97 | + height: 200, |
| 98 | + fit: BoxFit.cover, |
| 99 | + loadingBuilder: (context, child, loadingProgress) { |
| 100 | + if (loadingProgress == null) return child; |
| 101 | + return Container( |
| 102 | + width: double.infinity, |
| 103 | + height: 200, |
| 104 | + color: Colors.grey[300], |
| 105 | + ); |
| 106 | + }, |
| 107 | + errorBuilder: (context, error, stackTrace) => |
| 108 | + const Icon(Icons.error), |
| 109 | + ), |
| 110 | + const SizedBox(height: 16), // Keep this |
| 111 | + Text( |
| 112 | + headline.title, |
| 113 | + style: Theme.of(context).textTheme.titleLarge, |
| 114 | + ), |
| 115 | + const SizedBox(height: 8), |
| 116 | + Column( |
| 117 | + children: [ |
| 118 | + if (headline.source != null) ...[ |
| 119 | + Row( |
| 120 | + children: [ |
| 121 | + const Icon(Icons.source), |
| 122 | + const SizedBox(width: 4), |
| 123 | + Text( |
| 124 | + headline.source!, |
| 125 | + style: Theme.of(context).textTheme.bodyMedium, |
| 126 | + ), |
| 127 | + ], |
| 128 | + ), |
| 129 | + const SizedBox( |
| 130 | + height: 8), // Add spacing between metadata items |
| 131 | + ], |
| 132 | + if (headline.categories != null && |
| 133 | + headline.categories!.isNotEmpty) ...[ |
| 134 | + Row( |
| 135 | + children: [ |
| 136 | + const Icon(Icons.category), |
| 137 | + const SizedBox(width: 4), |
| 138 | + Text( |
| 139 | + headline.categories!.join(', '), |
| 140 | + style: Theme.of(context).textTheme.bodyMedium, |
| 141 | + ), |
| 142 | + ], |
| 143 | + ), |
| 144 | + const SizedBox(height: 8), |
| 145 | + ], |
| 146 | + if (headline.eventCountry != null) ...[ |
| 147 | + Row( |
| 148 | + children: [ |
| 149 | + const Icon(Icons.location_on), |
| 150 | + const SizedBox(width: 4), |
| 151 | + Text( |
| 152 | + headline.eventCountry!, |
| 153 | + style: Theme.of(context).textTheme.bodyMedium, |
| 154 | + ), |
| 155 | + ], |
| 156 | + ), |
| 157 | + const SizedBox(height: 8), |
| 158 | + ], |
| 159 | + if (headline.publishedAt != null) |
| 160 | + Row( |
| 161 | + children: [ |
| 162 | + const Icon(Icons.date_range), |
| 163 | + const SizedBox(width: 4), |
| 164 | + Text( |
| 165 | + DateFormat('MMMM dd, yyyy') |
| 166 | + .format(headline.publishedAt!), |
| 167 | + style: Theme.of(context).textTheme.bodyMedium, |
| 168 | + ), |
| 169 | + ], |
| 170 | + ), |
| 171 | + ], |
| 172 | + ), |
| 173 | + const SizedBox(height: 16), |
| 174 | + if (headline.description != null) |
| 175 | + Text( |
| 176 | + headline.description!, |
| 177 | + style: Theme.of(context).textTheme.bodyLarge, |
| 178 | + ), |
| 179 | + const SizedBox(height: 16), |
| 180 | + ElevatedButton( |
| 181 | + onPressed: () async { |
| 182 | + if (headline.url != null) { |
| 183 | + await launchUrlString(headline.url!); |
| 184 | + } |
| 185 | + }, |
| 186 | + style: ElevatedButton.styleFrom( |
| 187 | + backgroundColor: Theme.of(context).colorScheme.primary, |
| 188 | + // Removed custom padding |
| 189 | + ), |
| 190 | + child: Text( |
| 191 | + 'Continue Reading', |
| 192 | + style: Theme.of(context).textTheme.labelLarge, |
| 193 | + ), |
| 194 | + ), |
| 195 | + ], |
| 196 | + ), |
| 197 | + ), |
| 198 | + ); |
| 199 | + } |
| 200 | +} |
0 commit comments