diff --git a/lib/data/repositories/fortnite_reppository.dart b/lib/data/repositories/fortnite_reppository.dart index b7f1a63..7875581 100644 --- a/lib/data/repositories/fortnite_reppository.dart +++ b/lib/data/repositories/fortnite_reppository.dart @@ -15,9 +15,12 @@ class FortniteRepository extends ApiInterface{ static const String _baseUrl = 'https://fortnite-api.com'; @override - Future?> loadData() async { + Future?> loadData({String? q}) async { try{ - const String url = '$_baseUrl/v2/cosmetics/br/search/all?type=pickaxe'; + String url = '$_baseUrl/v2/cosmetics/br/search/all?type=pickaxe'; + if(q != null && q != ""){ + url += '&matchMethod=contains&name=$q'; + } final Response response = await _dio.get>(url); diff --git a/lib/presentation/home_page/home_page.dart b/lib/presentation/home_page/home_page.dart index c68c9d1..7a974e0 100644 --- a/lib/presentation/home_page/home_page.dart +++ b/lib/presentation/home_page/home_page.dart @@ -1,7 +1,6 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:untitled/data/repositories/fortnite_reppository.dart'; -import 'package:untitled/data/repositories/mock_repository.dart'; import '../../domain/models/card.dart'; import '../details_page/details_page.dart'; @@ -29,28 +28,60 @@ class _MyHomePageState extends State { } } -class Body extends StatelessWidget { +class Body extends StatefulWidget { const Body({super.key}); + @override + State createState() => _BodyState(); +} +class _BodyState extends State { + + final searchController = TextEditingController(); + late Future?> data; + final repo = FortniteRepository(); + @override + void initState() { + data = repo.loadData(); + super.initState(); + } @override Widget build(BuildContext context) { - final data = FortniteRepository().loadData(); - return Center( - child: FutureBuilder?>( - future: data, - builder: (context, snapshot) => SingleChildScrollView( - child: snapshot.hasData ? Column( - mainAxisAlignment: MainAxisAlignment.center, - children: snapshot.data?.map((data) { return _Card.fromData( - data, - onLike: (String name, bool isLiked) => - _showSnackBar(context, name, isLiked), - onTap: () => _navToDetails(context, data), - ); - }).toList() ?? [], - ) - : const CircularProgressIndicator(), - ), + return Padding( + padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top), + child: Column( + children: [ + Padding( + padding: const EdgeInsets.all(12), + child: CupertinoSearchTextField( + controller: searchController, + onSubmitted: (search) { + setState(() { + data = repo.loadData(q: search); + }); + }, + ), + ), + Expanded( + child: Center( + child: FutureBuilder?>( + future: data, + builder: (context, snapshot) => SingleChildScrollView( + child: snapshot.hasData ? Column( + mainAxisAlignment: MainAxisAlignment.center, + children: snapshot.data?.map((data) { return _Card.fromData( + data, + onLike: (String name, bool isLiked) => + _showSnackBar(context, name, isLiked), + onTap: () => _navToDetails(context, data), + ); + }).toList() ?? [], + ) + : const CircularProgressIndicator(), + ), + ), + ), + ), + ], ), ); }