Forgot about search

This commit is contained in:
Serxiolog 2024-11-18 16:30:59 +04:00
parent bfd46aca31
commit f021d050ef
2 changed files with 65 additions and 34 deletions

View File

@ -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';
@ -16,17 +15,22 @@ class AnimeRepository extends ApiInterface {
static const String _baseUrl = 'https://api.jikan.moe';
@override
Future<List<CardData>?> loadData() async{
try
{
Future<List<CardData>?> loadData({String? q}) async {
try {
const String url = '$_baseUrl/v4/anime';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
Map<String, dynamic> query = {'limit' : 5, 'q' : q};
print(q);
final Response<dynamic> response =
await _dio.get<Map<dynamic, dynamic>>(
url,
queryParameters: query,
);
final AnimesDto dto = AnimesDto.fromJson(response.data as Map<String, dynamic>);
final AnimesDto dto =
AnimesDto.fromJson(response.data as Map<String, dynamic>);
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
return data;
} on DioException catch (e)
{
} on DioException catch (e) {
return null;
}
}

View File

@ -43,12 +43,26 @@ class _MyHomePageState extends State<MyHomePage> {
}
}
class Body extends StatelessWidget {
class Body extends StatefulWidget {
const Body({super.key});
@override
State<Body> createState() => _BodyState();
}
class _BodyState extends State<Body> {
final AnimeRepository repos = AnimeRepository();
final searchController = TextEditingController();
late Future<List<CardData>?> data;
@override
void initState() {
data = repos.loadData();
super.initState();
}
@override
Widget build(BuildContext context) {
final data = AnimeRepository().loadData();
return Center(
child: FutureBuilder<List<CardData>?>(
future: data,
@ -56,7 +70,19 @@ class Body extends StatelessWidget {
child: snapshot.hasData
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: snapshot.data!.map(
children: [
Padding(
padding: const EdgeInsets.all(8),
child: CupertinoSearchTextField(
controller: searchController,
onChanged: (search) {
setState(() {
data = repos.loadData(q : search);
});
},
),
),
...snapshot.data!.map(
(e) {
return _Card.fromData(
e,
@ -65,7 +91,8 @@ class Body extends StatelessWidget {
onTap: () => _navToDetails(context, e),
);
},
).toList(),
).toList()
],
)
: const CircularProgressIndicator(),
),