This commit is contained in:
Ivan 2024-11-27 12:33:41 +04:00
parent 67bc09caad
commit 71b375ff79
11 changed files with 220 additions and 48 deletions

View File

@ -0,0 +1,25 @@
import 'package:json_annotation/json_annotation.dart';
part 'PlaceDTO.g.dart';
@JsonSerializable(createToJson: false)
class PlacesDto {
final List<PlaceDataDto>? data;
const PlacesDto({this.data});
factory PlacesDto.fromJson(Map<String, dynamic> json) => _$PlacesDtoFromJson(json);
}
@JsonSerializable(createToJson: false)
class PlaceDataDto {
final int? id;
final String? name;
final int? yearBuilt;
final String? description;
final String? imageData;
const PlaceDataDto({this.id, this.name, this.yearBuilt, this.description, this.imageData});
factory PlaceDataDto.fromJson(Map<String, dynamic> json) => _$PlaceDataDtoFromJson(json);
}

View File

@ -0,0 +1,21 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'PlaceDTO.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
PlacesDto _$PlacesDtoFromJson(Map<String, dynamic> json) => PlacesDto(
data: (json['data'] as List<dynamic>?)
?.map((e) => PlaceDataDto.fromJson(e as Map<String, dynamic>))
.toList(),
);
PlaceDataDto _$PlaceDataDtoFromJson(Map<String, dynamic> json) => PlaceDataDto(
id: (json['id'] as num?)?.toInt(),
name: json['name'] as String?,
yearBuilt: (json['yearBuilt'] as num?)?.toInt(),
description: json['description'] as String?,
imageData: json['imageData'] as String?,
);

View File

@ -0,0 +1,19 @@
import 'dart:convert';
import '../../models/CardData.dart';
import '../dtos/PlaceDTO.dart';
extension PlaceDataDtoToModel on PlaceDataDto {
CardData toDomain() => CardData(
name ?? 'Неизвестное имя',
//imageUrl: attributes?.image,
descriptionText: getText(),
//bytes: (imagedata != null) ? base64.decode(imagedata!) : null,
bytes: imageData?? null,
);
String getText(){
final String str1 = description ?? "Описания нет";
final String str2 = yearBuilt?.toString() ?? "Даты построики нет";
return "$str1\n$str2 год";
}
}

View File

@ -0,0 +1,36 @@
import 'dart:ui';
import 'package:pmu_new/data/repositories/api_Interface.dart';
import '../../models/CardData.dart';
class MockRepository extends ApiInterface{
@override
Future<List<CardData>?> loadData({OnErrorCallback? onError}) async{
final data = [
CardData(
'Фрукты',
descriptionText:
'Какие же они полезные и сладкие!!!! Generate Lorem Ipsum placeholder text for use in your graphic, print and web layouts, and discover plugins for your favorite writing, design and blogging tools',
imageUrl:
'https://media.gettyimages.com/id/182810893/photo/fruit-mix.jpg?s=612x612&w=0&k=20&c=v9jopDXbS5LCXY1d8uSwEldLJVVkOpYtYtyHD8azWDU=',
),
CardData(
'Киви',
descriptionText:
'сладкий и спелый, можно купить по акции прямо сейчас звоните не пожалеете',
Backgroundcolor: Color.fromARGB(10, 210, 30, 40),
imageUrl:
'https://www.diyphotography.net/files/images/3/pictures-of-sliced-fruits-09b.jpg',
),
CardData(
'Банан',
descriptionText: 'Ого, че с ним произошло',
Backgroundcolor: Color.fromARGB(30, 30, 210, 15),
imageUrl:
'https://www.diyphotography.net/files/images/3/pictures-of-sliced-fruits-01.jpg',
),
];
return data;
}
}

View File

@ -0,0 +1,38 @@
import 'dart:developer';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:pmu_new/data/mappers/PlaceDataDtoToModel.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import '../../models/CardData.dart';
import '../dtos/PlaceDTO.dart';
import 'api_Interface.dart';
class PlacesRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
static const String _baseUrl = 'https://localhost:5001';
@override
Future<List<CardData>?> loadData({int? q, OnErrorCallback? onError}) async {
try {
const String url = '$_baseUrl/api/Main/GetManufactureList';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: q != null ? {'year': q} : null,
);
final PlacesDto dto = PlacesDto.fromJson(response.data as Map<String, dynamic>);
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
//debugPrint("length: ${data?.length}");
return data;
} on DioException catch (e) {
onError?.call(e.response?.statusMessage);
return null;
}
}
}

View File

@ -0,0 +1,7 @@
import '../../models/CardData.dart';
typedef OnErrorCallback = void Function(String? error);
abstract class ApiInterface {
Future<List<CardData>?> loadData({OnErrorCallback? onError});
}

View File

@ -1,3 +1,5 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
class CardData {
@ -5,13 +7,13 @@ class CardData {
final String descriptionText;
final Color Backgroundcolor;
final String? imageUrl;
final void Function(String title, bool isliked)? onlike;
final String? bytes;
CardData(
this.text, {
required this.descriptionText,
this.Backgroundcolor = const Color.fromARGB(70, 173, 216, 230),
this.imageUrl,
this.onlike
this.imageUrl = 'https://simbircity.ru/wp-content/uploads/2020/10/1IMG_36222.jpg',
this.bytes = null,
});
}

View File

@ -1,13 +1,18 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:pmu_new/models/CardData.dart';
class MyCard extends StatefulWidget {
final CardData cardData;
final VoidCallback onTap;
final void Function(String title, bool isliked)? onlike;
const MyCard({
required this.cardData,
required this.onTap,
this.onlike
});
@override
@ -53,7 +58,9 @@ class _CardState extends State<MyCard> {
child: SizedBox(
height: 240,
width: 300,
child: Image.network(
child: this.widget.cardData.bytes != null?
Image.memory(base64Decode(this.widget.cardData.bytes!)) :
Image.network(
this.widget.cardData.imageUrl ?? '',
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(),
@ -107,7 +114,7 @@ class _CardState extends State<MyCard> {
isliked = !isliked;
turns += 1.0;
});
widget.cardData.onlike?.call(widget.cardData.text, isliked);
widget.onlike?.call(widget.cardData.text, isliked);
},
child: Icon(
isliked ? Icons.favorite : Icons.favorite_border,

View File

@ -1,8 +1,10 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:pmu_new/data/repositories/PlacesRepository.dart';
import 'package:pmu_new/presentation/home_page/MyCard.dart';
import 'package:pmu_new/models/CardData.dart';
import '../../data/repositories/MockRepository.dart';
import '../details_page/details_page.dart';
class MyHomePage extends StatefulWidget {
@ -17,12 +19,14 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
backgroundColor: Theme
.of(context)
.colorScheme
.inversePrimary,
title: Text(widget.title),
),
body: const CardWidget());
@ -38,7 +42,10 @@ class CardWidget extends StatelessWidget {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(
'$title ${isLiked ? 'Понравилось!' : 'Больше не нравится'}',
style: Theme.of(context).textTheme.bodyLarge,
style: Theme
.of(context)
.textTheme
.bodyLarge,
),
backgroundColor: Colors.greenAccent,
duration: const Duration(seconds: 1),
@ -46,49 +53,33 @@ class CardWidget extends StatelessWidget {
});
}
@override
Widget build(BuildContext context) {
final data = [
CardData(
'Фрукты',
descriptionText:
'Какие же они полезные и сладкие!!!! Generate Lorem Ipsum placeholder text for use in your graphic, print and web layouts, and discover plugins for your favorite writing, design and blogging tools',
imageUrl:
'https://media.gettyimages.com/id/182810893/photo/fruit-mix.jpg?s=612x612&w=0&k=20&c=v9jopDXbS5LCXY1d8uSwEldLJVVkOpYtYtyHD8azWDU=',
onlike: (name, state) => _showSnackBar(context, name, state),
),
CardData(
'Киви',
descriptionText:
'сладкий и спелый, можно купить по акции прямо сейчас звоните не пожалеете',
Backgroundcolor: Color.fromARGB(10, 210, 30, 40),
imageUrl:
'https://www.diyphotography.net/files/images/3/pictures-of-sliced-fruits-09b.jpg',
onlike: (name, state) => _showSnackBar(context, name, state),
),
CardData(
'Банан',
descriptionText: 'Ого, че с ним произошло',
Backgroundcolor: Color.fromARGB(30, 30, 210, 15),
imageUrl:
'https://www.diyphotography.net/files/images/3/pictures-of-sliced-fruits-01.jpg',
onlike: (name, state) => _showSnackBar(context, name, state),
),
];
final data = PlacesRepository().loadData(q: 1780);
return Center(
child: SingleChildScrollView(
child: Column(
child: FutureBuilder<List<CardData>?>(
future: data,
builder: (context, snapshot) =>
SingleChildScrollView(
child: snapshot.hasData ?
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: data.map((e) => MyCard(cardData: e, onTap: () {
children: snapshot.data?.map((e) {
return MyCard(
cardData: e,
onlike: (title, state) => _showSnackBar(context, title, state),
onTap: () {
Navigator.push(
context,
CupertinoPageRoute(
builder: (context) => DetailsPage(e)),
);
})).toList(),
),
}
);
}).toList() ?? [],
)
: const CircularProgressIndicator()
)
),
);
}

View File

@ -182,6 +182,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.7"
dio:
dependency: "direct main"
description:
name: dio
sha256: "5598aa796bbf4699afd5c67c0f5f6e2ed542afc956884b9cd58c306966efc260"
url: "https://pub.dev"
source: hosted
version: "5.7.0"
dio_web_adapter:
dependency: transitive
description:
name: dio_web_adapter
sha256: "33259a9276d6cea88774a0000cfae0d861003497755969c92faa223108620dc8"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
fake_async:
dependency: transitive
description:
@ -400,6 +416,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.1"
pretty_dio_logger:
dependency: "direct main"
description:
name: pretty_dio_logger
sha256: "36f2101299786d567869493e2f5731de61ce130faa14679473b26905a92b6407"
url: "https://pub.dev"
source: hosted
version: "1.4.0"
pub_semver:
dependency: transitive
description:

View File

@ -38,6 +38,8 @@ dependencies:
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.8
json_annotation: ^4.8.1
dio: ^5.4.2+1
pretty_dio_logger: ^1.3.1
dev_dependencies:
flutter_test:
sdk: flutter