5
This commit is contained in:
parent
ba69a8aa27
commit
bf376bd912
@ -2,7 +2,7 @@ import 'package:flutter/cupertino.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:labs_petrushin/repositories/mock_repository.dart';
|
import 'package:labs_petrushin/repositories/mock_repository.dart';
|
||||||
import '../../Presentation/detailPage.dart';
|
import '../../Presentation/detailPage.dart';
|
||||||
import '../../repositories/bread_repository.dart';
|
import '../../repositories/food_repository.dart';
|
||||||
part '../../domain/models/card.dart';
|
part '../../domain/models/card.dart';
|
||||||
|
|
||||||
class MyHomePage extends StatefulWidget {
|
class MyHomePage extends StatefulWidget {
|
||||||
@ -44,7 +44,7 @@ class _BodyState extends State<Body> {
|
|||||||
final TextEditingController searchController = TextEditingController();
|
final TextEditingController searchController = TextEditingController();
|
||||||
late Future<List<CardData>?> data;
|
late Future<List<CardData>?> data;
|
||||||
|
|
||||||
final repo = BreadRepository();
|
final repo = FoodRepository();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
@ -95,7 +95,7 @@ class _BodyState extends State<Body> {
|
|||||||
WidgetsBinding.instance.addPostFrameCallback((_){
|
WidgetsBinding.instance.addPostFrameCallback((_){
|
||||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||||
content: Text(
|
content: Text(
|
||||||
'Bread liked!',
|
'Food liked!',
|
||||||
style: Theme.of(context).textTheme.bodyLarge,
|
style: Theme.of(context).textTheme.bodyLarge,
|
||||||
),
|
),
|
||||||
backgroundColor: Colors.red,
|
backgroundColor: Colors.red,
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
import 'dart:convert';
|
|
||||||
import 'dart:ffi';
|
|
||||||
|
|
||||||
import 'package:json_annotation/json_annotation.dart';
|
|
||||||
|
|
||||||
part 'breads_dto.g.dart';
|
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
|
||||||
class BreadsDto {
|
|
||||||
final List<BreadDataDto>? foods;
|
|
||||||
|
|
||||||
const BreadsDto({this.foods});
|
|
||||||
|
|
||||||
factory BreadsDto.fromJson(Map<String, dynamic> json) => _$BreadsDtoFromJson(json);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
|
||||||
class BreadDataDto {
|
|
||||||
final int? fdcId;
|
|
||||||
final String? brandName;
|
|
||||||
final String? description;
|
|
||||||
final String? image;
|
|
||||||
|
|
||||||
const BreadDataDto({this.fdcId, this.brandName, this.description, this.image});
|
|
||||||
|
|
||||||
factory BreadDataDto.fromJson(Map<String, dynamic> json) => _$BreadDataDtoFromJson(json);
|
|
||||||
}
|
|
28
lib/data/dtos/foods_dto.dart
Normal file
28
lib/data/dtos/foods_dto.dart
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:ffi';
|
||||||
|
|
||||||
|
import 'package:json_annotation/json_annotation.dart';
|
||||||
|
|
||||||
|
part 'Foods_dto.g.dart';
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class FoodsDto {
|
||||||
|
final List<FoodDataDto>? foods;
|
||||||
|
|
||||||
|
const FoodsDto({this.foods});
|
||||||
|
|
||||||
|
factory FoodsDto.fromJson(Map<String, dynamic> json) => _$FoodsDtoFromJson(json);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class FoodDataDto {
|
||||||
|
final int? fdcId;
|
||||||
|
final String? brandName;
|
||||||
|
final String? description;
|
||||||
|
final String? image;
|
||||||
|
|
||||||
|
const FoodDataDto({this.fdcId, this.brandName, this.description, this.image});
|
||||||
|
|
||||||
|
factory FoodDataDto.fromJson(Map<String, dynamic> json) => _$FoodDataDtoFromJson(json);
|
||||||
|
}
|
@ -1,18 +1,18 @@
|
|||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
part of 'breads_dto.dart';
|
part of 'foods_dto.dart';
|
||||||
|
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
// JsonSerializableGenerator
|
// JsonSerializableGenerator
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
BreadsDto _$BreadsDtoFromJson(Map<String, dynamic> json) => BreadsDto(
|
FoodsDto _$FoodsDtoFromJson(Map<String, dynamic> json) => FoodsDto(
|
||||||
foods: (json['foods'] as List<dynamic>?)
|
foods: (json['foods'] as List<dynamic>?)
|
||||||
?.map((e) => BreadDataDto.fromJson(e as Map<String, dynamic>))
|
?.map((e) => FoodDataDto.fromJson(e as Map<String, dynamic>))
|
||||||
.toList(),
|
.toList(),
|
||||||
);
|
);
|
||||||
|
|
||||||
BreadDataDto _$BreadDataDtoFromJson(Map<String, dynamic> json) => BreadDataDto(
|
FoodDataDto _$FoodDataDtoFromJson(Map<String, dynamic> json) => FoodDataDto(
|
||||||
fdcId: (json['fdcId'] as num?)?.toInt(),
|
fdcId: (json['fdcId'] as num?)?.toInt(),
|
||||||
brandName: json['brandName'] as String?,
|
brandName: json['brandName'] as String?,
|
||||||
description: json['description'] as String?,
|
description: json['description'] as String?,
|
@ -1,8 +1,8 @@
|
|||||||
|
|
||||||
import 'package:labs_petrushin/Presentation/home_page/home_page.dart';
|
import 'package:labs_petrushin/Presentation/home_page/home_page.dart';
|
||||||
import 'package:labs_petrushin/data/dtos/breads_dto.dart';
|
import 'package:labs_petrushin/data/dtos/foods_dto.dart';
|
||||||
|
|
||||||
extension CharacterDataDtoToModel on BreadDataDto {
|
extension CharacterDataDtoToModel on FoodDataDto {
|
||||||
CardData toDomain() => CardData(
|
CardData toDomain() => CardData(
|
||||||
text: brandName ?? "Просто хлэп",
|
text: brandName ?? "Просто хлэп",
|
||||||
info: description ?? "Очень кусьна",
|
info: description ?? "Очень кусьна",
|
@ -1,12 +1,12 @@
|
|||||||
|
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:labs_petrushin/Presentation/home_page/home_page.dart';
|
import 'package:labs_petrushin/Presentation/home_page/home_page.dart';
|
||||||
import 'package:labs_petrushin/data/dtos/breads_dto.dart';
|
import 'package:labs_petrushin/data/dtos/foods_dto.dart';
|
||||||
import 'package:labs_petrushin/data/mappers/bread_mapper.dart';
|
import 'package:labs_petrushin/data/mappers/food_mapper.dart';
|
||||||
import 'package:labs_petrushin/repositories/api_interface.dart';
|
import 'package:labs_petrushin/repositories/api_interface.dart';
|
||||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||||
|
|
||||||
class BreadRepository extends ApiInterface {
|
class FoodRepository extends ApiInterface {
|
||||||
static final Dio _dio = Dio()
|
static final Dio _dio = Dio()
|
||||||
..interceptors.add(
|
..interceptors.add(
|
||||||
PrettyDioLogger(
|
PrettyDioLogger(
|
||||||
@ -20,12 +20,12 @@ class BreadRepository extends ApiInterface {
|
|||||||
@override
|
@override
|
||||||
Future<List<CardData>?> loadData({String? q}) async {
|
Future<List<CardData>?> loadData({String? q}) async {
|
||||||
try {
|
try {
|
||||||
final String searchQuery = q ?? 'Bread'; // Используем 'Bread' как значение по умолчанию
|
final String searchQuery = q ?? 'food'; // Используем 'food' как значение по умолчанию
|
||||||
final String url = '$_baseUrl/fdc/v1/foods/search?api_key=91xPcWwfSGljSRMuoS8IH0GP4hM9QqwwtgSzqJMw&query=$searchQuery&pageSize=10';
|
final String url = '$_baseUrl/fdc/v1/foods/search?api_key=91xPcWwfSGljSRMuoS8IH0GP4hM9QqwwtgSzqJMw&query=$searchQuery&pageSize=10';
|
||||||
|
|
||||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
|
||||||
|
|
||||||
final BreadsDto dto = BreadsDto.fromJson(response.data as Map<String, dynamic>);
|
final FoodsDto dto = FoodsDto.fromJson(response.data as Map<String, dynamic>);
|
||||||
final List<CardData>? data = dto.foods?.map((e) => e.toDomain()).toList();
|
final List<CardData>? data = dto.foods?.map((e) => e.toDomain()).toList();
|
||||||
return data;
|
return data;
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
Loading…
Reference in New Issue
Block a user