lab-5 in state "pu pu pu..."
This commit is contained in:
parent
6b8d71fc3d
commit
55ec5f5ecc
36
lib/data/dtos/potions_dto.dart
Normal file
36
lib/data/dtos/potions_dto.dart
Normal file
@ -0,0 +1,36 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
|
||||
part 'potions_dto.g.dart';
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PotionsDto {
|
||||
final List<PotionDataDto>? data;
|
||||
|
||||
const PotionsDto({this.data});
|
||||
|
||||
factory PotionsDto.fromJson(Map<String, dynamic> json) => _$PotionsDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PotionDataDto {
|
||||
final String? id;
|
||||
final String? type;
|
||||
final PotionAttributesDataDto? attributes;
|
||||
|
||||
const PotionDataDto({this.id, this.type, this.attributes});
|
||||
|
||||
factory PotionDataDto.fromJson(Map<String, dynamic> json) => _$PotionDataDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PotionAttributesDataDto {
|
||||
final String? name;
|
||||
final String? characteristics;
|
||||
final String? effect;
|
||||
final String? image;
|
||||
|
||||
const PotionAttributesDataDto({this.name, this.characteristics, this.effect, this.image});
|
||||
|
||||
factory PotionAttributesDataDto.fromJson(Map<String, dynamic> json) =>
|
||||
_$PotionAttributesDataDtoFromJson(json);
|
||||
}
|
32
lib/data/dtos/potions_dto.g.dart
Normal file
32
lib/data/dtos/potions_dto.g.dart
Normal file
@ -0,0 +1,32 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'potions_dto.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
PotionsDto _$PotionsDtoFromJson(Map<String, dynamic> json) => PotionsDto(
|
||||
data: (json['data'] as List<dynamic>?)
|
||||
?.map((e) => PotionDataDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
|
||||
PotionDataDto _$PotionDataDtoFromJson(Map<String, dynamic> json) =>
|
||||
PotionDataDto(
|
||||
id: json['id'] as String?,
|
||||
type: json['type'] as String?,
|
||||
attributes: json['attributes'] == null
|
||||
? null
|
||||
: PotionAttributesDataDto.fromJson(
|
||||
json['attributes'] as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
PotionAttributesDataDto _$PotionAttributesDataDtoFromJson(
|
||||
Map<String, dynamic> json) =>
|
||||
PotionAttributesDataDto(
|
||||
name: json['name'] as String?,
|
||||
characteristics: json['characteristics'] as String?,
|
||||
effect: json['effect'] as String?,
|
||||
image: json['image'] as String?,
|
||||
);
|
23
lib/data/mappers/potions_mapper.dart
Normal file
23
lib/data/mappers/potions_mapper.dart
Normal file
@ -0,0 +1,23 @@
|
||||
import 'package:leonteva_pmu/data/dtos/potions_dto.dart';
|
||||
import 'package:leonteva_pmu/domain/models/card.dart';
|
||||
|
||||
const String imagePlaceholder =
|
||||
'https://cdn-icons-png.flaticon.com/512/4036/4036418.png';
|
||||
|
||||
extension PotionDataDtoToModel on PotionDataDto {
|
||||
CardData toDomain() => CardData(
|
||||
attributes?.name ?? 'UNKNOWN',
|
||||
imageUrl: attributes?.image ?? imagePlaceholder,
|
||||
descriptionText: _makeDescriptionText(attributes?.characteristics, attributes?.effect),
|
||||
);
|
||||
|
||||
String _makeDescriptionText(String? characteristics, String? effect) {
|
||||
return characteristics != null && effect != null
|
||||
? '$characteristics - $effect'
|
||||
: characteristics != null
|
||||
? 'characteristics: $characteristics'
|
||||
: effect != null
|
||||
? 'effect: $effect'
|
||||
: '';
|
||||
}
|
||||
}
|
7
lib/data/repositories/api_interface.dart
Normal file
7
lib/data/repositories/api_interface.dart
Normal file
@ -0,0 +1,7 @@
|
||||
import 'package:leonteva_pmu/domain/models/card.dart';
|
||||
|
||||
typedef OnErrorCallback = void Function(String? error);
|
||||
|
||||
abstract class ApiInterface {
|
||||
Future<List<CardData>?> loadData({OnErrorCallback? onError});
|
||||
}
|
30
lib/data/repositories/mock_repository.dart
Normal file
30
lib/data/repositories/mock_repository.dart
Normal file
@ -0,0 +1,30 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:leonteva_pmu/data/repositories/api_interface.dart';
|
||||
import 'package:leonteva_pmu/domain/models/card.dart';
|
||||
|
||||
class MockRepository extends ApiInterface {
|
||||
@override
|
||||
Future<List<CardData>?> loadData({OnErrorCallback? onError}) async {
|
||||
return [
|
||||
CardData(
|
||||
'dish1',
|
||||
descriptionText: 'hehehe',
|
||||
imageUrl:
|
||||
'https://n1s2.hsmedia.ru/48/2d/63/482d63d02b668677a73a2ffbd791a71b/728x546_1_aaca034dfa8a8c33247bd8cb2ed26817@1700x1275_0xac120003_9749770561671744766.jpeg',
|
||||
),
|
||||
CardData(
|
||||
'dish2',
|
||||
descriptionText: 'eeee',
|
||||
icon: Icons.hail,
|
||||
imageUrl:
|
||||
'https://n1s2.hsmedia.ru/48/2d/63/482d63d02b668677a73a2ffbd791a71b/728x546_1_aaca034dfa8a8c33247bd8cb2ed26817@1700x1275_0xac120003_9749770561671744766.jpeg',
|
||||
),
|
||||
CardData(
|
||||
'dish3',
|
||||
descriptionText: 'aaaaaa',
|
||||
icon: Icons.warning_amber,
|
||||
imageUrl: 'https://n1s2.hsmedia.ru/48/2d/63/482d63d02b668677a73a2ffbd791a71b/728x546_1_aaca034dfa8a8c33247bd8cb2ed26817@1700x1275_0xac120003_9749770561671744766.jpeg',
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
35
lib/data/repositories/potter_repository.dart
Normal file
35
lib/data/repositories/potter_repository.dart
Normal file
@ -0,0 +1,35 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:leonteva_pmu/data/dtos/potions_dto.dart';
|
||||
import 'package:leonteva_pmu/data/mappers/potions_mapper.dart';
|
||||
import 'package:leonteva_pmu/data/repositories/api_interface.dart';
|
||||
import 'package:leonteva_pmu/domain/models/card.dart';
|
||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||
|
||||
class PotterRepository extends ApiInterface {
|
||||
static final Dio _dio = Dio()
|
||||
..interceptors.add(PrettyDioLogger(
|
||||
requestHeader: true,
|
||||
requestBody: true,
|
||||
));
|
||||
|
||||
static const String _baseUrl = 'https://api.potterdb.com';
|
||||
|
||||
@override
|
||||
Future<List<CardData>?> loadData({String? q, OnErrorCallback? onError}) async {
|
||||
try {
|
||||
const String url = '$_baseUrl/v1/potions';
|
||||
|
||||
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||||
url,
|
||||
queryParameters: q != null ? {'filter[name_cont]': q} : null,
|
||||
);
|
||||
|
||||
final PotionsDto dto = PotionsDto.fromJson(response.data as Map<String, dynamic>);
|
||||
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
|
||||
return data;
|
||||
} on DioException catch (e) {
|
||||
onError?.call(e.response?.statusMessage);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:leonteva_pmu/data/repositories/potter_repository.dart';
|
||||
import 'package:leonteva_pmu/presentation/details_page/details_page.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:leonteva_pmu/domain/models/card.dart';
|
||||
import 'package:leonteva_pmu/presentation/dialogs/show_dialog.dart';
|
||||
|
||||
part 'card.dart';
|
||||
|
||||
@ -15,21 +17,13 @@ class HomePage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> {
|
||||
final Color _color = Colors.orangeAccent;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
backgroundColor: _color,
|
||||
title: Text(widget.title),
|
||||
),
|
||||
body: const Body(),
|
||||
);
|
||||
return const Scaffold(body: Body());
|
||||
}
|
||||
}
|
||||
|
||||
class Body extends StatelessWidget {
|
||||
class Body extends StatefulWidget {
|
||||
const Body({super.key}); // ключи
|
||||
|
||||
@override
|
||||
|
@ -99,6 +99,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.1.1"
|
||||
json_annotation:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: json_annotation
|
||||
sha256: "1ce844379ca14835a50d2f019a3099f419082cfdd231cd86a142af94dd5c6bb1"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.9.0"
|
||||
leak_tracker:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -30,6 +30,7 @@ environment:
|
||||
dependencies:
|
||||
dio: ^5.7.0
|
||||
pretty_dio_logger: ^1.4.0
|
||||
json_annotation: ^4.9.0
|
||||
flutter:
|
||||
sdk: flutter
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user