Compare commits
No commits in common. "0f8228076ce9dc24882b8c77168c79b698ca0ef2" and "539dc15d6dfde86d079e4ee6d49667286aee162d" have entirely different histories.
0f8228076c
...
539dc15d6d
@ -1,20 +0,0 @@
|
||||
import 'dart:async';
|
||||
import 'dart:ui';
|
||||
|
||||
class Debounce{
|
||||
factory Debounce() => _instance;
|
||||
|
||||
Debounce._();
|
||||
|
||||
static final Debounce _instance = Debounce._();
|
||||
|
||||
static Timer? _timer;
|
||||
|
||||
static void run(
|
||||
VoidCallback action, {
|
||||
Duration delay = const Duration(milliseconds: 500),
|
||||
}) {
|
||||
_timer?.cancel();
|
||||
_timer = Timer(delay, action);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
|
||||
import 'package:untitled/domain/models/card.dart';
|
||||
typedef OnErrorCallback = void Function(String? error);
|
||||
|
||||
abstract class ApiInterface {
|
||||
Future<List<CardData>?> loadData();
|
||||
}
|
@ -15,12 +15,9 @@ class FortniteRepository extends ApiInterface{
|
||||
static const String _baseUrl = 'https://fortnite-api.com';
|
||||
|
||||
@override
|
||||
Future<List<CardData>?> loadData({String? q, OnErrorCallback? onError}) async {
|
||||
Future<List<CardData>?> loadData() async {
|
||||
try{
|
||||
String url = '$_baseUrl/v2/cosmetics/br/search/all?type=pickaxe';
|
||||
if(q != null && q != ""){
|
||||
url += '&matchMethod=contains&name=$q';
|
||||
}
|
||||
const String url = '$_baseUrl/v2/cosmetics/br/search/all?type=pickaxe';
|
||||
|
||||
final Response<dynamic> response = await _dio.get<Map<dynamic,dynamic>>(url);
|
||||
|
||||
@ -28,7 +25,6 @@ class FortniteRepository extends ApiInterface{
|
||||
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
|
||||
return data;
|
||||
} on DioException catch (e){
|
||||
onError?.call(e.error?.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,4 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:untitled/data/repositories/fortnite_reppository.dart';
|
||||
import 'package:untitled/presentation/home_page/bloc/bloc.dart';
|
||||
import 'package:untitled/presentation/home_page/home_page.dart';
|
||||
|
||||
void main() {
|
||||
@ -19,15 +16,7 @@ class MyApp extends StatelessWidget {
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: RepositoryProvider<FortniteRepository>(
|
||||
lazy: true,
|
||||
create: (_) => FortniteRepository(),
|
||||
child: BlocProvider<HomeBlock>(
|
||||
lazy: false,
|
||||
create: (context) => HomeBlock(context.read<FortniteRepository>()),
|
||||
child: const MyHomePage(title: "PIbd-31 Sagirov Marsel"),
|
||||
),
|
||||
),
|
||||
home: const MyHomePage(title: 'PMU Sagirov Marsel PIbd-31'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +0,0 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:untitled/data/repositories/fortnite_reppository.dart';
|
||||
import 'package:untitled/presentation/home_page/bloc/events.dart';
|
||||
import 'package:untitled/presentation/home_page/bloc/state.dart';
|
||||
|
||||
class HomeBlock extends Bloc<HomeEvent, HomeState> {
|
||||
final FortniteRepository repo;
|
||||
|
||||
HomeBlock(this.repo) : super(const HomeState()){
|
||||
on<HomeLoadDataEvent>(_onLoadData);
|
||||
}
|
||||
|
||||
Future<void> _onLoadData (HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
String? error;
|
||||
|
||||
final data = await repo.loadData(q: event.search, onError: (e) => error = e);
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
data: data,
|
||||
error: error,
|
||||
));
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
abstract class HomeEvent {
|
||||
const HomeEvent();
|
||||
}
|
||||
|
||||
class HomeLoadDataEvent extends HomeEvent{
|
||||
final String? search;
|
||||
const HomeLoadDataEvent({this.search});
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:untitled/domain/models/card.dart';
|
||||
|
||||
class HomeState extends Equatable{
|
||||
final List<CardData>? data;
|
||||
final bool isLoading;
|
||||
final String? error;
|
||||
|
||||
const HomeState({this.data, this.isLoading = false, this.error});
|
||||
|
||||
HomeState copyWith({List<CardData>? data, bool? isLoading, String? error}) => HomeState(data: data ?? this.data, isLoading: isLoading ?? this.isLoading, error: error ?? this.error);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [data,isLoading,error];
|
||||
}
|
@ -1,10 +1,8 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:untitled/presentation/home_page/bloc/bloc.dart';
|
||||
import 'package:untitled/presentation/home_page/bloc/events.dart';
|
||||
import 'package:untitled/presentation/home_page/bloc/state.dart';
|
||||
import '../../components/utils/debounce.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';
|
||||
part 'card.dart';
|
||||
@ -31,76 +29,30 @@ class _MyHomePageState extends State<MyHomePage> {
|
||||
}
|
||||
}
|
||||
|
||||
class Body extends StatefulWidget {
|
||||
class Body extends StatelessWidget {
|
||||
const Body({super.key});
|
||||
|
||||
@override
|
||||
State<Body> createState() => _BodyState();
|
||||
}
|
||||
class _BodyState extends State<Body> {
|
||||
final searchController = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.read<HomeBlock>().add(const HomeLoadDataEvent());
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
@override
|
||||
void dispose() {
|
||||
searchController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
Future<void> _onRefresh() {
|
||||
context.read<HomeBlock>().add(HomeLoadDataEvent(search: searchController.text));
|
||||
return Future.value(null);
|
||||
}
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: CupertinoSearchTextField(
|
||||
controller: searchController,
|
||||
onSubmitted: (search) {
|
||||
Debounce.run(() => context.read<HomeBlock>().add(HomeLoadDataEvent(search: search)));
|
||||
},
|
||||
),
|
||||
),
|
||||
BlocBuilder<HomeBlock, HomeState>(
|
||||
builder: (context,state) => state.error != null ?
|
||||
Text(
|
||||
state.error ?? "",
|
||||
style: Theme.of(context).textTheme.headlineSmall?.copyWith(color: Colors.red),
|
||||
)
|
||||
:
|
||||
state.isLoading ?
|
||||
const CircularProgressIndicator() :
|
||||
Expanded(
|
||||
child: RefreshIndicator(
|
||||
onRefresh: _onRefresh,
|
||||
child: ListView.builder(
|
||||
padding: EdgeInsets.zero,
|
||||
itemCount: state.data?.length ?? 0,
|
||||
itemBuilder: (context, index) {
|
||||
final data = state.data?[index];
|
||||
return data != null
|
||||
? _Card.fromData(
|
||||
data,
|
||||
onLike: (name, isLiked) =>
|
||||
_showSnackBar(context, name, isLiked),
|
||||
onTap: () => _navToDetails(context,data),
|
||||
)
|
||||
: const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
)
|
||||
)
|
||||
final data = FortniteRepository().loadData();
|
||||
return Center(
|
||||
child: FutureBuilder<List<CardData>?>(
|
||||
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(),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showSnackBar(BuildContext context, String name, bool isLiked) {
|
||||
|
40
pubspec.lock
40
pubspec.lock
@ -38,14 +38,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.11.0"
|
||||
bloc:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: bloc
|
||||
sha256: "106842ad6569f0b60297619e9e0b1885c2fb9bf84812935490e6c5275777804e"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "8.1.4"
|
||||
boolean_selector:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -206,14 +198,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.0"
|
||||
equatable:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: equatable
|
||||
sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.5"
|
||||
fake_async:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -243,14 +227,6 @@ packages:
|
||||
description: flutter
|
||||
source: sdk
|
||||
version: "0.0.0"
|
||||
flutter_bloc:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: flutter_bloc
|
||||
sha256: b594505eac31a0518bdcb4b5b79573b8d9117b193cc80cc12e17d639b10aa27a
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "8.1.6"
|
||||
flutter_lints:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
@ -416,14 +392,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.0"
|
||||
nested:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: nested
|
||||
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.0.0"
|
||||
package_config:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -456,14 +424,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.4.0"
|
||||
provider:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: provider
|
||||
sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "6.1.2"
|
||||
pub_semver:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
@ -38,8 +38,7 @@ dependencies:
|
||||
json_annotation: ^4.8.1
|
||||
dio: ^5.4.2+1
|
||||
pretty_dio_logger: ^1.4.0
|
||||
equatable: ^2.0.5
|
||||
flutter_bloc: ^8.1.5
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
Loading…
Reference in New Issue
Block a user