add search support

This commit is contained in:
shirotame 2024-10-05 00:59:39 +04:00
parent 726e6f4323
commit 72affbaf25
2 changed files with 51 additions and 25 deletions

View File

@ -1,3 +1,5 @@
import 'dart:developer';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:flutter_project/data/mappers/animes_mapper.dart'; import 'package:flutter_project/data/mappers/animes_mapper.dart';
import 'package:flutter_project/data/repositories/api_interface.dart'; import 'package:flutter_project/data/repositories/api_interface.dart';
@ -11,12 +13,13 @@ class AnimeRepository extends ApiInterface {
static const String _baseUrl = "https://api.jikan.moe"; static const String _baseUrl = "https://api.jikan.moe";
@override @override
Future<List<CardData>?> loadData() async { Future<List<CardData>?> loadData({String? q}) async {
try { try {
const String url = "$_baseUrl/v4/top/anime?sfw&limit=10"; const String url = "$_baseUrl/v4/anime?sfw";
final Response<dynamic> response = final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
await _dio.get<Map<dynamic, dynamic>>(url); url,
queryParameters: q != null ? {'q': q} : null);
final AnimesDto dto = final AnimesDto dto =
AnimesDto.fromJson(response.data as Map<String, dynamic>); AnimesDto.fromJson(response.data as Map<String, dynamic>);
@ -25,6 +28,7 @@ class AnimeRepository extends ApiInterface {
return data; return data;
} on DioException catch (e) { } on DioException catch (e) {
log("Exception. ${e.message.toString()}", error: e);
return null; return null;
} }
} }

View File

@ -1,6 +1,5 @@
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_project/data/repositories/anime_repository.dart'; import 'package:flutter_project/data/repositories/anime_repository.dart';
import 'package:flutter_project/domain/models/card.dart'; import 'package:flutter_project/domain/models/card.dart';
import 'package:flutter_project/views/details_page/details_page.dart'; import 'package:flutter_project/views/details_page/details_page.dart';
@ -28,31 +27,54 @@ class _HomePageState extends State<HomePage> {
} }
} }
class Body extends StatelessWidget { class Body extends StatefulWidget {
const Body({super.key}); const Body({super.key});
@override @override
Widget build(BuildContext context) { State<Body> createState() => _BodyState();
final data = AnimeRepository().loadData(); }
return Center( class _BodyState extends State<Body> {
child: FutureBuilder( final AnimeRepository repo = AnimeRepository();
future: data, var data = AnimeRepository().loadData();
builder: (context, snapshot) => SingleChildScrollView(
child: snapshot.hasData @override
? Column( Widget build(BuildContext context) {
children: snapshot.data return Column(
?.map((data) => _Card.withData( children: [
data, Padding(
onLike: (String title, bool isLiked) => padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
_showSnackBar(context, isLiked, title), child: CupertinoSearchTextField(
onTap: () => _navToDetails(context, data), onChanged: (search) {
)) setState(() {
.toList() ?? data = repo.loadData(q: search);
[]) });
: const CircularProgressIndicator(), },
),
), ),
), Expanded(
child: Center(
child: FutureBuilder<List<CardData>?>(
future: data,
builder: (context, snapshot) => SingleChildScrollView(
child: snapshot.hasData
? Column(
children: snapshot.data
?.map((data) => _Card.withData(
data,
onLike: (String title, bool isLiked) =>
_showSnackBar(
context, isLiked, title),
onTap: () => _navToDetails(context, data),
))
.toList() ??
[])
: const CircularProgressIndicator(),
),
),
),
),
],
); );
} }