process last lab
19
Makefile
Normal file
@ -0,0 +1,19 @@
|
||||
gen:
|
||||
flutter pub run build_runner build --delete-conflicting-outputs
|
||||
|
||||
icon:
|
||||
flutter pub run flutter_launcher_icons:main
|
||||
|
||||
init_res:
|
||||
dart pub global activate flutter_asset_generator
|
||||
|
||||
format:
|
||||
dart format . --line-length 100
|
||||
|
||||
res:
|
||||
fgen --output lib/components/resources.g.dart --no-watch --no-preview; \
|
||||
make format
|
||||
|
||||
loc:
|
||||
flutter gen-l10n; \
|
||||
make format
|
Before Width: | Height: | Size: 544 B After Width: | Height: | Size: 2.0 KiB |
Before Width: | Height: | Size: 442 B After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 721 B After Width: | Height: | Size: 2.7 KiB |
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 4.0 KiB |
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 5.3 KiB |
BIN
assets/launcher.png
Normal file
After Width: | Height: | Size: 13 KiB |
19
assets/svg/ru.svg
Normal file
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
|
||||
<svg width="800px" height="800px" viewBox="0 -4 28 28" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<g clip-path="url(#clip0_503_2726)">
|
||||
<rect x="0.25" y="0.25" width="27.5" height="19.5" rx="1.75" fill="white" stroke="#F5F5F5" stroke-width="0.5"/>
|
||||
<mask id="mask0_503_2726" style="mask-type:alpha" maskUnits="userSpaceOnUse" x="0" y="0" width="28" height="20">
|
||||
<rect x="0.25" y="0.25" width="27.5" height="19.5" rx="1.75" fill="white" stroke="white" stroke-width="0.5"/>
|
||||
</mask>
|
||||
<g mask="url(#mask0_503_2726)">
|
||||
<path fill-rule="evenodd" clip-rule="evenodd" d="M0 13.3333H28V6.66667H0V13.3333Z" fill="#0C47B7"/>
|
||||
<path fill-rule="evenodd" clip-rule="evenodd" d="M0 20H28V13.3333H0V20Z" fill="#E53B35"/>
|
||||
</g>
|
||||
</g>
|
||||
<defs>
|
||||
<clipPath id="clip0_503_2726">
|
||||
<rect width="28" height="20" rx="2" fill="white"/>
|
||||
</clipPath>
|
||||
</defs>
|
||||
</svg>
|
After Width: | Height: | Size: 968 B |
23
assets/svg/uk.svg
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="iso-8859-1"?>
|
||||
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
|
||||
<svg height="800px" width="800px" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||
viewBox="0 0 512 512" xml:space="preserve">
|
||||
<path style="fill:#41479B;" d="M473.655,88.276H38.345C17.167,88.276,0,105.443,0,126.621V385.38
|
||||
c0,21.177,17.167,38.345,38.345,38.345h435.31c21.177,0,38.345-17.167,38.345-38.345V126.621
|
||||
C512,105.443,494.833,88.276,473.655,88.276z"/>
|
||||
<path style="fill:#F5F5F5;" d="M511.469,120.282c-3.022-18.159-18.797-32.007-37.814-32.007h-9.977l-163.54,107.147V88.276h-88.276
|
||||
v107.147L48.322,88.276h-9.977c-19.017,0-34.792,13.847-37.814,32.007l139.778,91.58H0v88.276h140.309L0.531,391.717
|
||||
c3.022,18.159,18.797,32.007,37.814,32.007h9.977l163.54-107.147v107.147h88.276V316.577l163.54,107.147h9.977
|
||||
c19.017,0,34.792-13.847,37.814-32.007l-139.778-91.58H512v-88.276H371.691L511.469,120.282z"/>
|
||||
<g>
|
||||
<polygon style="fill:#FF4B55;" points="282.483,88.276 229.517,88.276 229.517,229.517 0,229.517 0,282.483 229.517,282.483
|
||||
229.517,423.724 282.483,423.724 282.483,282.483 512,282.483 512,229.517 282.483,229.517 "/>
|
||||
<path style="fill:#FF4B55;" d="M24.793,421.252l186.583-121.114h-32.428L9.224,410.31
|
||||
C13.377,415.157,18.714,418.955,24.793,421.252z"/>
|
||||
<path style="fill:#FF4B55;" d="M346.388,300.138H313.96l180.716,117.305c5.057-3.321,9.277-7.807,12.287-13.075L346.388,300.138z"
|
||||
/>
|
||||
<path style="fill:#FF4B55;" d="M4.049,109.475l157.73,102.387h32.428L15.475,95.842C10.676,99.414,6.749,104.084,4.049,109.475z"/>
|
||||
<path style="fill:#FF4B55;" d="M332.566,211.862l170.035-110.375c-4.199-4.831-9.578-8.607-15.699-10.86L300.138,211.862H332.566z"
|
||||
/>
|
||||
</g>
|
||||
</svg>
|
After Width: | Height: | Size: 1.7 KiB |
9
l10n/app_en.arb
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"@@locale": "en",
|
||||
|
||||
"search": "Search",
|
||||
"liked": "liked",
|
||||
"disliked": "disliked",
|
||||
|
||||
"arbEnding": "Чтобы не забыть про отсутствие запятой :)"
|
||||
}
|
9
l10n/app_ru.arb
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"@@locale": "ru",
|
||||
|
||||
"search": "Поиск",
|
||||
"liked": "поставлен лайк",
|
||||
"disliked": "убран лайк",
|
||||
|
||||
"arbEnding": "Чтобы не забыть про отсутствие запятой :)"
|
||||
}
|
22
lib/components/utils/debounce.dart
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
import 'dart:ui';
|
||||
import 'dart:async';
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -4,8 +4,9 @@ part 'words_dto.g.dart';
|
||||
@JsonSerializable(createToJson: false)
|
||||
class WordsDto{
|
||||
final List<WordDataDto>? data;
|
||||
final MetaDto? meta;
|
||||
|
||||
const WordsDto({this.data});
|
||||
const WordsDto({this.data, this.meta});
|
||||
|
||||
factory WordsDto.fromJson(Map<String, dynamic> json) => _$WordsDtoFromJson(json);
|
||||
}
|
||||
@ -32,4 +33,22 @@ class WordAttributesDataDto{
|
||||
factory WordAttributesDataDto.fromJson(Map<String, dynamic> json) => _$WordAttributesDataDtoFromJson(json);
|
||||
|
||||
WordAttributesDataDto(this.name, this.born, this.died, this.image);
|
||||
}
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class MetaDto {
|
||||
final PaginationDto? pagination;
|
||||
const MetaDto({this.pagination});
|
||||
factory MetaDto.fromJson(Map<String, dynamic> json) => _$MetaDtoFromJson(json);
|
||||
}
|
||||
|
||||
@JsonSerializable(createToJson: false)
|
||||
class PaginationDto {
|
||||
final int? current;
|
||||
final int? next;
|
||||
final int? last;
|
||||
|
||||
const PaginationDto({this.current, this.next, this.last});
|
||||
factory PaginationDto.fromJson(Map<String, dynamic> json) => _$PaginationDtoFromJson(json);
|
||||
}
|
||||
|
||||
|
@ -10,6 +10,9 @@ WordsDto _$WordsDtoFromJson(Map<String, dynamic> json) => WordsDto(
|
||||
data: (json['data'] as List<dynamic>?)
|
||||
?.map((e) => WordDataDto.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
meta: json['meta'] == null
|
||||
? null
|
||||
: MetaDto.fromJson(json['meta'] as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
WordDataDto _$WordDataDtoFromJson(Map<String, dynamic> json) => WordDataDto(
|
||||
@ -29,3 +32,16 @@ WordAttributesDataDto _$WordAttributesDataDtoFromJson(
|
||||
json['died'] as String?,
|
||||
json['image'] as String?,
|
||||
);
|
||||
|
||||
MetaDto _$MetaDtoFromJson(Map<String, dynamic> json) => MetaDto(
|
||||
pagination: json['pagination'] == null
|
||||
? null
|
||||
: PaginationDto.fromJson(json['pagination'] as Map<String, dynamic>),
|
||||
);
|
||||
|
||||
PaginationDto _$PaginationDtoFromJson(Map<String, dynamic> json) =>
|
||||
PaginationDto(
|
||||
current: (json['current'] as num?)?.toInt(),
|
||||
next: (json['next'] as num?)?.toInt(),
|
||||
last: (json['last'] as num?)?.toInt(),
|
||||
);
|
||||
|
@ -1,5 +1,6 @@
|
||||
import 'package:card_app/data/dtos/words_dto.dart';
|
||||
import 'package:card_app/domain/models/card.dart';
|
||||
import 'package:card_app/domain/models/home.dart';
|
||||
|
||||
const _imagePlaceholder = 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS69a8OWnRueQNykBpuyfbGxl3gt5ct46NOHg&s';
|
||||
extension WordDataDtoToModel on WordDataDto{
|
||||
@ -8,4 +9,9 @@ extension WordDataDtoToModel on WordDataDto{
|
||||
image: attributes?.image ?? _imagePlaceholder,
|
||||
translation: '${attributes?.born}',
|
||||
);
|
||||
}
|
||||
extension WordsDtoToModel on WordsDto{
|
||||
HomeData toDomain() => HomeData(
|
||||
data: data?.map((e) => e.toDomain()).toList(),
|
||||
nextPage: meta?.pagination?.next,);
|
||||
}
|
10
lib/domain/models/home.dart
Normal file
@ -0,0 +1,10 @@
|
||||
|
||||
import 'card.dart';
|
||||
|
||||
class HomeData{
|
||||
final List<CardData>? data;
|
||||
final int? nextPage;
|
||||
|
||||
HomeData({this.data, this.nextPage});
|
||||
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
import 'package:card_app/presentation/home_page/bloc/bloc.dart';
|
||||
import 'package:card_app/presentation/home_page/home_page.dart';
|
||||
import 'package:card_app/repositories/WordsRepository.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MyApp());
|
||||
@ -18,7 +21,15 @@ class MyApp extends StatelessWidget {
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.indigoAccent),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: const MyHomePage(title: 'Валова Анна Дмитриевна'),
|
||||
home: RepositoryProvider<WordsRepository>(
|
||||
lazy: true,
|
||||
create: (_) => WordsRepository(),
|
||||
child: BlocProvider<HomeBloc>(
|
||||
lazy: false,
|
||||
create: (context) => HomeBloc(context.read<WordsRepository>()),
|
||||
child: MyHomePage(title: 'FlashCardsApp'),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
|
||||
|
||||
import 'package:card_app/presentation/home_page/bloc/events.dart';
|
||||
import 'package:card_app/presentation/home_page/bloc/state.dart';
|
||||
import 'package:card_app/repositories/WordsRepository.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class HomeBloc extends Bloc<HomeEvent, HomeState>{
|
||||
final WordsRepository repo;
|
||||
|
||||
HomeBloc(this.repo) : super(const HomeState()){
|
||||
on<HomeLoadDataEvent>(_onLoadData);
|
||||
|
||||
}
|
||||
|
||||
Future<void> _onLoadData(HomeLoadDataEvent event, Emitter<HomeState> emit) async{
|
||||
|
||||
if (event.nextPage == null){
|
||||
emit(state.copyWith(isLoading: true));
|
||||
} else{
|
||||
emit(state.copyWith(isPaginationLoading: true));
|
||||
}
|
||||
|
||||
String? error;
|
||||
final data = await repo.loadData(
|
||||
q: event.search,
|
||||
page: event.nextPage ?? 1,
|
||||
onError: (e) => error = e);
|
||||
|
||||
if (event.nextPage != null){
|
||||
data?.data?.insertAll(0, state.data?.data ?? []);
|
||||
}
|
||||
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
isPaginationLoading: false,
|
||||
data: data,
|
||||
error: error,
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -4,5 +4,7 @@ abstract class HomeEvent{
|
||||
}
|
||||
|
||||
class HomeLoadDataEvent extends HomeEvent {
|
||||
const HomeLoadDataEvent();
|
||||
final String? search;
|
||||
final int? nextPage;
|
||||
const HomeLoadDataEvent({this.search, this.nextPage});
|
||||
}
|
@ -1,9 +1,25 @@
|
||||
|
||||
|
||||
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||
import 'package:card_app/domain/models/card.dart';
|
||||
import 'package:card_app/domain/models/home.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
part 'state.g.dart';
|
||||
|
||||
@CopyWith()
|
||||
class HomeState extends Equatable {
|
||||
final Future<List<CardData>?>? data;
|
||||
const HomeState({this.data});
|
||||
HomeState copyWith({Future<List<CardData>?>? data})
|
||||
final HomeData? data;
|
||||
final bool isLoading;
|
||||
final bool isPaginationLoading;
|
||||
final String? error;
|
||||
|
||||
const HomeState({
|
||||
this.data,
|
||||
this.isLoading = false,
|
||||
this.isPaginationLoading = false,
|
||||
this.error});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [data, isLoading, isPaginationLoading, error];
|
||||
|
||||
}
|
92
lib/presentation/home_page/bloc/state.g.dart
Normal file
@ -0,0 +1,92 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'state.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// CopyWithGenerator
|
||||
// **************************************************************************
|
||||
|
||||
abstract class _$HomeStateCWProxy {
|
||||
HomeState data(HomeData? data);
|
||||
|
||||
HomeState isLoading(bool isLoading);
|
||||
|
||||
HomeState isPaginationLoading(bool isPaginationLoading);
|
||||
|
||||
HomeState error(String? error);
|
||||
|
||||
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `HomeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||
///
|
||||
/// Usage
|
||||
/// ```dart
|
||||
/// HomeState(...).copyWith(id: 12, name: "My name")
|
||||
/// ````
|
||||
HomeState call({
|
||||
HomeData? data,
|
||||
bool? isLoading,
|
||||
bool? isPaginationLoading,
|
||||
String? error,
|
||||
});
|
||||
}
|
||||
|
||||
/// Proxy class for `copyWith` functionality. This is a callable class and can be used as follows: `instanceOfHomeState.copyWith(...)`. Additionally contains functions for specific fields e.g. `instanceOfHomeState.copyWith.fieldName(...)`
|
||||
class _$HomeStateCWProxyImpl implements _$HomeStateCWProxy {
|
||||
const _$HomeStateCWProxyImpl(this._value);
|
||||
|
||||
final HomeState _value;
|
||||
|
||||
@override
|
||||
HomeState data(HomeData? data) => this(data: data);
|
||||
|
||||
@override
|
||||
HomeState isLoading(bool isLoading) => this(isLoading: isLoading);
|
||||
|
||||
@override
|
||||
HomeState isPaginationLoading(bool isPaginationLoading) =>
|
||||
this(isPaginationLoading: isPaginationLoading);
|
||||
|
||||
@override
|
||||
HomeState error(String? error) => this(error: error);
|
||||
|
||||
@override
|
||||
|
||||
/// This function **does support** nullification of nullable fields. All `null` values passed to `non-nullable` fields will be ignored. You can also use `HomeState(...).copyWith.fieldName(...)` to override fields one at a time with nullification support.
|
||||
///
|
||||
/// Usage
|
||||
/// ```dart
|
||||
/// HomeState(...).copyWith(id: 12, name: "My name")
|
||||
/// ````
|
||||
HomeState call({
|
||||
Object? data = const $CopyWithPlaceholder(),
|
||||
Object? isLoading = const $CopyWithPlaceholder(),
|
||||
Object? isPaginationLoading = const $CopyWithPlaceholder(),
|
||||
Object? error = const $CopyWithPlaceholder(),
|
||||
}) {
|
||||
return HomeState(
|
||||
data: data == const $CopyWithPlaceholder()
|
||||
? _value.data
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: data as HomeData?,
|
||||
isLoading: isLoading == const $CopyWithPlaceholder() || isLoading == null
|
||||
? _value.isLoading
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: isLoading as bool,
|
||||
isPaginationLoading:
|
||||
isPaginationLoading == const $CopyWithPlaceholder() ||
|
||||
isPaginationLoading == null
|
||||
? _value.isPaginationLoading
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: isPaginationLoading as bool,
|
||||
error: error == const $CopyWithPlaceholder()
|
||||
? _value.error
|
||||
// ignore: cast_nullable_to_non_nullable
|
||||
: error as String?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
extension $HomeStateCopyWith on HomeState {
|
||||
/// Returns a callable class that can be used as follows: `instanceOfHomeState.copyWith(...)` or like so:`instanceOfHomeState.copyWith.fieldName(...)`.
|
||||
// ignore: library_private_types_in_public_api
|
||||
_$HomeStateCWProxy get copyWith => _$HomeStateCWProxyImpl(this);
|
||||
}
|
@ -1,10 +1,15 @@
|
||||
import 'package:card_app/components/utils/debounce.dart';
|
||||
import 'package:card_app/domain/models/card.dart';
|
||||
import 'package:card_app/presentation/home_page/bloc/bloc.dart';
|
||||
import 'package:card_app/presentation/home_page/bloc/events.dart';
|
||||
import 'package:card_app/repositories/WordsRepository.dart';
|
||||
import 'package:card_app/repositories/mock_repository.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../details_page/details_page.dart';
|
||||
import 'bloc/state.dart';
|
||||
|
||||
part 'card.dart';
|
||||
|
||||
@ -38,68 +43,89 @@ class Body extends StatefulWidget {
|
||||
|
||||
class _BodyState extends State<Body> {
|
||||
final SearchController searchController = SearchController();
|
||||
Future<List<CardData>?>? data;
|
||||
final scrollController = ScrollController();
|
||||
// Future<List<CardData>?>? data;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||
});
|
||||
scrollController.addListener(_onNextPageListener);
|
||||
super.initState();
|
||||
data = WordsRepository().loadData(); // Инициализация данных
|
||||
//data = WordsRepository().loadData(); // Инициализация данных
|
||||
}
|
||||
|
||||
void _onNextPageListener(){
|
||||
if (scrollController.offset > scrollController.position.maxScrollExtent - 300){
|
||||
final bloc = context.read<HomeBloc>();
|
||||
if (!bloc.state.isPaginationLoading){
|
||||
bloc.add(HomeLoadDataEvent(
|
||||
search: searchController.text,
|
||||
nextPage: bloc.state.data?.nextPage,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
searchController.dispose();
|
||||
scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
||||
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
|
||||
child: Column(
|
||||
children: [
|
||||
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
|
||||
child: Column(children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(12),
|
||||
child: CupertinoSearchTextField(
|
||||
onChanged: (search) {
|
||||
setState(() {
|
||||
Debounce.run(() =>
|
||||
context.read<HomeBloc>().add(HomeLoadDataEvent(search: search)));
|
||||
|
||||
data = WordsRepository().loadData(q: search);
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
Expanded(
|
||||
child: Center(
|
||||
child: FutureBuilder<List<CardData>?>(
|
||||
future: data,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const CircularProgressIndicator(); // Индикатор загрузки
|
||||
} else if (snapshot.hasError) {
|
||||
return Text('Ошибка: ${snapshot.error}'); // Обработка ошибок
|
||||
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
|
||||
return const Text('Нет данных'); // Сообщение при отсутствии данных
|
||||
} else {
|
||||
return SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: snapshot.data!.map((data) {
|
||||
return _Card.fromData(
|
||||
data,
|
||||
onKnow: (title, isKnow) =>
|
||||
_showSnackBar(context, title, isKnow),
|
||||
onTap: () => _navToDetails(context, data),
|
||||
);
|
||||
}).toList(),
|
||||
BlocBuilder<HomeBloc, HomeState>(
|
||||
builder: (context, state) => state.error != null
|
||||
? Text(state.error ?? '',
|
||||
style: Theme.of(context).textTheme.headlineLarge?.copyWith(color: Colors.red))
|
||||
|
||||
: state.isLoading
|
||||
? const CircularProgressIndicator()
|
||||
: Expanded(
|
||||
child: RefreshIndicator(
|
||||
onRefresh: _onRefresh,
|
||||
child: ListView.builder(
|
||||
controller: scrollController,
|
||||
padding: EdgeInsets.zero,
|
||||
itemCount: state.data?.data?.length ?? 0,
|
||||
itemBuilder: (context, index) {
|
||||
final data = state.data?.data?[index];
|
||||
return data != null
|
||||
? _Card.fromData(
|
||||
data,
|
||||
onKnow: (title, isKnown) =>
|
||||
_showSnackBar(context, title, isKnown),
|
||||
onTap: () => _navToDetails(context, data),
|
||||
)
|
||||
: const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
BlocBuilder<HomeBloc, HomeState>(
|
||||
builder: (context, state) => state.isPaginationLoading
|
||||
? const CircularProgressIndicator()
|
||||
: const SizedBox.shrink(),)
|
||||
|
||||
]));
|
||||
}
|
||||
|
||||
void _navToDetails(BuildContext context, CardData data) {
|
||||
@ -121,6 +147,11 @@ class _BodyState extends State<Body> {
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _onRefresh(){
|
||||
context.read<HomeBloc>().add(HomeLoadDataEvent(search: searchController.text));
|
||||
return Future.value(null);
|
||||
}
|
||||
}
|
||||
|
||||
class _CardState extends State<_Card> {
|
||||
@ -184,7 +215,6 @@ class _CardState extends State<_Card> {
|
||||
),
|
||||
Align(
|
||||
alignment: Alignment.center,
|
||||
|
||||
)
|
||||
],
|
||||
);
|
||||
|
@ -6,6 +6,7 @@ import 'package:dio/dio.dart';
|
||||
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||
|
||||
import '../domain/models/card.dart';
|
||||
import '../domain/models/home.dart';
|
||||
|
||||
class WordsRepository extends ApiInterface{
|
||||
static final Dio _dio = Dio()
|
||||
@ -19,7 +20,12 @@ class WordsRepository extends ApiInterface{
|
||||
|
||||
|
||||
@override
|
||||
Future<List<CardData>?> loadData({String? q, int pageSize = 5}) async{
|
||||
Future<HomeData?> loadData({
|
||||
OnErrorCallback? onError,
|
||||
String? q,
|
||||
int page = 1,
|
||||
int pageSize = 5,
|
||||
}) async{
|
||||
try {
|
||||
const String url = '$_baseUrl/v1/characters';
|
||||
|
||||
@ -27,13 +33,15 @@ class WordsRepository extends ApiInterface{
|
||||
url,
|
||||
queryParameters: {
|
||||
'filter[name_cont]': q,
|
||||
'page[number]': page,
|
||||
'page[size]': pageSize
|
||||
},
|
||||
);
|
||||
final WordsDto dto = WordsDto.fromJson(response.data as Map<String, dynamic>);
|
||||
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
|
||||
final HomeData data = dto.toDomain();
|
||||
return data;
|
||||
} on DioException catch (e) {
|
||||
onError?.call(e.error?.toString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
import 'package:card_app/domain/models/card.dart';
|
||||
import 'package:card_app/domain/models/home.dart';
|
||||
|
||||
typedef OnErrorCallback = void Function(String? error);
|
||||
|
||||
abstract class ApiInterface{
|
||||
Future<List<CardData>?> loadData();
|
||||
Future<HomeData?> loadData({OnErrorCallback? onError});
|
||||
|
||||
}
|
@ -1,31 +1,30 @@
|
||||
|
||||
import 'package:card_app/repositories/api_interface.dart';
|
||||
|
||||
import '../domain/models/card.dart';
|
||||
import '../domain/models/home.dart';
|
||||
|
||||
class MockRepository extends ApiInterface{
|
||||
class MockRepository extends ApiInterface {
|
||||
@override
|
||||
Future<List<CardData>?> loadData({String q = ''}) async{
|
||||
return [
|
||||
Future<HomeData?> loadData({OnErrorCallback? onError}) async {
|
||||
return HomeData(data: [
|
||||
CardData(
|
||||
word: 'Hello',
|
||||
translation: 'Привет',
|
||||
image:
|
||||
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS69a8OWnRueQNykBpuyfbGxl3gt5ct46NOHg&s',
|
||||
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS69a8OWnRueQNykBpuyfbGxl3gt5ct46NOHg&s',
|
||||
mark: 10,
|
||||
),
|
||||
CardData(
|
||||
word: 'Book',
|
||||
translation: 'Книга',
|
||||
image:
|
||||
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSfbH7Ckkbzrr3KZlb144GTaAmJnz87U9opGw&s',
|
||||
'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSfbH7Ckkbzrr3KZlb144GTaAmJnz87U9opGw&s',
|
||||
mark: 0),
|
||||
CardData(
|
||||
word: 'Cat',
|
||||
translation: 'Кот',
|
||||
image:
|
||||
'https://upload.wikimedia.org/wikipedia/commons/0/0e/Felis_silvestris_silvestris.jpg'),
|
||||
|
||||
];
|
||||
'https://upload.wikimedia.org/wikipedia/commons/0/0e/Felis_silvestris_silvestris.jpg'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
104
pubspec.lock
@ -22,6 +22,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "6.7.0"
|
||||
archive:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: archive
|
||||
sha256: cb6a278ef2dbb298455e1a713bda08524a175630ec643a242c399c932a0a1f7d
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.6.1"
|
||||
args:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -38,6 +46,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:
|
||||
@ -126,6 +142,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.3"
|
||||
cli_util:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: cli_util
|
||||
sha256: ff6785f7e9e3c38ac98b2fb035701789de90154024a75b6cb926445e83197d1c
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.4.2"
|
||||
clock:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -158,6 +182,22 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
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 main"
|
||||
description:
|
||||
name: copy_with_extension_gen
|
||||
sha256: "51cd11094096d40824c8da629ca7f16f3b7cea5fc44132b679617483d43346b0"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.0.4"
|
||||
crypto:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -198,6 +238,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.0"
|
||||
equatable:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: equatable
|
||||
sha256: "567c64b3cb4cf82397aac55f4f0cbd3ca20d77c6c03bedbc4ceaddc08904aef7"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.7"
|
||||
fake_async:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -227,6 +275,22 @@ 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_launcher_icons:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
name: flutter_launcher_icons
|
||||
sha256: "526faf84284b86a4cb36d20a5e45147747b7563d921373d4ee0559c54fcdbcea"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.13.1"
|
||||
flutter_lints:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
@ -280,6 +344,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.0.2"
|
||||
image:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: image
|
||||
sha256: f31d52537dc417fdcde36088fdf11d191026fd5e4fae742491ebd40e5a8bea7d
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.3.0"
|
||||
io:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -392,6 +464,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:
|
||||
@ -408,6 +488,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.9.0"
|
||||
petitparser:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: petitparser
|
||||
sha256: c15605cd28af66339f8eb6fbe0e541bfe2d1b72d5825efc6598f3e0a31b9ad27
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "6.0.2"
|
||||
pool:
|
||||
dependency: transitive
|
||||
description:
|
||||
@ -424,6 +512,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:
|
||||
@ -597,6 +693,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.0.1"
|
||||
xml:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: xml
|
||||
sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "6.5.0"
|
||||
yaml:
|
||||
dependency: transitive
|
||||
description:
|
||||
|
52
pubspec.yaml
@ -39,12 +39,26 @@ dependencies:
|
||||
dio: ^5.4.2+1
|
||||
pretty_dio_logger: ^1.3.1
|
||||
|
||||
#BLoC
|
||||
equatable: ^2.0.5
|
||||
flutter_bloc: ^8.1.5
|
||||
|
||||
copy_with_extension_gen: ^5.0.4
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
build_runner: ^2.4.9
|
||||
json_serializable: ^6.7.1
|
||||
|
||||
# Иконки
|
||||
flutter_launcher_icons: 0.13.1
|
||||
|
||||
|
||||
# The "flutter_lints" package below contains a set of recommended lints to
|
||||
# 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
|
||||
@ -54,42 +68,18 @@ dev_dependencies:
|
||||
|
||||
# For information on the generic Dart part of this file, see the
|
||||
# following page: https://dart.dev/tools/pub/pubspec
|
||||
|
||||
flutter_icons:
|
||||
android: "ic_launcher"
|
||||
image_path: "assets/launcher.png"
|
||||
min_sdk_android: 21
|
||||
# The following section is specific to Flutter packages.
|
||||
flutter:
|
||||
|
||||
# The following line ensures that the Material Icons font is
|
||||
# included with your application, so that you can use the icons in
|
||||
# the material Icons class.
|
||||
generate: true
|
||||
uses-material-design: true
|
||||
assets:
|
||||
- assets/svg/
|
||||
|
||||
# To add assets to your application, add an assets section, like this:
|
||||
# assets:
|
||||
# - images/a_dot_burr.jpeg
|
||||
# - images/a_dot_ham.jpeg
|
||||
|
||||
# An image asset can refer to one or more resolution-specific "variants", see
|
||||
# https://flutter.dev/to/resolution-aware-images
|
||||
|
||||
# For details regarding adding assets from package dependencies, see
|
||||
# https://flutter.dev/to/asset-from-package
|
||||
|
||||
# To add custom fonts to your application, add a fonts section here,
|
||||
# in this "flutter" section. Each entry in this list should have a
|
||||
# "family" key with the font family name, and a "fonts" key with a
|
||||
# list giving the asset and other descriptors for the font. For
|
||||
# example:
|
||||
# fonts:
|
||||
# - family: Schyler
|
||||
# fonts:
|
||||
# - asset: fonts/Schyler-Regular.ttf
|
||||
# - asset: fonts/Schyler-Italic.ttf
|
||||
# style: italic
|
||||
# - family: Trajan Pro
|
||||
# fonts:
|
||||
# - asset: fonts/TrajanPro.ttf
|
||||
# - asset: fonts/TrajanPro_Bold.ttf
|
||||
# weight: 700
|
||||
#
|
||||
# For details regarding fonts from package dependencies,
|
||||
# see https://flutter.dev/to/font-from-package
|
||||
|