add search support
This commit is contained in:
parent
726e6f4323
commit
72affbaf25
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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,15 +27,34 @@ 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(
|
||||
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
|
||||
@ -45,7 +63,8 @@ class Body extends StatelessWidget {
|
||||
?.map((data) => _Card.withData(
|
||||
data,
|
||||
onLike: (String title, bool isLiked) =>
|
||||
_showSnackBar(context, isLiked, title),
|
||||
_showSnackBar(
|
||||
context, isLiked, title),
|
||||
onTap: () => _navToDetails(context, data),
|
||||
))
|
||||
.toList() ??
|
||||
@ -53,6 +72,9 @@ class Body extends StatelessWidget {
|
||||
: const CircularProgressIndicator(),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user