This commit is contained in:
GokaPek 2024-10-18 19:20:31 +04:00
parent a49b5eb9f8
commit ba69a8aa27
6 changed files with 125 additions and 68 deletions

View File

@ -1,6 +1,8 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:labs_petrushin/repositories/mock_repository.dart';
import '../../Presentation/detailPage.dart';
import '../../repositories/bread_repository.dart';
part '../../domain/models/card.dart';
class MyHomePage extends StatefulWidget {
@ -31,27 +33,61 @@ class _MyHomePageState extends State<MyHomePage> {
}
}
class Body extends StatelessWidget {
class Body extends StatefulWidget {
const Body({super.key});
@override
State<Body> createState() => _BodyState();
}
class _BodyState extends State<Body> {
final TextEditingController searchController = TextEditingController();
late Future<List<CardData>?> data;
final repo = BreadRepository();
@override
void initState() {
data = repo.loadData();
super.initState();
}
@override
Widget build(BuildContext context) {
final data = MockRepository().loadData();
return Center(
child: FutureBuilder<List<CardData>?>(
future: data,
builder: (context, snapshot) =>
SingleChildScrollView(
child: snapshot.hasData ? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: snapshot.data?.map((e) => MyCard.fromData(
e,
onLike: () => _showSnackBar(context),
)).toList() ?? [],
)
: const CircularProgressIndicator(),
return Column(
children: [
Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: Padding(padding: const EdgeInsets.all(12),
child: CupertinoSearchTextField(
controller: searchController,
onChanged: (search) {
setState(() {
data = repo.loadData(q: search);
});
},
)
),
),
Expanded(
child: Center(
child: FutureBuilder<List<CardData>?>(
future: data,
builder: (context, snapshot) =>
SingleChildScrollView(
child: snapshot.hasData ? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: snapshot.data?.map((e) => MyCard.fromData(
e,
onLike: () => _showSnackBar(context),
)).toList() ?? [],
)
: const CircularProgressIndicator(),
),
),
),
),
),
],
);
}

View File

@ -1,33 +0,0 @@
import 'package:json_annotation/json_annotation.dart';
part 'bread_dto.g.dart';
@JsonSerializable(createToJson: false)
class BreadsDto {
final List<BreadDataDto>? data;
const BreadsDto({this.data});
factory BreadsDto.fromJson(Map<String, dynamic> json) => _$BreadsDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class BreadDataDto {
final String? id;
final String? type;
final BreadAttributesDataDto? attributes;
const BreadDataDto({this.id, this.type, this.attributes});
factory BreadDataDto.fromJson(Map<String, dynamic> json) => _$BreadDataDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class BreadAttributesDataDto {
final String? name;
final String? info;
final String? image;
const BreadAttributesDataDto({this.name, this.info, this.image});
factory BreadAttributesDataDto.fromJson(Map<String, dynamic> json) => _$BreadAttributesDataDtoFromJson(json);
}

View File

@ -0,0 +1,28 @@
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);
}

View File

@ -1,30 +1,20 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'bread_dto.dart';
part of 'breads_dto.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
BreadsDto _$BreadsDtoFromJson(Map<String, dynamic> json) => BreadsDto(
data: (json['data'] as List<dynamic>?)
foods: (json['foods'] as List<dynamic>?)
?.map((e) => BreadDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
);
BreadDataDto _$BreadDataDtoFromJson(Map<String, dynamic> json) => BreadDataDto(
id: json['id'] as String?,
type: json['type'] as String?,
attributes: json['attributes'] == null
? null
: BreadAttributesDataDto.fromJson(
json['attributes'] as Map<String, dynamic>),
);
BreadAttributesDataDto _$BreadAttributesDataDtoFromJson(
Map<String, dynamic> json) =>
BreadAttributesDataDto(
name: json['name'] as String?,
info: json['info'] as String?,
fdcId: (json['fdcId'] as num?)?.toInt(),
brandName: json['brandName'] as String?,
description: json['description'] as String?,
image: json['image'] as String?,
);

View File

@ -1,10 +1,10 @@
import 'package:labs_petrushin/Presentation/home_page/home_page.dart';
import 'package:labs_petrushin/data/dtos/bread_dto.dart';
import 'package:labs_petrushin/data/dtos/breads_dto.dart';
extension CharacterDataDtoToModel on BreadDataDto {
CardData toDomain() => CardData(
text: attributes?.name ?? "Просто хлэп",
info: attributes?.info ?? "Очень кусьна",
urlImage: attributes?.image);
text: brandName ?? "Просто хлэп",
info: description ?? "Очень кусьна",
urlImage: image);
}

View File

@ -0,0 +1,36 @@
import 'package:dio/dio.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/mappers/bread_mapper.dart';
import 'package:labs_petrushin/repositories/api_interface.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
class BreadRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(
PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
// 91xPcWwfSGljSRMuoS8IH0GP4hM9QqwwtgSzqJMw
static const String _baseUrl = 'https://api.nal.usda.gov';
@override
Future<List<CardData>?> loadData({String? q}) async {
try {
final String searchQuery = q ?? 'Bread'; // Используем 'Bread' как значение по умолчанию
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 BreadsDto dto = BreadsDto.fromJson(response.data as Map<String, dynamic>);
final List<CardData>? data = dto.foods?.map((e) => e.toDomain()).toList();
return data;
} on DioException catch (e) {
// err
return null;
}
}
}