|
| 1 | +import 'package:core/core.dart'; |
| 2 | +import 'package:data_repository/data_repository.dart'; |
| 3 | +import 'package:flutter/material.dart'; |
| 4 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 5 | +import 'package:flutter_news_app_mobile_client_full_source_code/account/bloc/account_bloc.dart'; |
| 6 | +import 'package:flutter_news_app_mobile_client_full_source_code/account/bloc/available_countries_bloc.dart'; |
| 7 | +import 'package:flutter_news_app_mobile_client_full_source_code/l10n/l10n.dart'; |
| 8 | +import 'package:ui_kit/ui_kit.dart'; |
| 9 | + |
| 10 | +/// {@template add_country_to_follow_page} |
| 11 | +/// A page that allows users to browse and select countries to follow. |
| 12 | +/// {@endtemplate} |
| 13 | +class AddCountryToFollowPage extends StatelessWidget { |
| 14 | + /// {@macro add_country_to_follow_page} |
| 15 | + const AddCountryToFollowPage({super.key}); |
| 16 | + |
| 17 | + @override |
| 18 | + Widget build(BuildContext context) { |
| 19 | + final l10n = AppLocalizationsX(context).l10n; |
| 20 | + final theme = Theme.of(context); |
| 21 | + final textTheme = theme.textTheme; |
| 22 | + |
| 23 | + return BlocProvider( |
| 24 | + create: (context) => AvailableCountriesBloc( |
| 25 | + countriesRepository: context.read<DataRepository<Country>>(), |
| 26 | + )..add(const FetchAvailableCountries()), |
| 27 | + child: Scaffold( |
| 28 | + appBar: AppBar( |
| 29 | + title: Text(l10n.addCountriesPageTitle, style: textTheme.titleLarge), |
| 30 | + ), |
| 31 | + body: BlocBuilder<AvailableCountriesBloc, AvailableCountriesState>( |
| 32 | + builder: (context, countriesState) { |
| 33 | + if (countriesState.status == AvailableCountriesStatus.loading) { |
| 34 | + return LoadingStateWidget( |
| 35 | + icon: Icons.flag_outlined, |
| 36 | + headline: l10n.countryFilterLoadingHeadline, |
| 37 | + subheadline: l10n.pleaseWait, |
| 38 | + ); |
| 39 | + } |
| 40 | + if (countriesState.status == AvailableCountriesStatus.failure) { |
| 41 | + return FailureStateWidget( |
| 42 | + exception: OperationFailedException( |
| 43 | + countriesState.error ?? l10n.countryFilterError, |
| 44 | + ), |
| 45 | + onRetry: () => context.read<AvailableCountriesBloc>().add( |
| 46 | + const FetchAvailableCountries(), |
| 47 | + ), |
| 48 | + ); |
| 49 | + } |
| 50 | + if (countriesState.availableCountries.isEmpty) { |
| 51 | + return InitialStateWidget( |
| 52 | + icon: Icons.search_off_outlined, |
| 53 | + headline: l10n.countryFilterEmptyHeadline, |
| 54 | + subheadline: l10n.countryFilterEmptySubheadline, |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + final countries = countriesState.availableCountries; |
| 59 | + |
| 60 | + return BlocBuilder<AccountBloc, AccountState>( |
| 61 | + buildWhen: (previous, current) => |
| 62 | + previous.preferences?.followedCountries != |
| 63 | + current.preferences?.followedCountries || |
| 64 | + previous.status != current.status, |
| 65 | + builder: (context, accountState) { |
| 66 | + final followedCountries = |
| 67 | + accountState.preferences?.followedCountries ?? []; |
| 68 | + |
| 69 | + return ListView.builder( |
| 70 | + padding: const EdgeInsets.symmetric( |
| 71 | + horizontal: AppSpacing.paddingMedium, |
| 72 | + vertical: AppSpacing.paddingSmall, |
| 73 | + ).copyWith(bottom: AppSpacing.xxl), |
| 74 | + itemCount: countries.length, |
| 75 | + itemBuilder: (context, index) { |
| 76 | + final country = countries[index]; |
| 77 | + final isFollowed = followedCountries.any( |
| 78 | + (fc) => fc.id == country.id, |
| 79 | + ); |
| 80 | + final colorScheme = Theme.of(context).colorScheme; |
| 81 | + |
| 82 | + return Card( |
| 83 | + margin: const EdgeInsets.only(bottom: AppSpacing.sm), |
| 84 | + elevation: 0.5, |
| 85 | + shape: RoundedRectangleBorder( |
| 86 | + borderRadius: BorderRadius.circular(AppSpacing.sm), |
| 87 | + side: BorderSide( |
| 88 | + color: colorScheme.outlineVariant.withOpacity(0.3), |
| 89 | + ), |
| 90 | + ), |
| 91 | + child: ListTile( |
| 92 | + leading: SizedBox( |
| 93 | + width: AppSpacing.xl + AppSpacing.xs, |
| 94 | + height: AppSpacing.xl + AppSpacing.xs, |
| 95 | + child: |
| 96 | + Uri.tryParse(country.flagUrl)?.isAbsolute == true |
| 97 | + ? ClipRRect( |
| 98 | + borderRadius: BorderRadius.circular( |
| 99 | + AppSpacing.xs, |
| 100 | + ), |
| 101 | + child: Image.network( |
| 102 | + country.flagUrl, |
| 103 | + fit: BoxFit.contain, |
| 104 | + errorBuilder: |
| 105 | + (context, error, stackTrace) => Icon( |
| 106 | + Icons.flag_outlined, |
| 107 | + color: colorScheme.onSurfaceVariant, |
| 108 | + size: AppSpacing.lg, |
| 109 | + ), |
| 110 | + loadingBuilder: |
| 111 | + (context, child, loadingProgress) { |
| 112 | + if (loadingProgress == null) { |
| 113 | + return child; |
| 114 | + } |
| 115 | + return Center( |
| 116 | + child: CircularProgressIndicator( |
| 117 | + strokeWidth: 2, |
| 118 | + value: |
| 119 | + loadingProgress |
| 120 | + .expectedTotalBytes != |
| 121 | + null |
| 122 | + ? loadingProgress |
| 123 | + .cumulativeBytesLoaded / |
| 124 | + loadingProgress |
| 125 | + .expectedTotalBytes! |
| 126 | + : null, |
| 127 | + ), |
| 128 | + ); |
| 129 | + }, |
| 130 | + ), |
| 131 | + ) |
| 132 | + : Icon( |
| 133 | + Icons.flag_outlined, |
| 134 | + color: colorScheme.onSurfaceVariant, |
| 135 | + size: AppSpacing.lg, |
| 136 | + ), |
| 137 | + ), |
| 138 | + title: Text(country.name, style: textTheme.titleMedium), |
| 139 | + trailing: IconButton( |
| 140 | + icon: isFollowed |
| 141 | + ? Icon( |
| 142 | + Icons.check_circle, |
| 143 | + color: colorScheme.primary, |
| 144 | + ) |
| 145 | + : Icon( |
| 146 | + Icons.add_circle_outline, |
| 147 | + color: colorScheme.onSurfaceVariant, |
| 148 | + ), |
| 149 | + tooltip: isFollowed |
| 150 | + ? l10n.unfollowCountryTooltip(country.name) |
| 151 | + : l10n.followCountryTooltip(country.name), |
| 152 | + onPressed: () { |
| 153 | + context.read<AccountBloc>().add( |
| 154 | + AccountFollowCountryToggled(country: country), |
| 155 | + ); |
| 156 | + }, |
| 157 | + ), |
| 158 | + contentPadding: const EdgeInsets.symmetric( |
| 159 | + horizontal: AppSpacing.paddingMedium, |
| 160 | + vertical: AppSpacing.xs, |
| 161 | + ), |
| 162 | + ), |
| 163 | + ); |
| 164 | + }, |
| 165 | + ); |
| 166 | + }, |
| 167 | + ); |
| 168 | + }, |
| 169 | + ), |
| 170 | + ), |
| 171 | + ); |
| 172 | + } |
| 173 | +} |
0 commit comments