add little bit of localization, add SharedPreferences for likes, add change locale button
This commit is contained in:
parent
9cb990d7ac
commit
011fbbd05f
@ -42,4 +42,5 @@
|
|||||||
<data android:mimeType="text/plain"/>
|
<data android:mimeType="text/plain"/>
|
||||||
</intent>
|
</intent>
|
||||||
</queries>
|
</queries>
|
||||||
|
<uses-permission android:name="android.permission.INTERNET"/>
|
||||||
</manifest>
|
</manifest>
|
||||||
|
@ -1,12 +1,21 @@
|
|||||||
{
|
{
|
||||||
"@@locale": "en",
|
"@@locale": "en",
|
||||||
|
|
||||||
|
"appBarTitle": "Anime List",
|
||||||
|
|
||||||
"search": "Search",
|
"search": "Search",
|
||||||
"liked": "You liked",
|
"liked": "You liked",
|
||||||
"unliked": "Like removed from",
|
"unliked": "Like removed from",
|
||||||
"errorOccured": "Error occured",
|
"errorOccured": "Error occured",
|
||||||
"noErrorMsg": "No message provided",
|
"noErrorMsg": "No message provided",
|
||||||
"retry": "Retry",
|
"retry": "Retry",
|
||||||
|
"unknown": "Unknown",
|
||||||
|
|
||||||
|
"apiYear": "Year",
|
||||||
|
"apiType": "Type",
|
||||||
|
"apiRating": "Rating",
|
||||||
|
"apiDesc": "",
|
||||||
|
"apiNoDesc": "No description provided",
|
||||||
|
|
||||||
"arbEnding": "t"
|
"arbEnding": "t"
|
||||||
}
|
}
|
@ -1,12 +1,21 @@
|
|||||||
{
|
{
|
||||||
"@@locale": "ru",
|
"@@locale": "ru",
|
||||||
|
|
||||||
|
"appBarTitle": "Список аниме",
|
||||||
|
|
||||||
"search": "Поиск",
|
"search": "Поиск",
|
||||||
"liked": "Вы лайкнули",
|
"liked": "Вы лайкнули",
|
||||||
"unliked": "Лайк снят с",
|
"unliked": "Лайк снят с",
|
||||||
"errorOccured": "Произошла ошибка",
|
"errorOccured": "Произошла ошибка",
|
||||||
"noErrorMsg": "Нет сообщения",
|
"noErrorMsg": "Нет сообщения",
|
||||||
"retry": "Повторить",
|
"retry": "Повторить",
|
||||||
|
"unknown": "Неизвестно",
|
||||||
|
|
||||||
|
"apiYear": "Год",
|
||||||
|
"apiType": "Тип",
|
||||||
|
"apiRating": "Рейтинг",
|
||||||
|
"apiDesc": "(Описание доступно только на английском языке)",
|
||||||
|
"apiNoDesc": "Нет описания",
|
||||||
|
|
||||||
"arbEnding": "t"
|
"arbEnding": "t"
|
||||||
}
|
}
|
@ -95,6 +95,12 @@ abstract class AppLocale {
|
|||||||
Locale('ru')
|
Locale('ru')
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/// No description provided for @appBarTitle.
|
||||||
|
///
|
||||||
|
/// In ru, this message translates to:
|
||||||
|
/// **'Список аниме'**
|
||||||
|
String get appBarTitle;
|
||||||
|
|
||||||
/// No description provided for @search.
|
/// No description provided for @search.
|
||||||
///
|
///
|
||||||
/// In ru, this message translates to:
|
/// In ru, this message translates to:
|
||||||
@ -131,6 +137,42 @@ abstract class AppLocale {
|
|||||||
/// **'Повторить'**
|
/// **'Повторить'**
|
||||||
String get retry;
|
String get retry;
|
||||||
|
|
||||||
|
/// No description provided for @unknown.
|
||||||
|
///
|
||||||
|
/// In ru, this message translates to:
|
||||||
|
/// **'Неизвестно'**
|
||||||
|
String get unknown;
|
||||||
|
|
||||||
|
/// No description provided for @apiYear.
|
||||||
|
///
|
||||||
|
/// In ru, this message translates to:
|
||||||
|
/// **'Год'**
|
||||||
|
String get apiYear;
|
||||||
|
|
||||||
|
/// No description provided for @apiType.
|
||||||
|
///
|
||||||
|
/// In ru, this message translates to:
|
||||||
|
/// **'Тип'**
|
||||||
|
String get apiType;
|
||||||
|
|
||||||
|
/// No description provided for @apiRating.
|
||||||
|
///
|
||||||
|
/// In ru, this message translates to:
|
||||||
|
/// **'Рейтинг'**
|
||||||
|
String get apiRating;
|
||||||
|
|
||||||
|
/// No description provided for @apiDesc.
|
||||||
|
///
|
||||||
|
/// In ru, this message translates to:
|
||||||
|
/// **'(Описание доступно только на английском языке)'**
|
||||||
|
String get apiDesc;
|
||||||
|
|
||||||
|
/// No description provided for @apiNoDesc.
|
||||||
|
///
|
||||||
|
/// In ru, this message translates to:
|
||||||
|
/// **'Нет описания'**
|
||||||
|
String get apiNoDesc;
|
||||||
|
|
||||||
/// No description provided for @arbEnding.
|
/// No description provided for @arbEnding.
|
||||||
///
|
///
|
||||||
/// In ru, this message translates to:
|
/// In ru, this message translates to:
|
||||||
|
@ -6,6 +6,9 @@ import 'app_localizations.dart';
|
|||||||
class AppLocaleEn extends AppLocale {
|
class AppLocaleEn extends AppLocale {
|
||||||
AppLocaleEn([String locale = 'en']) : super(locale);
|
AppLocaleEn([String locale = 'en']) : super(locale);
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get appBarTitle => 'Anime List';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get search => 'Search';
|
String get search => 'Search';
|
||||||
|
|
||||||
@ -24,6 +27,24 @@ class AppLocaleEn extends AppLocale {
|
|||||||
@override
|
@override
|
||||||
String get retry => 'Retry';
|
String get retry => 'Retry';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get unknown => 'Unknown';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get apiYear => 'Year';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get apiType => 'Type';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get apiRating => 'Rating';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get apiDesc => '';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get apiNoDesc => 'No description provided';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get arbEnding => 't';
|
String get arbEnding => 't';
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,9 @@ import 'app_localizations.dart';
|
|||||||
class AppLocaleRu extends AppLocale {
|
class AppLocaleRu extends AppLocale {
|
||||||
AppLocaleRu([String locale = 'ru']) : super(locale);
|
AppLocaleRu([String locale = 'ru']) : super(locale);
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get appBarTitle => 'Список аниме';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get search => 'Поиск';
|
String get search => 'Поиск';
|
||||||
|
|
||||||
@ -24,6 +27,24 @@ class AppLocaleRu extends AppLocale {
|
|||||||
@override
|
@override
|
||||||
String get retry => 'Повторить';
|
String get retry => 'Повторить';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get unknown => 'Неизвестно';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get apiYear => 'Год';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get apiType => 'Тип';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get apiRating => 'Рейтинг';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get apiDesc => '(Описание доступно только на английском языке)';
|
||||||
|
|
||||||
|
@override
|
||||||
|
String get apiNoDesc => 'Нет описания';
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String get arbEnding => 't';
|
String get arbEnding => 't';
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,8 @@ class AnimesDto {
|
|||||||
final PaginationDto? pagination;
|
final PaginationDto? pagination;
|
||||||
const AnimesDto({this.data, this.pagination});
|
const AnimesDto({this.data, this.pagination});
|
||||||
|
|
||||||
factory AnimesDto.fromJson(Map<String, dynamic> json) => _$AnimesDtoFromJson(json);
|
factory AnimesDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$AnimesDtoFromJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
@ -22,11 +23,14 @@ class PaginationDto {
|
|||||||
|
|
||||||
const PaginationDto({this.currentPage, this.hasNextPage, this.lastPage});
|
const PaginationDto({this.currentPage, this.hasNextPage, this.lastPage});
|
||||||
|
|
||||||
factory PaginationDto.fromJson(Map<String, dynamic> json) => _$PaginationDtoFromJson(json);
|
factory PaginationDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$PaginationDtoFromJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class AnimeDto {
|
class AnimeDto {
|
||||||
|
@JsonKey(name: "mal_id")
|
||||||
|
final int? id;
|
||||||
final String? title;
|
final String? title;
|
||||||
final int? year;
|
final int? year;
|
||||||
final String? type;
|
final String? type;
|
||||||
@ -34,9 +38,17 @@ class AnimeDto {
|
|||||||
final String? rating;
|
final String? rating;
|
||||||
final ImagesDto? images;
|
final ImagesDto? images;
|
||||||
|
|
||||||
const AnimeDto({this.title, this.rating, this.synopsis, this.type, this.year, this.images});
|
const AnimeDto(
|
||||||
|
{this.id,
|
||||||
|
this.title,
|
||||||
|
this.rating,
|
||||||
|
this.synopsis,
|
||||||
|
this.type,
|
||||||
|
this.year,
|
||||||
|
this.images});
|
||||||
|
|
||||||
factory AnimeDto.fromJson(Map<String, dynamic> json) => _$AnimeDtoFromJson(json);
|
factory AnimeDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$AnimeDtoFromJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
@ -45,7 +57,8 @@ class ImagesDto {
|
|||||||
|
|
||||||
const ImagesDto({this.jpg});
|
const ImagesDto({this.jpg});
|
||||||
|
|
||||||
factory ImagesDto.fromJson(Map<String, dynamic> json) => _$ImagesDtoFromJson(json);
|
factory ImagesDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$ImagesDtoFromJson(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
@ -55,5 +68,6 @@ class ImageDto {
|
|||||||
|
|
||||||
const ImageDto({this.imageUrl});
|
const ImageDto({this.imageUrl});
|
||||||
|
|
||||||
factory ImageDto.fromJson(Map<String, dynamic> json) => _$ImageDtoFromJson(json);
|
factory ImageDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$ImageDtoFromJson(json);
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,7 @@ PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
|
|||||||
);
|
);
|
||||||
|
|
||||||
AnimeDto _$AnimeDtoFromJson(Map<String, dynamic> json) => AnimeDto(
|
AnimeDto _$AnimeDtoFromJson(Map<String, dynamic> json) => AnimeDto(
|
||||||
|
id: (json['mal_id'] as num?)?.toInt(),
|
||||||
title: json['title'] as String?,
|
title: json['title'] as String?,
|
||||||
rating: json['rating'] as String?,
|
rating: json['rating'] as String?,
|
||||||
synopsis: json['synopsis'] as String?,
|
synopsis: json['synopsis'] as String?,
|
||||||
|
@ -6,15 +6,18 @@ import '../dtos/animes_dto.dart';
|
|||||||
extension AnimesMapper on AnimesDto {
|
extension AnimesMapper on AnimesDto {
|
||||||
HomeData toDomain() => HomeData(
|
HomeData toDomain() => HomeData(
|
||||||
data: data?.map((dto) => dto.toDomain()).toList(),
|
data: data?.map((dto) => dto.toDomain()).toList(),
|
||||||
nextPage: (pagination?.hasNextPage ?? false) ? ((pagination?.currentPage ?? 0) + 1) : null);
|
nextPage: (pagination?.hasNextPage ?? false)
|
||||||
|
? ((pagination?.currentPage ?? 0) + 1)
|
||||||
|
: null);
|
||||||
}
|
}
|
||||||
|
|
||||||
extension AnimeMapper on AnimeDto {
|
extension AnimeMapper on AnimeDto {
|
||||||
CardData toDomain() => CardData(
|
CardData toDomain() => CardData(
|
||||||
|
id: id.toString(),
|
||||||
name: title ?? "",
|
name: title ?? "",
|
||||||
imageUrl: images?.jpg?.imageUrl ?? "placeholder.co/250",
|
imageUrl: images?.jpg?.imageUrl ?? "placeholder.co/250",
|
||||||
descr:
|
type: type,
|
||||||
"Rating: ${rating ?? "unknown"}\nYear: ${year ?? "unknown"}\nType: ${type ?? "unknown"}.\n\n${synopsis ?? "No description provided"} ",
|
year: year,
|
||||||
cuttedDescr:
|
descr: synopsis,
|
||||||
"Rating: ${rating ?? "unknown"}\nYear: ${year ?? "unknown"}\nType: ${type ?? "unknown"}");
|
rating: rating);
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
import 'dart:developer';
|
import 'dart:developer';
|
||||||
|
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:flutter_project/components/utils/error_callback.dart';
|
|
||||||
import 'package:flutter_project/data/mappers/animes_mapper.dart';
|
import 'package:flutter_project/data/mappers/animes_mapper.dart';
|
||||||
import 'package:flutter_project/data/repositories/api_interface.dart';
|
import 'package:flutter_project/data/repositories/api_interface.dart';
|
||||||
|
|
||||||
|
import '../../components/utils/error_callback.dart';
|
||||||
import '../../domain/models/home.dart';
|
import '../../domain/models/home.dart';
|
||||||
import '../dtos/animes_dto.dart';
|
import '../dtos/animes_dto.dart';
|
||||||
|
|
||||||
@ -15,14 +15,22 @@ class AnimeRepository extends ApiInterface {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Future<HomeData?> loadData(
|
Future<HomeData?> loadData(
|
||||||
{OnErrorCallback onError, String? q, int page = 1, int pageSize = 25}) async {
|
{OnErrorCallback onError,
|
||||||
|
String? q,
|
||||||
|
int page = 1,
|
||||||
|
int pageSize = 25}) async {
|
||||||
try {
|
try {
|
||||||
const String url = "$_baseUrl/v4/anime?sfw";
|
const String url = "$_baseUrl/v4/anime?sfw";
|
||||||
|
|
||||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url,
|
final Response<dynamic> response = await _dio
|
||||||
queryParameters: {'q': q, 'page': page, 'limit': !(pageSize > 25) ? pageSize : 25});
|
.get<Map<dynamic, dynamic>>(url, queryParameters: {
|
||||||
|
'q': q,
|
||||||
|
'page': page,
|
||||||
|
'limit': !(pageSize > 25) ? pageSize : 25
|
||||||
|
});
|
||||||
|
|
||||||
final AnimesDto dto = AnimesDto.fromJson(response.data as Map<String, dynamic>);
|
final AnimesDto dto =
|
||||||
|
AnimesDto.fromJson(response.data as Map<String, dynamic>);
|
||||||
|
|
||||||
final HomeData data = dto.toDomain();
|
final HomeData data = dto.toDomain();
|
||||||
|
|
||||||
|
@ -12,21 +12,24 @@ class MockRepository extends ApiInterface {
|
|||||||
CardData(
|
CardData(
|
||||||
name: "Test",
|
name: "Test",
|
||||||
imageUrl: "https://loremflickr.com/250/150/cat",
|
imageUrl: "https://loremflickr.com/250/150/cat",
|
||||||
descr: "Description",
|
descr: "descr",
|
||||||
cuttedDescr: "cutted",
|
type: "Type",
|
||||||
),
|
year: 2024,
|
||||||
|
rating: "R"),
|
||||||
CardData(
|
CardData(
|
||||||
name: "Test 2",
|
name: "Test 2",
|
||||||
imageUrl: "https://loremflickr.com/200/250/cat",
|
imageUrl: "https://loremflickr.com/200/250/cat",
|
||||||
descr: "Description",
|
descr: "descr",
|
||||||
cuttedDescr: "cutted",
|
type: "Type",
|
||||||
),
|
year: 2024,
|
||||||
|
rating: "R"),
|
||||||
CardData(
|
CardData(
|
||||||
name: "Test 3",
|
name: "Test 3",
|
||||||
imageUrl: "https://loremflickr.com/200/200/cat",
|
imageUrl: "https://loremflickr.com/200/200/cat",
|
||||||
descr: "Description",
|
descr: "descr",
|
||||||
cuttedDescr: "cutted",
|
type: "Type",
|
||||||
),
|
year: 2024,
|
||||||
|
rating: "R"),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,18 @@
|
|||||||
class CardData {
|
class CardData {
|
||||||
final String name;
|
final String name;
|
||||||
final String imageUrl;
|
final String imageUrl;
|
||||||
final String descr;
|
final String? type;
|
||||||
final String cuttedDescr;
|
final int? year;
|
||||||
|
final String? rating;
|
||||||
|
final String? descr;
|
||||||
|
final String? id;
|
||||||
|
|
||||||
const CardData(
|
const CardData(
|
||||||
{required this.name, required this.imageUrl, required this.descr, required this.cuttedDescr});
|
{required this.name,
|
||||||
|
required this.imageUrl,
|
||||||
|
required this.type,
|
||||||
|
required this.year,
|
||||||
|
required this.rating,
|
||||||
|
required this.descr,
|
||||||
|
this.id});
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flutter_project/components/locale/l10n/app_localizations.dart';
|
import 'package:flutter_project/components/locale/l10n/app_localizations.dart';
|
||||||
import 'package:flutter_project/data/repositories/anime_repository.dart';
|
import 'package:flutter_project/data/repositories/anime_repository.dart';
|
||||||
import 'package:flutter_project/views/home_page/bloc/bloc.dart';
|
import 'package:flutter_project/views/home_page/home_bloc/bloc.dart';
|
||||||
import 'package:flutter_project/views/home_page/home_page.dart';
|
import 'package:flutter_project/views/home_page/home_page.dart';
|
||||||
|
import 'package:flutter_project/views/like_bloc/like_bloc.dart';
|
||||||
|
import 'package:flutter_project/views/locale_bloc/locale_bloc.dart';
|
||||||
|
import 'package:flutter_project/views/locale_bloc/locale_state.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
@ -14,21 +19,31 @@ class MyApp extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return RepositoryProvider<AnimeRepository>(
|
return BlocProvider<LikeBloc>(
|
||||||
lazy: true,
|
|
||||||
create: (_) => AnimeRepository(),
|
|
||||||
child: BlocProvider<HomeBloc>(
|
|
||||||
lazy: false,
|
lazy: false,
|
||||||
create: (context) => HomeBloc(context.read<AnimeRepository>()),
|
create: (context) => LikeBloc(),
|
||||||
child: MaterialApp(
|
child: BlocProvider<LocaleBloc>(
|
||||||
|
lazy: false,
|
||||||
|
create: (context) => LocaleBloc(Locale(Platform.localeName)),
|
||||||
|
child: BlocBuilder<LocaleBloc, LocaleState>(
|
||||||
|
builder: (context, state) => MaterialApp(
|
||||||
title: 'Anime list',
|
title: 'Anime list',
|
||||||
|
locale: state.currentLocale,
|
||||||
localizationsDelegates: AppLocale.localizationsDelegates,
|
localizationsDelegates: AppLocale.localizationsDelegates,
|
||||||
supportedLocales: AppLocale.supportedLocales,
|
supportedLocales: AppLocale.supportedLocales,
|
||||||
theme: ThemeData(
|
theme: ThemeData(
|
||||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.lightGreen),
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.lightGreen),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
home: const HomePage(),
|
home: RepositoryProvider<AnimeRepository>(
|
||||||
|
lazy: true,
|
||||||
|
create: (_) => AnimeRepository(),
|
||||||
|
child: BlocProvider<HomeBloc>(
|
||||||
|
lazy: false,
|
||||||
|
create: (context) =>
|
||||||
|
HomeBloc(context.read<AnimeRepository>()),
|
||||||
|
child: const HomePage())),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_project/components/locale/l10n/app_localizations.dart';
|
||||||
|
|
||||||
import '../../domain/models/card.dart';
|
import '../../domain/models/card.dart';
|
||||||
|
|
||||||
class DetailsPage extends StatelessWidget {
|
class DetailsPage extends StatelessWidget {
|
||||||
|
final AppLocale locale;
|
||||||
final CardData data;
|
final CardData data;
|
||||||
|
|
||||||
const DetailsPage(this.data, {super.key});
|
const DetailsPage(this.locale, this.data, {super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -38,7 +40,7 @@ class DetailsPage extends StatelessWidget {
|
|||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.all(10),
|
padding: const EdgeInsets.all(10),
|
||||||
child: Text(
|
child: Text(
|
||||||
data.descr,
|
'${locale.apiYear}: ${data.year ?? locale.unknown}, ${locale.apiType}: ${data.type ?? locale.unknown}, ${locale.apiRating}: ${data.rating ?? locale.unknown}\n${locale.apiDesc != "" ? "\n${locale.apiDesc}\n" : ""}\n${data.descr ?? locale.apiNoDesc}',
|
||||||
style: Theme.of(context).textTheme.bodyLarge,
|
style: Theme.of(context).textTheme.bodyLarge,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -1,36 +1,49 @@
|
|||||||
part of 'home_page.dart';
|
part of 'home_page.dart';
|
||||||
|
|
||||||
typedef onLikeCallback = void Function(String title, bool isLiked)?;
|
typedef onLikeCallback = void Function(String? id, String title, bool isLiked)?;
|
||||||
|
|
||||||
class _Card extends StatefulWidget {
|
class _Card extends StatelessWidget {
|
||||||
|
final AppLocale locale;
|
||||||
final String name;
|
final String name;
|
||||||
final String imageUrl;
|
final String imageUrl;
|
||||||
final String descr;
|
final String? type;
|
||||||
|
final int? year;
|
||||||
|
final String? rating;
|
||||||
|
final String? id;
|
||||||
final onLikeCallback onLike;
|
final onLikeCallback onLike;
|
||||||
final VoidCallback? onTap;
|
final VoidCallback? onTap;
|
||||||
|
final bool isLiked;
|
||||||
|
|
||||||
const _Card(
|
const _Card(
|
||||||
{required this.name, required this.imageUrl, this.onLike, this.onTap, required this.descr});
|
{required this.locale,
|
||||||
|
required this.name,
|
||||||
|
required this.imageUrl,
|
||||||
|
required this.type,
|
||||||
|
required this.year,
|
||||||
|
required this.rating,
|
||||||
|
this.id,
|
||||||
|
this.onLike,
|
||||||
|
this.onTap,
|
||||||
|
this.isLiked = false});
|
||||||
|
|
||||||
factory _Card.withData(CardData d, {onLikeCallback onLike, VoidCallback? onTap}) => _Card(
|
factory _Card.withData(AppLocale locale, CardData d,
|
||||||
|
{onLikeCallback onLike, VoidCallback? onTap, bool isLiked = false}) =>
|
||||||
|
_Card(
|
||||||
|
locale: locale,
|
||||||
name: d.name,
|
name: d.name,
|
||||||
imageUrl: d.imageUrl,
|
imageUrl: d.imageUrl,
|
||||||
onLike: onLike,
|
onLike: onLike,
|
||||||
onTap: onTap,
|
onTap: onTap,
|
||||||
descr: d.cuttedDescr,
|
type: d.type,
|
||||||
);
|
year: d.year,
|
||||||
|
rating: d.rating,
|
||||||
@override
|
isLiked: isLiked,
|
||||||
State<_Card> createState() => _CardState();
|
id: d.id);
|
||||||
}
|
|
||||||
|
|
||||||
class _CardState extends State<_Card> {
|
|
||||||
bool isLiked = false;
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return GestureDetector(
|
return GestureDetector(
|
||||||
onTap: widget.onTap,
|
onTap: onTap,
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
child: Container(
|
child: Container(
|
||||||
@ -51,14 +64,15 @@ class _CardState extends State<_Card> {
|
|||||||
children: [
|
children: [
|
||||||
ClipRRect(
|
ClipRRect(
|
||||||
borderRadius: BorderRadius.only(
|
borderRadius: BorderRadius.only(
|
||||||
topLeft: Radius.circular(20), bottomLeft: Radius.circular(20)),
|
topLeft: Radius.circular(20),
|
||||||
|
bottomLeft: Radius.circular(20)),
|
||||||
child: ConstrainedBox(
|
child: ConstrainedBox(
|
||||||
constraints: BoxConstraints(minHeight: 200),
|
constraints: BoxConstraints(minHeight: 200),
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
width: 150,
|
width: 150,
|
||||||
height: double.infinity,
|
height: double.infinity,
|
||||||
child: Image.network(
|
child: Image.network(
|
||||||
widget.imageUrl,
|
imageUrl,
|
||||||
fit: BoxFit.cover,
|
fit: BoxFit.cover,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -71,11 +85,11 @@ class _CardState extends State<_Card> {
|
|||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
widget.name,
|
name,
|
||||||
style: Theme.of(context).textTheme.headlineSmall,
|
style: Theme.of(context).textTheme.headlineSmall,
|
||||||
),
|
),
|
||||||
Text(
|
Text(
|
||||||
widget.descr,
|
'${locale.apiYear}: ${year ?? locale.unknown}, ${locale.apiType}: ${type ?? locale.unknown}, ${locale.apiRating}: ${rating ?? locale.unknown}',
|
||||||
style: Theme.of(context).textTheme.labelSmall,
|
style: Theme.of(context).textTheme.labelSmall,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@ -87,12 +101,7 @@ class _CardState extends State<_Card> {
|
|||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.only(top: 16.0, right: 8.0),
|
padding: const EdgeInsets.only(top: 16.0, right: 8.0),
|
||||||
child: GestureDetector(
|
child: GestureDetector(
|
||||||
onTap: () {
|
onTap: () => onLike?.call(id, name, isLiked),
|
||||||
setState(() {
|
|
||||||
isLiked = !isLiked;
|
|
||||||
});
|
|
||||||
widget.onLike?.call(widget.name, isLiked);
|
|
||||||
},
|
|
||||||
child: AnimatedSwitcher(
|
child: AnimatedSwitcher(
|
||||||
duration: const Duration(milliseconds: 150),
|
duration: const Duration(milliseconds: 150),
|
||||||
child: isLiked
|
child: isLiked
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flutter_project/data/repositories/anime_repository.dart';
|
import 'package:flutter_project/data/repositories/anime_repository.dart';
|
||||||
import 'package:flutter_project/views/home_page/bloc/events.dart';
|
import 'package:flutter_project/views/home_page/home_bloc/events.dart';
|
||||||
import 'package:flutter_project/views/home_page/bloc/state.dart';
|
import 'package:flutter_project/views/home_page/home_bloc/state.dart';
|
||||||
|
|
||||||
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||||
final AnimeRepository repo;
|
final AnimeRepository repo;
|
||||||
@ -11,7 +11,8 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|||||||
on<HomeShowButtonToTopEvent>(_onShowButton);
|
on<HomeShowButtonToTopEvent>(_onShowButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onShowButton(HomeShowButtonToTopEvent event, Emitter<HomeState> emit) {
|
Future<void> _onShowButton(
|
||||||
|
HomeShowButtonToTopEvent event, Emitter<HomeState> emit) async {
|
||||||
if (event.isShown != null) {
|
if (event.isShown != null) {
|
||||||
if (event.isShown == true) {
|
if (event.isShown == true) {
|
||||||
emit(state.copyWith(isButtonToTopShown: true));
|
emit(state.copyWith(isButtonToTopShown: true));
|
||||||
@ -21,7 +22,8 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
Future<void> _onLoadData(
|
||||||
|
HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||||||
if (event.hasError != null && event.hasError == true) {
|
if (event.hasError != null && event.hasError == true) {
|
||||||
emit(state.copyWith(error: null));
|
emit(state.copyWith(error: null));
|
||||||
}
|
}
|
||||||
@ -34,13 +36,17 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
|||||||
|
|
||||||
String? error;
|
String? error;
|
||||||
|
|
||||||
final data =
|
final data = await repo.loadData(
|
||||||
await repo.loadData(q: event.search, page: event.nextPage ?? 1, onError: (e) => error = e);
|
q: event.search, page: event.nextPage ?? 1, onError: (e) => error = e);
|
||||||
|
|
||||||
if (event.nextPage != null) {
|
if (event.nextPage != null) {
|
||||||
data?.data?.insertAll(0, state.data?.data ?? []);
|
data?.data?.insertAll(0, state.data?.data ?? []);
|
||||||
}
|
}
|
||||||
|
|
||||||
emit(state.copyWith(data: data, isLoading: false, isPaginationLoading: false, error: error));
|
emit(state.copyWith(
|
||||||
|
data: data,
|
||||||
|
isLoading: false,
|
||||||
|
isPaginationLoading: false,
|
||||||
|
error: error));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,14 +2,21 @@ import 'package:flutter/cupertino.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:flutter_project/components/extensions/context_x.dart';
|
import 'package:flutter_project/components/extensions/context_x.dart';
|
||||||
|
import 'package:flutter_project/components/locale/l10n/app_localizations.dart';
|
||||||
import 'package:flutter_project/components/utils/debounce.dart';
|
import 'package:flutter_project/components/utils/debounce.dart';
|
||||||
import 'package:flutter_project/domain/models/card.dart';
|
import 'package:flutter_project/domain/models/card.dart';
|
||||||
import 'package:flutter_project/views/common/svg_objects.dart';
|
import 'package:flutter_project/views/common/svg_objects.dart';
|
||||||
import 'package:flutter_project/views/details_page/details_page.dart';
|
import 'package:flutter_project/views/details_page/details_page.dart';
|
||||||
import 'package:flutter_project/views/home_page/bloc/events.dart';
|
import 'package:flutter_project/views/home_page/home_bloc/events.dart';
|
||||||
import 'package:flutter_project/views/home_page/bloc/state.dart';
|
import 'package:flutter_project/views/home_page/home_bloc/state.dart';
|
||||||
|
import 'package:flutter_project/views/like_bloc/like_events.dart';
|
||||||
|
import 'package:flutter_project/views/like_bloc/like_state.dart';
|
||||||
|
import 'package:flutter_project/views/locale_bloc/locale_bloc.dart';
|
||||||
|
import 'package:flutter_project/views/locale_bloc/locale_events.dart';
|
||||||
|
import 'package:flutter_project/views/locale_bloc/locale_state.dart';
|
||||||
|
|
||||||
import 'bloc/bloc.dart';
|
import '../like_bloc/like_bloc.dart';
|
||||||
|
import 'home_bloc/bloc.dart';
|
||||||
|
|
||||||
part 'card.dart';
|
part 'card.dart';
|
||||||
|
|
||||||
@ -29,7 +36,7 @@ class _HomePageState extends State<HomePage> {
|
|||||||
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
|
||||||
title: Center(
|
title: Center(
|
||||||
child: Text(
|
child: Text(
|
||||||
'Anime list',
|
context.locale.appBarTitle,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -53,8 +60,10 @@ class _BodyState extends State<Body> {
|
|||||||
void initState() {
|
void initState() {
|
||||||
SvgObjects.init();
|
SvgObjects.init();
|
||||||
|
|
||||||
WidgetsBinding.instance.addPostFrameCallback(
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
(_) => context.read<HomeBloc>().add(const HomeLoadDataEvent()));
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||||
|
context.read<LikeBloc>().add(const LoadLikesEvent());
|
||||||
|
});
|
||||||
scrollController.addListener(_viewListScrollListener);
|
scrollController.addListener(_viewListScrollListener);
|
||||||
|
|
||||||
super.initState();
|
super.initState();
|
||||||
@ -72,8 +81,11 @@ class _BodyState extends State<Body> {
|
|||||||
return Stack(children: [
|
return Stack(children: [
|
||||||
Column(
|
Column(
|
||||||
children: [
|
children: [
|
||||||
Padding(
|
Row(
|
||||||
padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Padding(
|
||||||
|
padding: EdgeInsets.only(left: 16, top: 10, bottom: 10),
|
||||||
child: Center(
|
child: Center(
|
||||||
child: CupertinoSearchTextField(
|
child: CupertinoSearchTextField(
|
||||||
placeholder: context.locale.search,
|
placeholder: context.locale.search,
|
||||||
@ -86,6 +98,24 @@ class _BodyState extends State<Body> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
|
GestureDetector(
|
||||||
|
onTap: () =>
|
||||||
|
context.read<LocaleBloc>().add(const ChangeLocaleEvent()),
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: BlocBuilder<LocaleBloc, LocaleState>(
|
||||||
|
builder: (context, state) {
|
||||||
|
return AnimatedSwitcher(
|
||||||
|
duration: const Duration(milliseconds: 250),
|
||||||
|
child: state.currentLocale.languageCode == 'ru'
|
||||||
|
? const SvgRu(key: ValueKey<int>(1))
|
||||||
|
: const SvgUs(key: ValueKey<int>(0)));
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
BlocConsumer<HomeBloc, HomeState>(
|
BlocConsumer<HomeBloc, HomeState>(
|
||||||
listener: (context, state) {
|
listener: (context, state) {
|
||||||
if (state.error != null) {
|
if (state.error != null) {
|
||||||
@ -94,7 +124,8 @@ class _BodyState extends State<Body> {
|
|||||||
},
|
},
|
||||||
builder: (context, state) => state.isLoading
|
builder: (context, state) => state.isLoading
|
||||||
? const CircularProgressIndicator()
|
? const CircularProgressIndicator()
|
||||||
: Expanded(
|
: BlocBuilder<LikeBloc, LikeState>(
|
||||||
|
builder: (context, likeState) => Expanded(
|
||||||
child: RefreshIndicator(
|
child: RefreshIndicator(
|
||||||
onRefresh: _onRefresh,
|
onRefresh: _onRefresh,
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
@ -105,15 +136,18 @@ class _BodyState extends State<Body> {
|
|||||||
final data = state.data?.data?[index];
|
final data = state.data?.data?[index];
|
||||||
|
|
||||||
return data != null
|
return data != null
|
||||||
? _Card.withData(data,
|
? _Card.withData(context.locale, data,
|
||||||
onLike: (title, isLiked) =>
|
isLiked:
|
||||||
_showSnackBar(context, isLiked, title),
|
likeState.likedIds?.contains(data.id) ==
|
||||||
|
true,
|
||||||
|
onLike: _onLike,
|
||||||
onTap: () => _navToDetails(context, data))
|
onTap: () => _navToDetails(context, data))
|
||||||
: const SizedBox.shrink();
|
: const SizedBox.shrink();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
BlocBuilder<HomeBloc, HomeState>(
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
builder: (context, state) => state.isPaginationLoading
|
builder: (context, state) => state.isPaginationLoading
|
||||||
? const CircularProgressIndicator()
|
? const CircularProgressIndicator()
|
||||||
@ -134,6 +168,13 @@ class _BodyState extends State<Body> {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _onLike(String? id, String title, bool isLiked) {
|
||||||
|
if (id != null) {
|
||||||
|
context.read<LikeBloc>().add(ChangeLikeEvent(id));
|
||||||
|
_showSnackBar(context, !isLiked, title);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _onErrorShowDialog(BuildContext context, HomeState state) {
|
void _onErrorShowDialog(BuildContext context, HomeState state) {
|
||||||
showCupertinoDialog(
|
showCupertinoDialog(
|
||||||
context: context,
|
context: context,
|
||||||
@ -207,6 +248,8 @@ class _BodyState extends State<Body> {
|
|||||||
|
|
||||||
void _navToDetails(BuildContext context, CardData d) {
|
void _navToDetails(BuildContext context, CardData d) {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context, CupertinoPageRoute(builder: (context) => DetailsPage(d)));
|
context,
|
||||||
|
CupertinoPageRoute(
|
||||||
|
builder: (context) => DetailsPage(context.locale, d)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
37
lib/views/like_bloc/like_bloc.dart
Normal file
37
lib/views/like_bloc/like_bloc.dart
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:flutter_project/views/like_bloc/like_events.dart';
|
||||||
|
import 'package:flutter_project/views/like_bloc/like_state.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
const String _likedPrefsKey = 'liked';
|
||||||
|
|
||||||
|
class LikeBloc extends Bloc<LikeEvent, LikeState> {
|
||||||
|
LikeBloc() : super(const LikeState(likedIds: [])) {
|
||||||
|
on<ChangeLikeEvent>(_onChangeLike);
|
||||||
|
on<LoadLikesEvent>(_onLoadLikes);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onLoadLikes(
|
||||||
|
LoadLikesEvent event, Emitter<LikeState> emit) async {
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
final data = prefs.getStringList(_likedPrefsKey);
|
||||||
|
|
||||||
|
emit(state.copyWith(likedIds: data));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onChangeLike(
|
||||||
|
ChangeLikeEvent event, Emitter<LikeState> emit) async {
|
||||||
|
final updatedList = List<String>.from(state.likedIds ?? []);
|
||||||
|
|
||||||
|
if (updatedList.contains(event.id)) {
|
||||||
|
updatedList.remove(event.id);
|
||||||
|
} else {
|
||||||
|
updatedList.add(event.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
prefs.setStringList(_likedPrefsKey, updatedList);
|
||||||
|
|
||||||
|
emit(state.copyWith(likedIds: updatedList));
|
||||||
|
}
|
||||||
|
}
|
12
lib/views/like_bloc/like_events.dart
Normal file
12
lib/views/like_bloc/like_events.dart
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
abstract class LikeEvent {
|
||||||
|
const LikeEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
class LoadLikesEvent extends LikeEvent {
|
||||||
|
const LoadLikesEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
class ChangeLikeEvent extends LikeEvent {
|
||||||
|
final String id;
|
||||||
|
const ChangeLikeEvent(this.id);
|
||||||
|
}
|
14
lib/views/like_bloc/like_state.dart
Normal file
14
lib/views/like_bloc/like_state.dart
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
|
||||||
|
part 'like_state.g.dart';
|
||||||
|
|
||||||
|
@CopyWith()
|
||||||
|
class LikeState extends Equatable {
|
||||||
|
final List<String>? likedIds;
|
||||||
|
|
||||||
|
const LikeState({this.likedIds});
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [likedIds];
|
||||||
|
}
|
56
lib/views/like_bloc/like_state.g.dart
Normal file
56
lib/views/like_bloc/like_state.g.dart
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'like_state.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// CopyWithGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
abstract class _$LikeStateCWProxy {
|
||||||
|
LikeState likedIds(List<String>? likedIds);
|
||||||
|
|
||||||
|
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `LikeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||||
|
///
|
||||||
|
/// Usage
|
||||||
|
/// ```dart
|
||||||
|
/// LikeState(...).copyWith(id: 12, name: "My name")
|
||||||
|
/// ````
|
||||||
|
LikeState call({
|
||||||
|
List<String>? likedIds,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfLikeState.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfLikeState.copyWith.fieldName(...)`
|
||||||
|
class _$LikeStateCWProxyImpl implements _$LikeStateCWProxy {
|
||||||
|
const _$LikeStateCWProxyImpl(this._value);
|
||||||
|
|
||||||
|
final LikeState _value;
|
||||||
|
|
||||||
|
@override
|
||||||
|
LikeState likedIds(List<String>? likedIds) => this(likedIds: likedIds);
|
||||||
|
|
||||||
|
@override
|
||||||
|
|
||||||
|
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `LikeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||||
|
///
|
||||||
|
/// Usage
|
||||||
|
/// ```dart
|
||||||
|
/// LikeState(...).copyWith(id: 12, name: "My name")
|
||||||
|
/// ````
|
||||||
|
LikeState call({
|
||||||
|
Object? likedIds = const $CopyWithPlaceholder(),
|
||||||
|
}) {
|
||||||
|
return LikeState(
|
||||||
|
likedIds: likedIds == const $CopyWithPlaceholder()
|
||||||
|
? _value.likedIds
|
||||||
|
// ignore: cast_nullable_to_non_nullable
|
||||||
|
: likedIds as List<String>?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension $LikeStateCopyWith on LikeState {
|
||||||
|
/// Returns a callable class that can be used as follows: `instanceOfLikeState.copyWith(...)` or like so:`instanceOfLikeState.copyWith.fieldName(...)`.
|
||||||
|
// ignore: library_private_types_in_public_api
|
||||||
|
_$LikeStateCWProxy get copyWith => _$LikeStateCWProxyImpl(this);
|
||||||
|
}
|
21
lib/views/locale_bloc/locale_bloc.dart
Normal file
21
lib/views/locale_bloc/locale_bloc.dart
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:flutter_project/components/locale/l10n/app_localizations.dart';
|
||||||
|
|
||||||
|
import 'locale_events.dart';
|
||||||
|
import 'locale_state.dart';
|
||||||
|
|
||||||
|
class LocaleBloc extends Bloc<LocaleEvent, LocaleState> {
|
||||||
|
LocaleBloc(Locale defaultLocale)
|
||||||
|
: super(LocaleState(currentLocale: defaultLocale)) {
|
||||||
|
on<ChangeLocaleEvent>(_onChangeLocale);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onChangeLocale(
|
||||||
|
ChangeLocaleEvent event, Emitter<LocaleState> emit) async {
|
||||||
|
final toChange = AppLocale.supportedLocales.firstWhere(
|
||||||
|
(loc) => loc.languageCode != state.currentLocale.languageCode);
|
||||||
|
emit(state.copyWith(currentLocale: toChange));
|
||||||
|
}
|
||||||
|
}
|
7
lib/views/locale_bloc/locale_events.dart
Normal file
7
lib/views/locale_bloc/locale_events.dart
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
abstract class LocaleEvent {
|
||||||
|
const LocaleEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
class ChangeLocaleEvent extends LocaleEvent {
|
||||||
|
const ChangeLocaleEvent();
|
||||||
|
}
|
16
lib/views/locale_bloc/locale_state.dart
Normal file
16
lib/views/locale_bloc/locale_state.dart
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
|
||||||
|
part 'locale_state.g.dart';
|
||||||
|
|
||||||
|
@CopyWith()
|
||||||
|
class LocaleState extends Equatable {
|
||||||
|
final Locale currentLocale;
|
||||||
|
|
||||||
|
const LocaleState({required this.currentLocale});
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [currentLocale];
|
||||||
|
}
|
58
lib/views/locale_bloc/locale_state.g.dart
Normal file
58
lib/views/locale_bloc/locale_state.g.dart
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'locale_state.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// CopyWithGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
abstract class _$LocaleStateCWProxy {
|
||||||
|
LocaleState currentLocale(Locale currentLocale);
|
||||||
|
|
||||||
|
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `LocaleState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||||
|
///
|
||||||
|
/// Usage
|
||||||
|
/// ```dart
|
||||||
|
/// LocaleState(...).copyWith(id: 12, name: "My name")
|
||||||
|
/// ````
|
||||||
|
LocaleState call({
|
||||||
|
Locale? currentLocale,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfLocaleState.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfLocaleState.copyWith.fieldName(...)`
|
||||||
|
class _$LocaleStateCWProxyImpl implements _$LocaleStateCWProxy {
|
||||||
|
const _$LocaleStateCWProxyImpl(this._value);
|
||||||
|
|
||||||
|
final LocaleState _value;
|
||||||
|
|
||||||
|
@override
|
||||||
|
LocaleState currentLocale(Locale currentLocale) =>
|
||||||
|
this(currentLocale: currentLocale);
|
||||||
|
|
||||||
|
@override
|
||||||
|
|
||||||
|
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `LocaleState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||||
|
///
|
||||||
|
/// Usage
|
||||||
|
/// ```dart
|
||||||
|
/// LocaleState(...).copyWith(id: 12, name: "My name")
|
||||||
|
/// ````
|
||||||
|
LocaleState call({
|
||||||
|
Object? currentLocale = const $CopyWithPlaceholder(),
|
||||||
|
}) {
|
||||||
|
return LocaleState(
|
||||||
|
currentLocale:
|
||||||
|
currentLocale == const $CopyWithPlaceholder() || currentLocale == null
|
||||||
|
? _value.currentLocale
|
||||||
|
// ignore: cast_nullable_to_non_nullable
|
||||||
|
: currentLocale as Locale,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension $LocaleStateCopyWith on LocaleState {
|
||||||
|
/// Returns a callable class that can be used as follows: `instanceOfLocaleState.copyWith(...)` or like so:`instanceOfLocaleState.copyWith.fieldName(...)`.
|
||||||
|
// ignore: library_private_types_in_public_api
|
||||||
|
_$LocaleStateCWProxy get copyWith => _$LocaleStateCWProxyImpl(this);
|
||||||
|
}
|
@ -183,7 +183,7 @@ packages:
|
|||||||
source: hosted
|
source: hosted
|
||||||
version: "3.1.1"
|
version: "3.1.1"
|
||||||
copy_with_extension:
|
copy_with_extension:
|
||||||
dependency: "direct dev"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: copy_with_extension
|
name: copy_with_extension
|
||||||
sha256: fbcf890b0c34aedf0894f91a11a579994b61b4e04080204656b582708b5b1125
|
sha256: fbcf890b0c34aedf0894f91a11a579994b61b4e04080204656b582708b5b1125
|
||||||
@ -191,7 +191,7 @@ packages:
|
|||||||
source: hosted
|
source: hosted
|
||||||
version: "5.0.4"
|
version: "5.0.4"
|
||||||
copy_with_extension_gen:
|
copy_with_extension_gen:
|
||||||
dependency: "direct dev"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: copy_with_extension_gen
|
name: copy_with_extension_gen
|
||||||
sha256: "51cd11094096d40824c8da629ca7f16f3b7cea5fc44132b679617483d43346b0"
|
sha256: "51cd11094096d40824c8da629ca7f16f3b7cea5fc44132b679617483d43346b0"
|
||||||
@ -627,7 +627,7 @@ packages:
|
|||||||
source: hosted
|
source: hosted
|
||||||
version: "1.3.0"
|
version: "1.3.0"
|
||||||
shared_preferences:
|
shared_preferences:
|
||||||
dependency: "direct dev"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: shared_preferences
|
name: shared_preferences
|
||||||
sha256: "746e5369a43170c25816cc472ee016d3a66bc13fcf430c0bc41ad7b4b2922051"
|
sha256: "746e5369a43170c25816cc472ee016d3a66bc13fcf430c0bc41ad7b4b2922051"
|
||||||
|
@ -41,6 +41,9 @@ dependencies:
|
|||||||
pretty_dio_logger: ^1.4.0
|
pretty_dio_logger: ^1.4.0
|
||||||
flutter_bloc: ^8.1.6
|
flutter_bloc: ^8.1.6
|
||||||
equatable: ^2.0.5
|
equatable: ^2.0.5
|
||||||
|
copy_with_extension: ^5.0.4
|
||||||
|
copy_with_extension_gen: ^5.0.4
|
||||||
|
shared_preferences: ^2.3.2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
@ -60,10 +63,6 @@ dev_dependencies:
|
|||||||
sdk: flutter
|
sdk: flutter
|
||||||
intl: ^0.19.0
|
intl: ^0.19.0
|
||||||
|
|
||||||
|
|
||||||
shared_preferences: ^2.3.2
|
|
||||||
copy_with_extension_gen: ^5.0.4
|
|
||||||
copy_with_extension: ^5.0.4
|
|
||||||
json_serializable: ^6.7.1
|
json_serializable: ^6.7.1
|
||||||
build_runner: ^2.4.9
|
build_runner: ^2.4.9
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user