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:flutter_project/data/mappers/animes_mapper.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";
@override
Future<List<CardData>?> loadData() async {
Future<List<CardData>?> loadData({String? q}) async {
try {
const String url = "$_baseUrl/v4/top/anime?sfw&limit=10";
const String url = "$_baseUrl/v4/anime?sfw";
final Response<dynamic> response =
await _dio.get<Map<dynamic, dynamic>>(url);
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: q != null ? {'q': q} : null);
final AnimesDto dto =
AnimesDto.fromJson(response.data as Map<String, dynamic>);
@ -25,6 +28,7 @@ class AnimeRepository extends ApiInterface {
return data;
} on DioException catch (e) {
log("Exception. ${e.message.toString()}", error: e);
return null;
}
}

View File

@ -1,6 +1,5 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_project/data/repositories/anime_repository.dart';
import 'package:flutter_project/domain/models/card.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});
@override
Widget build(BuildContext context) {
final data = AnimeRepository().loadData();
State<Body> createState() => _BodyState();
}
return Center(
child: FutureBuilder(
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(),
class _BodyState extends State<Body> {
final AnimeRepository repo = AnimeRepository();
var data = AnimeRepository().loadData();
@override
Widget build(BuildContext context) {
return Column(
children: [
Padding(
padding: EdgeInsets.only(left: 16, right: 16, top: 10, bottom: 10),
child: CupertinoSearchTextField(
onChanged: (search) {
setState(() {
data = repo.loadData(q: search);
});
},
),
),
),
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(),
),
),
),
),
],
);
}