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:dio/dio.dart';
import 'package:first_project/data/dtos/anime_dto.dart'; import 'package:first_project/data/dtos/anime_dto.dart';
import 'package:first_project/data/mappers/anime_mapper.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 { class AnimeRepository extends ApiInterface {
static final Dio _dio = Dio() static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger( ..interceptors.add(PrettyDioLogger(
requestHeader: true, requestHeader: true,
requestBody: true, requestBody: true,
)); ));
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/anime';
const String url = '$_baseUrl/v4/anime'; Map<String, dynamic> query = {'limit' : 5, 'q' : q};
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url); 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 =
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList(); AnimesDto.fromJson(response.data as Map<String, dynamic>);
return data; final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
} on DioException catch (e) return data;
{ } on DioException catch (e) {
return null; return null;
} }
} }
} }

View File

@ -43,31 +43,58 @@ class _MyHomePageState extends State<MyHomePage> {
} }
} }
class Body extends StatelessWidget { class Body extends StatefulWidget {
const Body({super.key}); 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final data = AnimeRepository().loadData();
return Center( return Center(
child: FutureBuilder<List<CardData>?>( child: FutureBuilder<List<CardData>?>(
future: data, future: data,
builder: (context, snapshot) => SingleChildScrollView( builder: (context, snapshot) => SingleChildScrollView(
child: snapshot.hasData child: snapshot.hasData
? Column( ? Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: snapshot.data!.map( children: [
(e) { Padding(
return _Card.fromData( padding: const EdgeInsets.all(8),
e, child: CupertinoSearchTextField(
onLike: (String title, bool isLiked) => controller: searchController,
_showLiked(context, title, isLiked), onChanged: (search) {
onTap: () => _navToDetails(context, e), 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(),
), ),
)); ));
} }