lab 7 done
This commit is contained in:
parent
cfbe1971ee
commit
c7f9571e4f
@ -13,14 +13,14 @@ class CarDto {
|
|||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class CarAttributesResultDto {
|
class CarAttributesResultDto {
|
||||||
@JsonKey(name: 'Model_ID')
|
|
||||||
final int? modelID;
|
|
||||||
@JsonKey(name: 'Make_Name')
|
@JsonKey(name: 'Make_Name')
|
||||||
final String? makeName;
|
final String? makeName;
|
||||||
@JsonKey(name: 'Model_Name')
|
@JsonKey(name: 'Model_Name')
|
||||||
final String? modelName;
|
final String? modelName;
|
||||||
|
@JsonKey(name: 'Model_ID')
|
||||||
|
final int? id;
|
||||||
|
|
||||||
const CarAttributesResultDto({this.modelID, this.makeName, this.modelName});
|
const CarAttributesResultDto({this.id, this.makeName, this.modelName});
|
||||||
|
|
||||||
factory CarAttributesResultDto.fromJson(Map<String, dynamic> json) =>
|
factory CarAttributesResultDto.fromJson(Map<String, dynamic> json) =>
|
||||||
_$CarAttributesResultDtoFromJson(json);
|
_$CarAttributesResultDtoFromJson(json);
|
||||||
|
@ -16,7 +16,7 @@ CarDto _$CarDtoFromJson(Map<String, dynamic> json) => CarDto(
|
|||||||
CarAttributesResultDto _$CarAttributesResultDtoFromJson(
|
CarAttributesResultDto _$CarAttributesResultDtoFromJson(
|
||||||
Map<String, dynamic> json) =>
|
Map<String, dynamic> json) =>
|
||||||
CarAttributesResultDto(
|
CarAttributesResultDto(
|
||||||
modelID: (json['Model_ID'] as num?)?.toInt(),
|
id: (json['Model_ID'] as num?)?.toInt(),
|
||||||
makeName: json['Make_Name'] as String?,
|
makeName: json['Make_Name'] as String?,
|
||||||
modelName: json['Model_Name'] as String?,
|
modelName: json['Model_Name'] as String?,
|
||||||
);
|
);
|
||||||
|
@ -16,9 +16,10 @@ extension CarDtoToModel on CarDto {
|
|||||||
extension CarAttributesResultDtoToModel on CarAttributesResultDto {
|
extension CarAttributesResultDtoToModel on CarAttributesResultDto {
|
||||||
CarData toDomain() => CarData(
|
CarData toDomain() => CarData(
|
||||||
modelName ?? 'unknown',
|
modelName ?? 'unknown',
|
||||||
descriptionCar: 'ID: $modelID \n'
|
descriptionCar: 'ID: $id \n'
|
||||||
'Marka: $makeName \n'
|
'Marka: $makeName \n'
|
||||||
'Model: $modelName',
|
'Model: $modelName',
|
||||||
imageUrl: _imagePlaceholder,
|
imageUrl: _imagePlaceholder,
|
||||||
|
id: id,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ class CarData {
|
|||||||
final String descriptionCar;
|
final String descriptionCar;
|
||||||
final IconData icon;
|
final IconData icon;
|
||||||
final String? imageUrl;
|
final String? imageUrl;
|
||||||
final String? id;
|
final int? id;
|
||||||
|
|
||||||
CarData(
|
CarData(
|
||||||
this.text, {
|
this.text, {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
part of 'home_page.dart';
|
part of 'home_page.dart';
|
||||||
|
|
||||||
typedef OnLikeCallback = void Function(String? id, String title, bool isLiked)?;
|
typedef OnLikeCallback = void Function(int? id, String title, bool isLiked)?;
|
||||||
|
|
||||||
class _Car extends StatelessWidget {
|
class _Car extends StatelessWidget {
|
||||||
final String text;
|
final String text;
|
||||||
@ -9,7 +9,7 @@ class _Car extends StatelessWidget {
|
|||||||
final String? imageUrl;
|
final String? imageUrl;
|
||||||
final OnLikeCallback onLike;
|
final OnLikeCallback onLike;
|
||||||
final VoidCallback? onTap;
|
final VoidCallback? onTap;
|
||||||
final String? id;
|
final int? id;
|
||||||
final bool isLiked;
|
final bool isLiked;
|
||||||
|
|
||||||
const _Car(
|
const _Car(
|
||||||
@ -66,7 +66,7 @@ class _Car extends StatelessWidget {
|
|||||||
ClipRRect(
|
ClipRRect(
|
||||||
borderRadius: BorderRadius.circular(20),
|
borderRadius: BorderRadius.circular(20),
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
height: 160,
|
height: 170,
|
||||||
width: 150,
|
width: 150,
|
||||||
child: Image.network(
|
child: Image.network(
|
||||||
imageUrl ?? '',
|
imageUrl ?? '',
|
||||||
@ -87,7 +87,7 @@ class _Car extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
Text(
|
Text(
|
||||||
descriptionCar,
|
descriptionCar,
|
||||||
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.normal),
|
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.normal),
|
||||||
),
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
// Отступ между текстом и виджетом со скидкой
|
// Отступ между текстом и виджетом со скидкой
|
||||||
|
@ -173,7 +173,7 @@ class _BodyState extends State<_Body> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onLike(String? id, String title, bool isLiked) {
|
void _onLike(int? id, String title, bool isLiked) {
|
||||||
if (id != null) {
|
if (id != null) {
|
||||||
context.read<LikeBloc>().add(ChangeLikeEvent(id));
|
context.read<LikeBloc>().add(ChangeLikeEvent(id));
|
||||||
_showSnackBar(context, title, !isLiked);
|
_showSnackBar(context, title, !isLiked);
|
||||||
@ -185,9 +185,9 @@ class _BodyState extends State<_Body> {
|
|||||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||||
content: Text(
|
content: Text(
|
||||||
'$title ${isLiked ? context.locale.liked : context.locale.disliked}',
|
'$title ${isLiked ? context.locale.liked : context.locale.disliked}',
|
||||||
style: Theme.of(context).textTheme.bodyLarge,
|
style: Theme.of(context).textTheme.bodyLarge?.copyWith(color: Colors.white),
|
||||||
),
|
),
|
||||||
backgroundColor: Colors.orangeAccent,
|
backgroundColor: Colors.deepPurple,
|
||||||
duration: const Duration(seconds: 1),
|
duration: const Duration(seconds: 1),
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
|
@ -15,11 +15,14 @@ class LikeBloc extends Bloc<LikeEvent, LikeState> {
|
|||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
final data = prefs.getStringList(_likedPrefsKey);
|
final data = prefs.getStringList(_likedPrefsKey);
|
||||||
|
|
||||||
emit(state.copyWith(likedIds: data));
|
// Преобразуем List<String> в List<int>
|
||||||
|
final likedIds = data?.map(int.parse).toList() ?? [];
|
||||||
|
|
||||||
|
emit(state.copyWith(likedIds: likedIds));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onChangeLike(ChangeLikeEvent event, Emitter<LikeState> emit) async {
|
Future<void> _onChangeLike(ChangeLikeEvent event, Emitter<LikeState> emit) async {
|
||||||
final updatedList = List<String>.from(state.likedIds ?? []);
|
final updatedList = List<int>.from(state.likedIds ?? []);
|
||||||
|
|
||||||
if (updatedList.contains(event.id)) {
|
if (updatedList.contains(event.id)) {
|
||||||
updatedList.remove(event.id);
|
updatedList.remove(event.id);
|
||||||
@ -28,7 +31,10 @@ class LikeBloc extends Bloc<LikeEvent, LikeState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
prefs.setStringList(_likedPrefsKey, updatedList);
|
|
||||||
|
// Сохраняем List<String> в SharedPreferences
|
||||||
|
// Преобразуем список целых чисел в список строк
|
||||||
|
prefs.setStringList(_likedPrefsKey, updatedList.map((id) => id.toString()).toList());
|
||||||
|
|
||||||
emit(state.copyWith(likedIds: updatedList));
|
emit(state.copyWith(likedIds: updatedList));
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ class LoadLikesEvent extends LikeEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class ChangeLikeEvent extends LikeEvent {
|
class ChangeLikeEvent extends LikeEvent {
|
||||||
final String id;
|
final int id;
|
||||||
|
|
||||||
const ChangeLikeEvent(this.id);
|
const ChangeLikeEvent(this.id);
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ part 'like_state.g.dart';
|
|||||||
|
|
||||||
@CopyWith()
|
@CopyWith()
|
||||||
class LikeState extends Equatable {
|
class LikeState extends Equatable {
|
||||||
final List<String>? likedIds;
|
final List<int>? likedIds;
|
||||||
|
|
||||||
const LikeState({required this.likedIds});
|
const LikeState({required this.likedIds});
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ part of 'like_state.dart';
|
|||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
abstract class _$LikeStateCWProxy {
|
abstract class _$LikeStateCWProxy {
|
||||||
LikeState likedIds(List<String>? likedIds);
|
LikeState likedIds(List<int>? 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.
|
/// 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.
|
||||||
///
|
///
|
||||||
@ -16,7 +16,7 @@ abstract class _$LikeStateCWProxy {
|
|||||||
/// LikeState(...).copyWith(id: 12, name: "My name")
|
/// LikeState(...).copyWith(id: 12, name: "My name")
|
||||||
/// ````
|
/// ````
|
||||||
LikeState call({
|
LikeState call({
|
||||||
List<String>? likedIds,
|
List<int>? likedIds,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ class _$LikeStateCWProxyImpl implements _$LikeStateCWProxy {
|
|||||||
final LikeState _value;
|
final LikeState _value;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
LikeState likedIds(List<String>? likedIds) => this(likedIds: likedIds);
|
LikeState likedIds(List<int>? likedIds) => this(likedIds: likedIds);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ class _$LikeStateCWProxyImpl implements _$LikeStateCWProxy {
|
|||||||
likedIds: likedIds == const $CopyWithPlaceholder()
|
likedIds: likedIds == const $CopyWithPlaceholder()
|
||||||
? _value.likedIds
|
? _value.likedIds
|
||||||
// ignore: cast_nullable_to_non_nullable
|
// ignore: cast_nullable_to_non_nullable
|
||||||
: likedIds as List<String>?,
|
: likedIds as List<int>?,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user