Готовая 5 лаба
This commit is contained in:
parent
19f937922f
commit
8083c2367f
@ -12,9 +12,17 @@ extension UserDtoToModel on UserDto {
|
||||
}
|
||||
|
||||
return CardPostData(
|
||||
name ?? "",
|
||||
description ?? "",
|
||||
isValidImageUrl(image) ? image! : "https://placehold.co/480x640/png",
|
||||
false);
|
||||
name: name ?? "",
|
||||
surname: surname ?? "",
|
||||
description: description ?? "",
|
||||
imageUrl: isValidImageUrl(image)
|
||||
? image
|
||||
: (gender! == "male"
|
||||
? "https://avatar.iran.liara.run/public/boy"
|
||||
: "https://avatar.iran.liara.run/public/girl")
|
||||
.toString(),
|
||||
isLiked: false,
|
||||
age: age,
|
||||
distance: distance);
|
||||
}
|
||||
}
|
||||
|
30
lib/data/repositories/api_user_repository.dart
Normal file
30
lib/data/repositories/api_user_repository.dart
Normal file
@ -0,0 +1,30 @@
|
||||
import 'package:pmu/data/dto/page_dto.dart';
|
||||
import 'package:pmu/data/dto/user_dto.dart';
|
||||
import 'package:pmu/data/mappers/user_mapper.dart';
|
||||
import 'package:pmu/data/repositories/api_interface.dart';
|
||||
import 'package:pmu/domain/card.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||
|
||||
class ApiUserRepository extends ApiInterface {
|
||||
static final Dio _dio = Dio()
|
||||
..interceptors.add(PrettyDioLogger(requestHeader: true, requestBody: true));
|
||||
static const String _baseUrl =
|
||||
'http://lovesearch-api.nspotapov.ru/api/users/';
|
||||
|
||||
@override
|
||||
Future<List<CardPostData>?> loadData() async {
|
||||
const String url = _baseUrl;
|
||||
final List<CardPostData> data = [];
|
||||
final Response<dynamic> response =
|
||||
await _dio.get<Map<dynamic, dynamic>>(url);
|
||||
final PageDto pageDto =
|
||||
PageDto.fromJson(response.data as Map<String, dynamic>);
|
||||
int itemsCount = pageDto.itemsCount ?? 0;
|
||||
for (int i = 0; i < itemsCount; i++) {
|
||||
final CardPostData cardPost = pageDto.items![i].toDomain();
|
||||
data.add(cardPost);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
@ -5,70 +5,6 @@ class MockUserRepository extends ApiInterface {
|
||||
@override
|
||||
Future<List<CardPostData>?> loadData() async {
|
||||
return [
|
||||
const CardPostData(
|
||||
name: "Марьяна",
|
||||
surname: "Ро",
|
||||
description: "Люблю вечерами погамать в майн",
|
||||
imageUrl: "https://placehold.co/600x400/png",
|
||||
age: 21,
|
||||
distance: 24.5,
|
||||
isLiked: false),
|
||||
const CardPostData(
|
||||
name: "Константин",
|
||||
surname: "Злобин",
|
||||
description: "Веду канал в тг про криптожизнь",
|
||||
imageUrl: "https://placehold.co/600x400/png",
|
||||
age: 24,
|
||||
distance: 478.3,
|
||||
isLiked: false),
|
||||
const CardPostData(
|
||||
name: "Марьяна",
|
||||
surname: "Ро",
|
||||
description: "Люблю вечерами погамать в майн",
|
||||
imageUrl: "https://placehold.co/600x400/png",
|
||||
age: 21,
|
||||
distance: 24.5,
|
||||
isLiked: false),
|
||||
const CardPostData(
|
||||
name: "Константин",
|
||||
surname: "Злобин",
|
||||
description: "Веду канал в тг про криптожизнь",
|
||||
imageUrl: "https://placehold.co/600x400/png",
|
||||
age: 24,
|
||||
distance: 478.3,
|
||||
isLiked: false),
|
||||
const CardPostData(
|
||||
name: "Марьяна",
|
||||
surname: "Ро",
|
||||
description: "Люблю вечерами погамать в майн",
|
||||
imageUrl: "https://placehold.co/600x400/png",
|
||||
age: 21,
|
||||
distance: 24.5,
|
||||
isLiked: false),
|
||||
const CardPostData(
|
||||
name: "Константин",
|
||||
surname: "Злобин",
|
||||
description: "Веду канал в тг про криптожизнь",
|
||||
imageUrl: "https://placehold.co/600x400/png",
|
||||
age: 24,
|
||||
distance: 478.3,
|
||||
isLiked: false),
|
||||
const CardPostData(
|
||||
name: "Марьяна",
|
||||
surname: "Ро",
|
||||
description: "Люблю вечерами погамать в майн",
|
||||
imageUrl: "https://placehold.co/600x400/png",
|
||||
age: 21,
|
||||
distance: 24.5,
|
||||
isLiked: false),
|
||||
const CardPostData(
|
||||
name: "Константин",
|
||||
surname: "Злобин",
|
||||
description: "Веду канал в тг про криптожизнь",
|
||||
imageUrl: "https://placehold.co/600x400/png",
|
||||
age: 24,
|
||||
distance: 478.3,
|
||||
isLiked: false),
|
||||
const CardPostData(
|
||||
name: "Марьяна",
|
||||
surname: "Ро",
|
||||
|
@ -1,8 +1,18 @@
|
||||
class CardPostData {
|
||||
final String name;
|
||||
final String description;
|
||||
final String? name;
|
||||
final String? surname;
|
||||
final String? description;
|
||||
final String? imageUrl;
|
||||
final bool isLiked;
|
||||
final int? age;
|
||||
final double? distance;
|
||||
final bool? isLiked;
|
||||
|
||||
const CardPostData(this.name, this.description, this.imageUrl, this.isLiked);
|
||||
const CardPostData(
|
||||
{this.name,
|
||||
this.surname,
|
||||
this.description,
|
||||
this.imageUrl,
|
||||
this.isLiked,
|
||||
this.age,
|
||||
this.distance});
|
||||
}
|
||||
|
@ -25,14 +25,14 @@ class DetailPage extends StatelessWidget {
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(4.0),
|
||||
child: Text(
|
||||
data.name,
|
||||
data.name ?? "",
|
||||
style: const TextStyle(color: Colors.black, fontSize: 30),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(4.0),
|
||||
child: Text(
|
||||
data.description,
|
||||
data.description ?? "",
|
||||
style: const TextStyle(color: Colors.black, fontSize: 20),
|
||||
),
|
||||
),
|
||||
|
@ -9,21 +9,34 @@ const double NORMAL_ICON_SCALE = 2.0;
|
||||
const double SCALED_ICON_SCALE = 2.5;
|
||||
|
||||
class CardPost extends StatefulWidget {
|
||||
final String description;
|
||||
final String? description;
|
||||
final String? imageUrl;
|
||||
final bool isLiked;
|
||||
final String name;
|
||||
final bool? isLiked;
|
||||
final String? name;
|
||||
final String? surname;
|
||||
|
||||
final OnLikeCallback onLike;
|
||||
final VoidCallback? onTap;
|
||||
|
||||
const CardPost(this.name, this.description, this.imageUrl, this.isLiked,
|
||||
this.onLike, this.onTap);
|
||||
const CardPost(
|
||||
{this.name,
|
||||
this.surname,
|
||||
this.description,
|
||||
this.imageUrl,
|
||||
this.isLiked,
|
||||
this.onLike,
|
||||
this.onTap});
|
||||
|
||||
factory CardPost.fromData(CardPostData data,
|
||||
{OnLikeCallback onLike, VoidCallback? onTap}) =>
|
||||
CardPost(data.name, data.description, data.imageUrl, data.isLiked, onLike,
|
||||
onTap);
|
||||
CardPost(
|
||||
name: data.name,
|
||||
surname: data.surname,
|
||||
description: data.description,
|
||||
imageUrl: data.imageUrl,
|
||||
isLiked: data.isLiked,
|
||||
onLike: onLike,
|
||||
onTap: onTap);
|
||||
|
||||
@override
|
||||
State<CardPost> createState() => _CardPostState();
|
||||
@ -67,7 +80,7 @@ class _CardPostState extends State<CardPost> {
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text(
|
||||
widget.name,
|
||||
(widget.name ?? "") + (" ") + (widget.surname ?? ""),
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 30,
|
||||
@ -83,7 +96,7 @@ class _CardPostState extends State<CardPost> {
|
||||
children: [
|
||||
Flexible(
|
||||
child: Text(
|
||||
widget.description,
|
||||
widget.description ?? "",
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
@ -118,7 +131,7 @@ class _CardPostState extends State<CardPost> {
|
||||
iconScale = NORMAL_ICON_SCALE;
|
||||
});
|
||||
});
|
||||
widget.onLike?.call(widget.name, isLiked);
|
||||
widget.onLike?.call(widget.name ?? "", isLiked);
|
||||
})
|
||||
},
|
||||
child: AnimatedScale(
|
||||
|
@ -1,5 +1,6 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pmu/data/repositories/api_user_repository.dart';
|
||||
import 'package:pmu/domain/card.dart';
|
||||
import 'package:pmu/presentation/detail_pages/card_detail_page.dart';
|
||||
import 'package:pmu/presentation/home_page/card.dart';
|
||||
@ -42,10 +43,7 @@ class Body extends StatelessWidget {
|
||||
content: Text(
|
||||
'${isLiked ? 'Вы поставили лайк: ' : 'Вы убрали лайк: '} $title',
|
||||
style: const TextStyle(
|
||||
color: Colors.black45,
|
||||
fontSize: 20,
|
||||
fontWeight: FontWeight.bold
|
||||
),
|
||||
color: Colors.black45, fontSize: 20, fontWeight: FontWeight.bold),
|
||||
),
|
||||
backgroundColor: isLiked ? Colors.green : Colors.red,
|
||||
duration: const Duration(seconds: 2),
|
||||
@ -55,32 +53,24 @@ class Body extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final data = [
|
||||
const CardPostData(
|
||||
"Евгения",
|
||||
"Люблю программирование и кататься на скейте",
|
||||
"https://avatar.iran.liara.run/public/girl",
|
||||
false),
|
||||
const CardPostData("Алекс", "Junior Flutter Engineer",
|
||||
"https://avatar.iran.liara.run/public/36", false),
|
||||
const CardPostData(
|
||||
"Станислав",
|
||||
"Нет ничего приятнее прогулки на природе!",
|
||||
"https://avatar.iran.liara.run/public/boy",
|
||||
false),
|
||||
const CardPostData("Вероника", "В свободное время хожу на уроки вокала",
|
||||
"https://avatar.iran.liara.run/public/93", false),
|
||||
];
|
||||
final data = ApiUserRepository().loadData();
|
||||
return Center(
|
||||
child: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: data.map((data) {
|
||||
return CardPost.fromData(data,
|
||||
onLike: (String title, bool isLiked) =>
|
||||
_showSnackBar(context, title, isLiked),
|
||||
onTap: () => _navToDetails(context, data));
|
||||
}).toList(),
|
||||
)));
|
||||
child: FutureBuilder<List<CardPostData>?>(
|
||||
future: data,
|
||||
builder: (context, snapshot) => SingleChildScrollView(
|
||||
child: snapshot.hasData
|
||||
? Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: snapshot.data
|
||||
?.map((data) => CardPost.fromData(data,
|
||||
onLike: (String title, bool isLiked) =>
|
||||
_showSnackBar(context, title, isLiked),
|
||||
onTap: () => _navToDetails(context, data)))
|
||||
.toList() ??
|
||||
[])
|
||||
: const CircularProgressIndicator(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user