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';
@ -8,26 +7,31 @@ import 'package:pretty_dio_logger/pretty_dio_logger.dart';
class AnimeRepository extends ApiInterface {
static final Dio _dio = Dio()
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
..interceptors.add(PrettyDioLogger(
requestHeader: true,
requestBody: true,
));
static const String _baseUrl = 'https://api.jikan.moe';
@override
Future<List<CardData>?> loadData() async{
try
{
const String url = '$_baseUrl/v4/anime';
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
Future<List<CardData>?> loadData({String? q}) async {
try {
const String url = '$_baseUrl/v4/anime';
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 List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
return data;
} on DioException catch (e)
{
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) {
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});
@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,
builder: (context, snapshot) => SingleChildScrollView(
child: snapshot.hasData
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: snapshot.data!.map(
(e) {
return _Card.fromData(
e,
onLike: (String title, bool isLiked) =>
_showLiked(context, title, isLiked),
onTap: () => _navToDetails(context, e),
);
future: data,
builder: (context, snapshot) => SingleChildScrollView(
child: snapshot.hasData
? Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8),
child: CupertinoSearchTextField(
controller: searchController,
onChanged: (search) {
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(),
),
));
}