лаба 5 почти готова

This commit is contained in:
Полина Чубыкина 2024-10-07 18:19:03 +04:00
parent 697a2b68ff
commit c9736aff2d
5 changed files with 89 additions and 97 deletions

View File

@ -13,23 +13,24 @@ class CharactersDto {
@JsonSerializable(createToJson: false) @JsonSerializable(createToJson: false)
class CharacterDataDto { class CharacterDataDto {
final String? id; final String? id;
final String? type; final String? type;
final CharacterAttributesDataDto? attributes; final CharacterAttributesDataDto? attributes;
const CharacterDataDto({this.id, this.type, this.attributes}); const CharacterDataDto({this.id, this.type, this.attributes});
factory CharacterDataDto.fromJson(Map<String, dynamic> json) => _$CharacterDataDtoFromJson(json); factory CharacterDataDto.fromJson(Map<String, dynamic> json) => _$CharacterDataDtoFromJson(json);
} }
@JsonSerializable(createToJson: false) @JsonSerializable(createToJson: false)
class CharacterAttributesDataDto { class CharacterAttributesDataDto {
final String? name; final String? name;
final String? born; final String? born;
final String? died; final String? died;
final String? image; final String? image;
const CharacterAttributesDataDto({this.name, this.born, this.died, this.image}); const CharacterAttributesDataDto({this.name, this.born, this.died, this.image});
factory CharacterAttributesDataDto.fromJson(Map<String, dynamic> json) => _$CharacterAttributesDataDtoFromJson(json); factory CharacterAttributesDataDto.fromJson(Map<String, dynamic> json) =>
} _$CharacterAttributesDataDtoFromJson(json);
}

View File

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

View File

@ -4,28 +4,27 @@ import 'package:mobilki_lab1/domain/models/card.dart';
class MockRepository extends ApiInterface { class MockRepository extends ApiInterface {
@override @override
Future<List<CardData>?> loadData(String? q) async { Future<List<CardData>?> loadData({OnErrorCallback? onError}) async {
return [ return [
CardData( CardData(
'Freeze', 'Freeze',
descriptionText: 'so cold..', descriptionText: 'so cold..',
imageUrl: imageUrl:
'https://www.skedaddlewildlife.com/wp-content/uploads/2018/09/depositphotos_22425309-stock-photo-a-lonely-raccoon-in-winter.jpg', 'https://www.skedaddlewildlife.com/wp-content/uploads/2018/09/depositphotos_22425309-stock-photo-a-lonely-raccoon-in-winter.jpg',
), ),
CardData( CardData(
'Hi', 'Hi',
descriptionText: 'pretty face', descriptionText: 'pretty face',
icon: Icons.hail, icon: Icons.hail,
imageUrl: imageUrl:
'https://www.thesprucepets.com/thmb/nKNaS4I586B_H7sEUw9QAXvWM_0=/2121x0/filters:no_upscale():strip_icc()/GettyImages-135630198-5ba7d225c9e77c0050cff91b.jpg', 'https://www.thesprucepets.com/thmb/nKNaS4I586B_H7sEUw9QAXvWM_0=/2121x0/filters:no_upscale():strip_icc()/GettyImages-135630198-5ba7d225c9e77c0050cff91b.jpg',
), ),
CardData( CardData(
'Orange', 'Orange',
descriptionText: 'I like autumn', descriptionText: 'I like autumn',
icon: Icons.warning_amber, icon: Icons.warning_amber,
imageUrl: imageUrl: 'https://furmanagers.com/wp-content/uploads/2019/11/dreamstime_l_22075357.jpg',
'https://furmanagers.com/wp-content/uploads/2019/11/dreamstime_l_22075357.jpg',
), ),
]; ];
} }
} }

View File

@ -14,21 +14,21 @@ class PotterRepository extends ApiInterface {
static const String _baseUrl = 'https://api.potterdb.com'; static const String _baseUrl = 'https://api.potterdb.com';
@override @override
Future<List<CardData>?> loadData(String? q) async{ Future<List<CardData>?> loadData({String? q, OnErrorCallback? onError}) async {
try{ try {
const String url = '$_baseUrl/v1/characters'; const String url = '$_baseUrl/v1/characters';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>( final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url, url,
queryParameters: q != null ? {'filter[name_cont]' : q} : null, queryParameters: q != null ? {'filter[name_cont]': q} : null,
); );
final CharactersDto dto = CharactersDto.fromJson(response.data as Map<String, dynamic>); final CharactersDto dto = CharactersDto.fromJson(response.data as Map<String, dynamic>);
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList(); final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
return data; return data;
} on DioException catch (e){ } on DioException catch (e) {
onError?.call(e.response?.statusMessage);
return null; return null;
} }
} }

View File

@ -1,8 +1,8 @@
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobilki_lab1/data/repositories/potter_repository.dart';
import 'package:mobilki_lab1/domain/models/card.dart'; import 'package:mobilki_lab1/domain/models/card.dart';
import 'package:mobilki_lab1/presentation/details_page/details_page.dart';
import '../details_page/details_page.dart';
part 'card.dart'; part 'card.dart';
@ -16,87 +16,77 @@ class MyHomePage extends StatefulWidget {
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
final Color _color = Colors.brown.shade300; @override
Widget build(BuildContext context) {
return const Scaffold(body: Body());
}
}
class Body extends StatefulWidget {
const Body({super.key});
@override
State<Body> createState() => _BodyState();
}
class _BodyState extends State<Body> {
final searchController = TextEditingController();
late Future<List<CardData>?> data;
final repo = PotterRepository();
@override @override
void initState() { void initState() {
data = repo.loadData();
super.initState(); super.initState();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Padding(
appBar: AppBar( padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
backgroundColor: _color, child: Column(
title: Text(widget.title), children: [
), Padding(
body: const Body(), padding: const EdgeInsets.all(12),
); child: CupertinoSearchTextField(
} controller: searchController,
} onChanged: (search) {
setState(() {
class Body extends StatelessWidget { data = repo.loadData(q: search);
const Body({super.key}); });
},
@override ),
Widget build(BuildContext context) { ),
final data = [ Expanded(
CardData( child: Center(
'кот', child: FutureBuilder<List<CardData>?>(
descriptionText: 'глупенький', future: data,
imageUrl: builder: (context, snapshot) => SingleChildScrollView(
'https://i.pinimg.com/564x/98/74/75/98747559040c03402a6d9ad4e47152c6.jpg', child: snapshot.hasData
), ? Column(
CardData( mainAxisAlignment: MainAxisAlignment.center,
'сырный кот', children: snapshot.data?.map((data) {
descriptionText: 'он не виноват', return _Card.fromData(
imageUrl: data,
'https://i.pinimg.com/564x/e7/c5/59/e7c559e449cd0bb5370d7740b57daa1f.jpg', onLike: (String title, bool isLiked) =>
), _showSnackBar(context, title, isLiked),
CardData( onTap: () => _navToDetails(context, data),
'злой кот', );
descriptionText: 'я бы ему не верила', }).toList() ??
imageUrl: [],
'https://i.pinimg.com/736x/27/cf/5e/27cf5e3abe1795ce04095941902c61ca.jpg', )
), : const CircularProgressIndicator(),
CardData( ),
'умный кот', ),
descriptionText: 'он только делает вид что умный', ),
imageUrl: ),
'https://i.pinimg.com/736x/e9/24/80/e924801430b813eee2635b40fe6e11d6.jpg', ],
),
CardData(
'кот в шляпе',
descriptionText: '我喜欢橘子!',
imageUrl:
'https://i.pinimg.com/564x/3e/ed/11/3eed1103c548ed7c46a1e37a2695f53a.jpg',
),
CardData(
'котенок',
descriptionText: 'я его очень понимаю',
imageUrl:
'https://i.pinimg.com/736x/38/9f/e6/389fe61133f036fc980a6b2e7abb2557.jpg',
),
];
return Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: data.map((data) {
return _Card.fromData(
data,
onLike: (String title, bool isLiked) =>
_showSnackBar(context, title, isLiked),
onTap: () => _navToDetails(context, data),
);
}).toList(),
),
), ),
); );
} }
void _navToDetails(BuildContext context, CardData data){ void _navToDetails(BuildContext context, CardData data) {
Navigator.push( Navigator.push(
context, context,
CupertinoPageRoute(builder: (context) => DetailsPage(data)), CupertinoPageRoute(builder: (context) => DetailsPage(data)),
@ -107,10 +97,10 @@ class Body extends StatelessWidget {
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar( ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text( content: Text(
'Вам ${isLiked ? 'понравился' : 'больше не нравится'} $title', '$title ${isLiked ? 'liked!' : 'disliked :('}',
style: Theme.of(context).textTheme.bodyLarge, style: Theme.of(context).textTheme.bodyLarge,
), ),
backgroundColor: Colors.brown.shade400, backgroundColor: Colors.orangeAccent,
duration: const Duration(seconds: 1), duration: const Duration(seconds: 1),
)); ));
}); });