Шестая сделано

This commit is contained in:
Marselchi 2024-11-12 12:35:50 +04:00
parent eb8e4b5483
commit 0f8228076c
10 changed files with 175 additions and 37 deletions

View File

@ -0,0 +1,20 @@
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);
}
}

View File

@ -1,6 +1,6 @@
import 'package:untitled/domain/models/card.dart';
typedef OnErrorCallback = void Function(String? error);
abstract class ApiInterface {
Future<List<CardData>?> loadData();
}

View File

@ -15,7 +15,7 @@ class FortniteRepository extends ApiInterface{
static const String _baseUrl = 'https://fortnite-api.com';
@override
Future<List<CardData>?> loadData({String? q}) async {
Future<List<CardData>?> loadData({String? q, OnErrorCallback? onError}) async {
try{
String url = '$_baseUrl/v2/cosmetics/br/search/all?type=pickaxe';
if(q != null && q != ""){
@ -28,6 +28,7 @@ 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;
}
}

View File

@ -1,4 +1,7 @@
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() {
@ -16,7 +19,15 @@ class MyApp extends StatelessWidget {
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'PMU Sagirov Marsel PIbd-31'),
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"),
),
),
);
}
}

View File

@ -0,0 +1,25 @@
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,
));
}
}

View File

@ -0,0 +1,8 @@
abstract class HomeEvent {
const HomeEvent();
}
class HomeLoadDataEvent extends HomeEvent{
final String? search;
const HomeLoadDataEvent({this.search});
}

View File

@ -0,0 +1,15 @@
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];
}

View File

@ -1,7 +1,10 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:untitled/data/repositories/fortnite_reppository.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 '../../domain/models/card.dart';
import '../details_page/details_page.dart';
part 'card.dart';
@ -35,54 +38,68 @@ class Body extends StatefulWidget {
State<Body> createState() => _BodyState();
}
class _BodyState extends State<Body> {
final searchController = TextEditingController();
late Future<List<CardData>?> data;
final repo = FortniteRepository();
@override
void initState() {
data = repo.loadData();
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 Padding(
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: Column(
return Column(
children: [
Padding(
padding: const EdgeInsets.all(12),
child: CupertinoSearchTextField(
controller: searchController,
onSubmitted: (search) {
setState(() {
data = repo.loadData(q: search);
});
Debounce.run(() => context.read<HomeBlock>().add(HomeLoadDataEvent(search: search)));
},
),
),
Expanded(
child: 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() ?? [],
BlocBuilder<HomeBlock, HomeState>(
builder: (context,state) => state.error != null ?
Text(
state.error ?? "",
style: Theme.of(context).textTheme.headlineSmall?.copyWith(color: Colors.red),
)
: const CircularProgressIndicator(),
),
),
),
:
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();
},
),
)
)
)
],
),
);
}

View File

@ -38,6 +38,14 @@ 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:
@ -198,6 +206,14 @@ 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:
@ -227,6 +243,14 @@ 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:
@ -392,6 +416,14 @@ 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:
@ -424,6 +456,14 @@ 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:

View File

@ -38,7 +38,8 @@ 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