From f021d050ef57f70a8617a31fe2b13590fb56314a Mon Sep 17 00:00:00 2001 From: Serxiolog Date: Mon, 18 Nov 2024 16:30:59 +0400 Subject: [PATCH] Forgot about search --- lib/data/repositories/anime_repository.dart | 36 ++++++------ lib/presentation/home_page/home_page.dart | 63 +++++++++++++++------ 2 files changed, 65 insertions(+), 34 deletions(-) diff --git a/lib/data/repositories/anime_repository.dart b/lib/data/repositories/anime_repository.dart index 3decdb0..a9da67a 100644 --- a/lib/data/repositories/anime_repository.dart +++ b/lib/data/repositories/anime_repository.dart @@ -1,4 +1,3 @@ - import 'package:dio/dio.dart'; import 'package:first_project/data/dtos/anime_dto.dart'; import 'package:first_project/data/mappers/anime_mapper.dart'; @@ -8,26 +7,31 @@ import 'package:pretty_dio_logger/pretty_dio_logger.dart'; class AnimeRepository extends ApiInterface { static final Dio _dio = Dio() - ..interceptors.add(PrettyDioLogger( - requestHeader: true, - requestBody: true, - )); + ..interceptors.add(PrettyDioLogger( + requestHeader: true, + requestBody: true, + )); static const String _baseUrl = 'https://api.jikan.moe'; @override - Future?> loadData() async{ - try - { - const String url = '$_baseUrl/v4/anime'; - final Response response = await _dio.get>(url); + Future?> loadData({String? q}) async { + try { + const String url = '$_baseUrl/v4/anime'; + Map query = {'limit' : 5, 'q' : q}; + print(q); + final Response response = + await _dio.get>( + url, + queryParameters: query, + ); - final AnimesDto dto = AnimesDto.fromJson(response.data as Map); - final List? data = dto.data?.map((e) => e.toDomain()).toList(); - return data; - } on DioException catch (e) - { + final AnimesDto dto = + AnimesDto.fromJson(response.data as Map); + final List? data = dto.data?.map((e) => e.toDomain()).toList(); + return data; + } on DioException catch (e) { return null; } } -} \ No newline at end of file +} diff --git a/lib/presentation/home_page/home_page.dart b/lib/presentation/home_page/home_page.dart index 06812a6..a1f1f8d 100644 --- a/lib/presentation/home_page/home_page.dart +++ b/lib/presentation/home_page/home_page.dart @@ -43,31 +43,58 @@ class _MyHomePageState extends State { } } -class Body extends StatelessWidget { +class Body extends StatefulWidget { const Body({super.key}); + @override + State createState() => _BodyState(); +} + +class _BodyState extends State { + final AnimeRepository repos = AnimeRepository(); + final searchController = TextEditingController(); + late Future?> data; + + @override + void initState() { + data = repos.loadData(); + super.initState(); + } + @override Widget build(BuildContext context) { - final data = AnimeRepository().loadData(); return Center( child: FutureBuilder?>( - future: data, - builder: (context, snapshot) => SingleChildScrollView( - child: snapshot.hasData - ? Column( - mainAxisAlignment: MainAxisAlignment.center, - children: snapshot.data!.map( - (e) { - return _Card.fromData( - e, - onLike: (String title, bool isLiked) => - _showLiked(context, title, isLiked), - onTap: () => _navToDetails(context, e), - ); + future: data, + builder: (context, snapshot) => SingleChildScrollView( + child: snapshot.hasData + ? Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Padding( + padding: const EdgeInsets.all(8), + child: CupertinoSearchTextField( + controller: searchController, + onChanged: (search) { + setState(() { + data = repos.loadData(q : search); + }); }, - ).toList(), - ) - : const CircularProgressIndicator(), + ), + ), + ...snapshot.data!.map( + (e) { + return _Card.fromData( + e, + onLike: (String title, bool isLiked) => + _showLiked(context, title, isLiked), + onTap: () => _navToDetails(context, e), + ); + }, + ).toList() + ], + ) + : const CircularProgressIndicator(), ), )); }