рабочая 6 лаба
This commit is contained in:
parent
af324833e6
commit
dd65cb730a
22
lib/components/utils/debounce.dart
Normal file
22
lib/components/utils/debounce.dart
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
|
|
||||||
|
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/domain/models/card.dart';
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
|
||||||
|
typedef OnErrorCallback = void Function(String? error);
|
||||||
|
|
||||||
abstract class ApiInterface{
|
abstract class ApiInterface{
|
||||||
Future<List<MarketData>?> loadData();
|
Future<List<MarketData>?> loadData();
|
||||||
}
|
}
|
@ -14,7 +14,7 @@ class FoodRepository extends ApiInterface{
|
|||||||
|
|
||||||
static const String _baseUrl = 'https://www.themealdb.com/api/json/v1/1';
|
static const String _baseUrl = 'https://www.themealdb.com/api/json/v1/1';
|
||||||
@override
|
@override
|
||||||
Future<List<MarketData>?> loadData({String? q}) async {
|
Future<List<MarketData>?> loadData({OnErrorCallback? onError, String? q}) async {
|
||||||
try {
|
try {
|
||||||
final String url = q != null ? '$_baseUrl/search.php' : '$_baseUrl/filter.php?i=chicken_breast';
|
final String url = q != null ? '$_baseUrl/search.php' : '$_baseUrl/filter.php?i=chicken_breast';
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ class FoodRepository extends ApiInterface{
|
|||||||
final List<MarketData>? items = dto.meals?.map((e) => e.toDomain()).toList();
|
final List<MarketData>? items = dto.meals?.map((e) => e.toDomain()).toList();
|
||||||
return items;
|
return items;
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
print('Ошибка запроса: $e');
|
onError?.call(e.error?.toString());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pmu/data/repository/food_repository.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/bloc.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:pmu/presentation/home_page/home_page.dart';
|
import 'package:pmu/presentation/home_page/home_page.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -16,7 +19,15 @@ class MyApp extends StatelessWidget {
|
|||||||
colorScheme: ColorScheme.fromSeed(seedColor: Color(0xFFFFA400)),
|
colorScheme: ColorScheme.fromSeed(seedColor: Color(0xFFFFA400)),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
home: const MyHomePage(title: 'Магизин у дома'),
|
home: RepositoryProvider<FoodRepository>(
|
||||||
|
lazy: true,
|
||||||
|
create: (_) => FoodRepository(),
|
||||||
|
child: BlocProvider<HomeBloc>(
|
||||||
|
lazy: false,
|
||||||
|
create: (context) => HomeBloc(context.read<FoodRepository>()),
|
||||||
|
child: const MyHomePage(title: "Food cort"),
|
||||||
|
),
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
29
lib/presentation/home_page/bloc/bloc.dart
Normal file
29
lib/presentation/home_page/bloc/bloc.dart
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import 'package:pmu/data/repository/food_repository.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/events.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/state.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
|
||||||
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||||
|
final FoodRepository repo;
|
||||||
|
|
||||||
|
HomeBloc(this.repo) : super(const HomeState()) {
|
||||||
|
on<HomeLoadDataEvent>(_onLoadData);
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
9
lib/presentation/home_page/bloc/events.dart
Normal file
9
lib/presentation/home_page/bloc/events.dart
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
abstract class HomeEvent {
|
||||||
|
const HomeEvent();
|
||||||
|
}
|
||||||
|
class HomeLoadDataEvent extends HomeEvent {
|
||||||
|
|
||||||
|
final String? search;
|
||||||
|
|
||||||
|
const HomeLoadDataEvent({this.search});
|
||||||
|
}
|
33
lib/presentation/home_page/bloc/state.dart
Normal file
33
lib/presentation/home_page/bloc/state.dart
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
|
||||||
|
|
||||||
|
class HomeState extends Equatable {
|
||||||
|
final List<MarketData>? data;
|
||||||
|
final bool isLoading;
|
||||||
|
final String? error;
|
||||||
|
|
||||||
|
const HomeState({
|
||||||
|
this.data,
|
||||||
|
this.isLoading = false,
|
||||||
|
this.error,
|
||||||
|
});
|
||||||
|
|
||||||
|
HomeState copyWith({
|
||||||
|
List<MarketData>? 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,9 +1,15 @@
|
|||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:pmu/components/utils/debounce.dart';
|
||||||
import 'package:pmu/data/repository/food_repository.dart';
|
import 'package:pmu/data/repository/food_repository.dart';
|
||||||
import 'package:pmu/data/repository/mock_repository.dart';
|
import 'package:pmu/data/repository/mock_repository.dart';
|
||||||
import 'package:pmu/domain/models/card.dart';
|
import 'package:pmu/domain/models/card.dart';
|
||||||
|
import 'package:pmu/main.dart';
|
||||||
import 'package:pmu/presentation/details_page/details_page.dart';
|
import 'package:pmu/presentation/details_page/details_page.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/bloc.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/events.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:pmu/presentation/home_page/bloc/state.dart';
|
||||||
|
|
||||||
part 'card.dart';
|
part 'card.dart';
|
||||||
|
|
||||||
@ -21,12 +27,29 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
final searchController = TextEditingController();
|
final searchController = TextEditingController();
|
||||||
late Future<List<MarketData>?> data;
|
late Future<List<MarketData>?> data;
|
||||||
final repo = FoodRepository();
|
final repo = FoodRepository();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
data = repo.loadData();
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||||
|
});
|
||||||
|
|
||||||
super.initState();
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
@ -38,45 +61,51 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
padding: const EdgeInsets.all(12),
|
padding: const EdgeInsets.all(12),
|
||||||
child: CupertinoSearchTextField(
|
child: CupertinoSearchTextField(
|
||||||
controller: searchController,
|
controller: searchController,
|
||||||
style: const TextStyle(color: Colors.white),
|
onChanged: (search) {
|
||||||
onSubmitted: (search) {
|
Debounce.run(() => context
|
||||||
setState(() {
|
.read<HomeBloc>()
|
||||||
data = repo.loadData(q: search);
|
.add(HomeLoadDataEvent(search: search)));
|
||||||
});
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Expanded(
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
child: Center(
|
builder: (context, state) => state.error != null
|
||||||
child: FutureBuilder<List<MarketData>?>(
|
? Text(
|
||||||
future: data,
|
state.error ?? '',
|
||||||
builder: (context, snapshot) => SingleChildScrollView(
|
style: Theme.of(context)
|
||||||
child: snapshot.hasData
|
.textTheme
|
||||||
? Column(
|
.headlineSmall
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
?.copyWith(color: Colors.red),
|
||||||
children: snapshot.data?.map((data) {
|
|
||||||
return _MarketCard.fromData(
|
|
||||||
data,
|
|
||||||
onLike: (String title, bool isLiked) =>
|
|
||||||
_showSnackBar(context, title, isLiked),
|
|
||||||
onTap: () => _navToDetails(context, data),
|
|
||||||
);
|
|
||||||
}).toList() ??
|
|
||||||
[],
|
|
||||||
)
|
)
|
||||||
: 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
|
||||||
|
? _MarketCard.fromData(
|
||||||
|
data,
|
||||||
|
onLike: (title, isLiked) =>
|
||||||
|
_showSnackBar(
|
||||||
|
context, title, isLiked),
|
||||||
|
onTap: () =>
|
||||||
|
_navToDetails(context, data),
|
||||||
|
)
|
||||||
|
: const SizedBox.shrink();
|
||||||
|
},
|
||||||
),
|
),
|
||||||
|
))),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
void _showSnackBar(BuildContext context, String title, bool isLiked) {
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||||
|
56
pubspec.lock
56
pubspec.lock
@ -38,6 +38,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.11.0"
|
version: "2.11.0"
|
||||||
|
bloc:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: bloc
|
||||||
|
sha256: "106842ad6569f0b60297619e9e0b1885c2fb9bf84812935490e6c5275777804e"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "8.1.4"
|
||||||
boolean_selector:
|
boolean_selector:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -158,6 +166,22 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.1.2"
|
version: "3.1.2"
|
||||||
|
copy_with_extension:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: copy_with_extension
|
||||||
|
sha256: fbcf890b0c34aedf0894f91a11a579994b61b4e04080204656b582708b5b1125
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.0.4"
|
||||||
|
copy_with_extension_gen:
|
||||||
|
dependency: "direct dev"
|
||||||
|
description:
|
||||||
|
name: copy_with_extension_gen
|
||||||
|
sha256: "51cd11094096d40824c8da629ca7f16f3b7cea5fc44132b679617483d43346b0"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "5.0.4"
|
||||||
crypto:
|
crypto:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -198,6 +222,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.0"
|
||||||
|
equatable:
|
||||||
|
dependency: "direct dev"
|
||||||
|
description:
|
||||||
|
name: equatable
|
||||||
|
sha256: "567c64b3cb4cf82397aac55f4f0cbd3ca20d77c6c03bedbc4ceaddc08904aef7"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.7"
|
||||||
fake_async:
|
fake_async:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -227,6 +259,14 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
flutter_bloc:
|
||||||
|
dependency: "direct dev"
|
||||||
|
description:
|
||||||
|
name: flutter_bloc
|
||||||
|
sha256: b594505eac31a0518bdcb4b5b79573b8d9117b193cc80cc12e17d639b10aa27a
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "8.1.6"
|
||||||
flutter_lints:
|
flutter_lints:
|
||||||
dependency: "direct dev"
|
dependency: "direct dev"
|
||||||
description:
|
description:
|
||||||
@ -392,6 +432,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.0.0"
|
version: "2.0.0"
|
||||||
|
nested:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: nested
|
||||||
|
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "1.0.0"
|
||||||
package_config:
|
package_config:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -424,6 +472,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.4.0"
|
version: "1.4.0"
|
||||||
|
provider:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: provider
|
||||||
|
sha256: c8a055ee5ce3fd98d6fc872478b03823ffdb448699c6ebdbbc71d59b596fd48c
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "6.1.2"
|
||||||
pub_semver:
|
pub_semver:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -47,6 +47,10 @@ dev_dependencies:
|
|||||||
build_runner: ^2.4.9
|
build_runner: ^2.4.9
|
||||||
json_serializable: ^6.7.1
|
json_serializable: ^6.7.1
|
||||||
|
|
||||||
|
equatable: ^2.0.5
|
||||||
|
flutter_bloc: ^8.1.5
|
||||||
|
copy_with_extension_gen: ^5.0.4
|
||||||
|
|
||||||
# The "flutter_lints" package below contains a set of recommended lints to
|
# The "flutter_lints" package below contains a set of recommended lints to
|
||||||
# encourage good coding practices. The lint set provided by the package is
|
# encourage good coding practices. The lint set provided by the package is
|
||||||
# activated in the `analysis_options.yaml` file located at the root of your
|
# activated in the `analysis_options.yaml` file located at the root of your
|
||||||
|
Loading…
x
Reference in New Issue
Block a user