Сделано
This commit is contained in:
parent
4b6d0fc446
commit
22de980f50
3
devtools_options.yaml
Normal file
3
devtools_options.yaml
Normal file
@ -0,0 +1,3 @@
|
||||
description: This file stores settings for Dart & Flutter DevTools.
|
||||
documentation: https://docs.flutter.dev/tools/devtools/extensions#configure-extension-enablement-states
|
||||
extensions:
|
20
lib/components/utils/debounce.dart
Normal file
20
lib/components/utils/debounce.dart
Normal 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);
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
import 'package:pmu_labs/domain/models/card.dart';
|
||||
|
||||
typedef OnErrorCallback = void Function(String? error);
|
||||
|
||||
abstract class ApiInterface{
|
||||
Future<List<CardData>?> loadData();
|
||||
}
|
@ -49,7 +49,7 @@ class BossesRepository extends ApiInterface {
|
||||
static const String _baseUrl = 'https://sekiro.fandom.com/ru/api.php';
|
||||
|
||||
@override
|
||||
Future<List<CardData>?> loadData({String? q}) async {
|
||||
Future<List<CardData>?> loadData({OnErrorCallback? onError,String? q,}) async {
|
||||
try {
|
||||
String url = '';
|
||||
if(q != null && q != ""){
|
||||
@ -94,6 +94,7 @@ class BossesRepository 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;
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,21 @@ class HomeBloc extends Bloc<HomeEvent, HomeState>{
|
||||
on<HomeLoadDataEvent>(_onLoadData);
|
||||
}
|
||||
|
||||
void _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit){
|
||||
emit(state.copyWith(data: rep.loadData()));
|
||||
Future<void> _onLoadData(
|
||||
HomeLoadDataEvent event, Emitter<HomeState> emit) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
|
||||
String? error;
|
||||
|
||||
final data = await rep.loadData(
|
||||
q: event.search,
|
||||
onError: (e) => error = e,
|
||||
);
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
data: data,
|
||||
error: error,
|
||||
));
|
||||
}
|
||||
}
|
@ -3,5 +3,7 @@ abstract class HomeEvent{
|
||||
}
|
||||
|
||||
class HomeLoadDataEvent extends HomeEvent{
|
||||
const HomeLoadDataEvent();
|
||||
final String? search;
|
||||
|
||||
const HomeLoadDataEvent({this.search});
|
||||
}
|
@ -2,12 +2,31 @@ import 'package:equatable/equatable.dart';
|
||||
import 'package:pmu_labs/domain/models/card.dart';
|
||||
|
||||
class HomeState extends Equatable {
|
||||
final Future<List<CardData>?>? data;
|
||||
final List<CardData>? data;
|
||||
final bool isLoading;
|
||||
final String? error;
|
||||
|
||||
const HomeState({this.data});
|
||||
const HomeState({
|
||||
this.data,
|
||||
this.isLoading = false,
|
||||
this.error,
|
||||
});
|
||||
|
||||
HomeState copyWith({Future<List<CardData>?>? data}) => HomeState(data: data ?? this.data);
|
||||
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];
|
||||
List<Object?> get props => [
|
||||
data,
|
||||
isLoading,
|
||||
error,
|
||||
];
|
||||
}
|
@ -1,8 +1,12 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:pmu_labs/data/repositories/bosses_repository.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart%20%20';
|
||||
import 'package:pmu_labs/presentation/details_page/details_page.dart';
|
||||
import 'package:pmu_labs/presentation/home_page/bloc/bloc.dart';
|
||||
import 'package:pmu_labs/presentation/home_page/bloc/events.dart';
|
||||
import 'package:pmu_labs/presentation/home_page/bloc/state.dart';
|
||||
|
||||
import '../../components/utils/debounce.dart';
|
||||
import '../../domain/models/card.dart';
|
||||
|
||||
part 'card.dart';
|
||||
@ -40,53 +44,77 @@ class Body extends StatefulWidget {
|
||||
|
||||
class _BodyState extends State<Body> {
|
||||
final searchController = TextEditingController();
|
||||
late Future<List<CardData>?> data;
|
||||
final repo = BossesRepository();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
data = repo.loadData();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||
});
|
||||
super.initState();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
searchController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _onRefresh() {
|
||||
context
|
||||
.read<HomeBloc>()
|
||||
.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(children: [
|
||||
return Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: CupertinoSearchTextField(
|
||||
controller: searchController,
|
||||
onSubmitted: (search) {
|
||||
setState(() {
|
||||
data = repo.loadData(q: search);
|
||||
});
|
||||
onChanged: (search ) {
|
||||
Debounce.run(() => context
|
||||
.read<HomeBloc>()
|
||||
.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(
|
||||
BlocBuilder<HomeBloc, 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: (String title, bool isLiked) =>
|
||||
_showSnackBar(context, title, isLiked),
|
||||
onTap: () => _navToDetails(context, data),
|
||||
);
|
||||
}).toList() ??
|
||||
[],
|
||||
)
|
||||
: const CircularProgressIndicator(),
|
||||
: const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
]));
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
void _navToDetails(BuildContext context, CardData data) {
|
||||
|
Loading…
Reference in New Issue
Block a user