7 лайки готовы
This commit is contained in:
parent
f4cbfe7e68
commit
25e9a42eb4
@ -4,6 +4,7 @@
|
||||
"search": "Search",
|
||||
"liked": "liked!",
|
||||
"disliked": "disliked :(",
|
||||
"bread": "Unknown",
|
||||
|
||||
"arbEnding": "Чтобы не забыть про отсутствие запятой :)"
|
||||
}
|
@ -4,6 +4,7 @@
|
||||
"search": "Поиск",
|
||||
"liked": "Добавлено в понравившиеся :)",
|
||||
"disliked": "Удалено из понравившегося :(",
|
||||
"bread": "Неизвестное название",
|
||||
|
||||
"arbEnding": "Чтобы не забыть про отсутствие запятой :)"
|
||||
}
|
@ -5,6 +5,9 @@ import 'package:labs_petrushin/Presentation/common/svg_objects.dart';
|
||||
import 'package:labs_petrushin/Presentation/home_page/bloc/bloc.dart';
|
||||
import 'package:labs_petrushin/Presentation/home_page/bloc/events.dart';
|
||||
import 'package:labs_petrushin/Presentation/home_page/bloc/state.dart';
|
||||
import 'package:labs_petrushin/Presentation/like_bloc/like_bloc.dart';
|
||||
import 'package:labs_petrushin/Presentation/like_bloc/like_event.dart';
|
||||
import 'package:labs_petrushin/Presentation/like_bloc/like_state.dart';
|
||||
import 'package:labs_petrushin/Presentation/locale_bloc/locale_bloc.dart';
|
||||
import 'package:labs_petrushin/Presentation/locale_bloc/locale_events.dart';
|
||||
import 'package:labs_petrushin/Presentation/locale_bloc/locale_state.dart';
|
||||
@ -45,6 +48,7 @@ class BodyState extends State<Body> {
|
||||
void initState() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||
context.read<LikeBloc>().add(const LoadLikesEvent());
|
||||
});
|
||||
|
||||
scrollController.addListener(_onNextPageListener);
|
||||
@ -111,27 +115,31 @@ class BodyState extends State<Body> {
|
||||
)
|
||||
: state.isLoading
|
||||
? const CircularProgressIndicator()
|
||||
: Expanded(
|
||||
child: RefreshIndicator(
|
||||
onRefresh: _onRefresh,
|
||||
child: ListView.builder(
|
||||
controller: scrollController,
|
||||
padding: EdgeInsets.zero,
|
||||
itemCount: state.data?.data?.length ?? 0,
|
||||
itemBuilder: (context, index) {
|
||||
final data = state.data?.data?[index];
|
||||
return data != null
|
||||
? MyCard.fromData(
|
||||
data,
|
||||
onLike: (title, isLiked) =>
|
||||
_showSnackBar(context, title, isLiked),
|
||||
onTap: () => _navToDetails(context, data),
|
||||
)
|
||||
: const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
: BlocBuilder<LikeBloc, LikeState>(
|
||||
builder: (context, likeState) {
|
||||
return Expanded(
|
||||
child: RefreshIndicator(
|
||||
onRefresh: _onRefresh,
|
||||
child: ListView.builder(
|
||||
controller: scrollController,
|
||||
padding: EdgeInsets.zero,
|
||||
itemCount: state.data?.data?.length ?? 0,
|
||||
itemBuilder: (context, index) {
|
||||
final data = state.data?.data?[index];
|
||||
return data != null
|
||||
? MyCard.fromData(
|
||||
data,
|
||||
onLike: _onLike,
|
||||
isLiked: likeState.likedIds?.contains(data.id.toString()) == true,
|
||||
onTap: () => _navToDetails(context, data),
|
||||
)
|
||||
: const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
),
|
||||
),
|
||||
BlocBuilder<HomeBloc, HomeState>(
|
||||
builder: (context, state) => state.isPaginationLoading
|
||||
@ -167,4 +175,11 @@ class BodyState extends State<Body> {
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
void _onLike(String? id, String title, bool isLiked) {
|
||||
if (id != null) {
|
||||
context.read<LikeBloc>().add(ChangeLikeEvent(id));
|
||||
_showSnackBar(context, title, !isLiked);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
35
lib/Presentation/like_bloc/like_bloc.dart
Normal file
35
lib/Presentation/like_bloc/like_bloc.dart
Normal file
@ -0,0 +1,35 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:labs_petrushin/Presentation/like_bloc/like_event.dart';
|
||||
import 'package:labs_petrushin/Presentation/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));
|
||||
}
|
||||
}
|
13
lib/Presentation/like_bloc/like_event.dart
Normal file
13
lib/Presentation/like_bloc/like_event.dart
Normal file
@ -0,0 +1,13 @@
|
||||
abstract class LikeEvent {
|
||||
const LikeEvent();
|
||||
}
|
||||
|
||||
class LoadLikesEvent extends LikeEvent {
|
||||
const LoadLikesEvent();
|
||||
}
|
||||
|
||||
class ChangeLikeEvent extends LikeEvent {
|
||||
final String id;
|
||||
|
||||
const ChangeLikeEvent(this.id);
|
||||
}
|
14
lib/Presentation/like_bloc/like_state.dart
Normal file
14
lib/Presentation/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({required this.likedIds});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [likedIds];
|
||||
}
|
56
lib/Presentation/like_bloc/like_state.g.dart
Normal file
56
lib/Presentation/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);
|
||||
}
|
@ -2,6 +2,7 @@ import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:labs_petrushin/Presentation/home_page/bloc/bloc.dart';
|
||||
import 'package:labs_petrushin/Presentation/like_bloc/like_bloc.dart';
|
||||
import 'package:labs_petrushin/Presentation/locale_bloc/locale_bloc.dart';
|
||||
import 'package:labs_petrushin/Presentation/locale_bloc/locale_state.dart';
|
||||
import 'package:labs_petrushin/components/locale/l10n/app_locale.dart';
|
||||
@ -9,6 +10,8 @@ import 'package:labs_petrushin/repositories/food_repository.dart';
|
||||
import 'home_page/home_page.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import 'like_bloc/like_bloc.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
}
|
||||
@ -36,11 +39,15 @@ class MyApp extends StatelessWidget {
|
||||
home: RepositoryProvider<FoodRepository>(
|
||||
lazy: true,
|
||||
create: (_) => FoodRepository(),
|
||||
child: BlocProvider<HomeBloc>(
|
||||
child: BlocProvider<LikeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => HomeBloc(context.read<FoodRepository>()),
|
||||
child: const MyHomePage(
|
||||
title: 'Петрушин Егор Александрович',
|
||||
create: (context) => LikeBloc(),
|
||||
child: BlocProvider<HomeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => HomeBloc(context.read<FoodRepository>()),
|
||||
child: const MyHomePage(
|
||||
title: 'Петрушин Егор Александрович',
|
||||
),
|
||||
),
|
||||
)));
|
||||
}
|
||||
|
@ -113,6 +113,12 @@ abstract class AppLocale {
|
||||
/// **'Удалено из понравившегося :('**
|
||||
String get disliked;
|
||||
|
||||
/// No description provided for @bread.
|
||||
///
|
||||
/// In ru, this message translates to:
|
||||
/// **'Неизвестное название'**
|
||||
String get bread;
|
||||
|
||||
/// No description provided for @arbEnding.
|
||||
///
|
||||
/// In ru, this message translates to:
|
||||
|
@ -15,6 +15,9 @@ class AppLocaleEn extends AppLocale {
|
||||
@override
|
||||
String get disliked => 'disliked :(';
|
||||
|
||||
@override
|
||||
String get bread => 'Unknown';
|
||||
|
||||
@override
|
||||
String get arbEnding => 'Чтобы не забыть про отсутствие запятой :)';
|
||||
}
|
||||
|
@ -15,6 +15,9 @@ class AppLocaleRu extends AppLocale {
|
||||
@override
|
||||
String get disliked => 'Удалено из понравившегося :(';
|
||||
|
||||
@override
|
||||
String get bread => 'Неизвестное название';
|
||||
|
||||
@override
|
||||
String get arbEnding => 'Чтобы не забыть про отсутствие запятой :)';
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import '../../domain/models/home.dart';
|
||||
|
||||
extension CharacterDataDtoToModel on FoodDataDto {
|
||||
CardData toDomain() => CardData(
|
||||
text: brandName ?? "Просто хлэп", info: description ?? "Очень кусьна", urlImage: image);
|
||||
text: brandName ?? "Unknown", info: description ?? "Unknown", urlImage: image, id: fdcId!);
|
||||
}
|
||||
|
||||
extension CharactersDtoToModel on FoodsDto {
|
||||
|
@ -1,21 +1,24 @@
|
||||
part of '../../Presentation/home_page/home_page.dart';
|
||||
|
||||
typedef OnLikeCallback = void Function(String title, bool isLiked)?;
|
||||
typedef OnLikeCallback = void Function(String? id, String title, bool isLiked)?;
|
||||
|
||||
class CardData {
|
||||
final int id;
|
||||
final String text;
|
||||
final String info;
|
||||
final String? urlImage;
|
||||
|
||||
CardData({required this.text, required this.info, required this.urlImage});
|
||||
CardData({required this.text, required this.info, required this.urlImage, required this.id});
|
||||
}
|
||||
|
||||
class MyCard extends StatefulWidget {
|
||||
class MyCard extends StatelessWidget {
|
||||
final String text;
|
||||
final String info;
|
||||
final String? urlImage;
|
||||
final OnLikeCallback onLike;
|
||||
final VoidCallback? onTap;
|
||||
final int id;
|
||||
final bool isLiked;
|
||||
|
||||
const MyCard(
|
||||
{super.key,
|
||||
@ -23,27 +26,24 @@ class MyCard extends StatefulWidget {
|
||||
required this.info,
|
||||
required this.urlImage,
|
||||
this.onLike,
|
||||
this.onTap});
|
||||
this.onTap,
|
||||
required this.id, this.isLiked = false});
|
||||
|
||||
factory MyCard.fromData(CardData data, {OnLikeCallback onLike, VoidCallback? onTap}) => MyCard(
|
||||
factory MyCard.fromData(CardData data, {OnLikeCallback onLike, VoidCallback? onTap, bool isLiked = false}) => MyCard(
|
||||
text: data.text,
|
||||
info: data.info,
|
||||
urlImage: data.urlImage,
|
||||
onLike: onLike,
|
||||
onTap: onTap,
|
||||
isLiked: isLiked,
|
||||
id: data.id.toInt()
|
||||
);
|
||||
|
||||
@override
|
||||
State<MyCard> createState() => _MyCardState();
|
||||
}
|
||||
|
||||
class _MyCardState extends State<MyCard> {
|
||||
bool isLiked = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return GestureDetector(
|
||||
onTap: widget.onTap,
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
margin: const EdgeInsets.all(16),
|
||||
constraints: const BoxConstraints(minHeight: 140),
|
||||
@ -65,7 +65,7 @@ class _MyCardState extends State<MyCard> {
|
||||
height: double.infinity,
|
||||
width: 160,
|
||||
child: Image.network(
|
||||
widget.urlImage ??
|
||||
urlImage ??
|
||||
'https://hlebzavod3.ru/images/virtuemart/product/011_IMG_9657.jpg',
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => const Placeholder(),
|
||||
@ -79,11 +79,11 @@ class _MyCardState extends State<MyCard> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
widget.text,
|
||||
text,
|
||||
style: Theme.of(context).textTheme.headlineLarge,
|
||||
),
|
||||
Text(
|
||||
widget.info,
|
||||
info,
|
||||
style: Theme.of(context).textTheme.bodyLarge,
|
||||
),
|
||||
],
|
||||
@ -93,30 +93,22 @@ class _MyCardState extends State<MyCard> {
|
||||
Align(
|
||||
alignment: Alignment.bottomRight,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.only(
|
||||
left: 8.0,
|
||||
right: 16.0,
|
||||
bottom: 16.0,
|
||||
),
|
||||
padding:
|
||||
const EdgeInsets.only(left: 8, right: 16, bottom: 16),
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
isLiked = !isLiked;
|
||||
});
|
||||
widget.onLike?.call(widget.text, isLiked);
|
||||
},
|
||||
onTap: () => onLike?.call(id.toString(), text, isLiked),
|
||||
child: AnimatedSwitcher(
|
||||
duration: const Duration(milliseconds: 300),
|
||||
duration: const Duration(milliseconds: 200),
|
||||
child: isLiked
|
||||
? const Icon(
|
||||
Icons.favorite,
|
||||
color: Colors.redAccent,
|
||||
key: ValueKey<int>(0),
|
||||
)
|
||||
Icons.favorite,
|
||||
color: Colors.redAccent,
|
||||
key: ValueKey<int>(0),
|
||||
)
|
||||
: const Icon(
|
||||
Icons.favorite_border,
|
||||
key: ValueKey<int>(1),
|
||||
),
|
||||
Icons.favorite_border,
|
||||
key: ValueKey<int>(1),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
Loading…
Reference in New Issue
Block a user