6 лаба ура победа
This commit is contained in:
parent
d64abe5ff6
commit
57d946a8a2
20
flutter_app/lib/components/utils/debounce.dart
Normal file
20
flutter_app/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);
|
||||||
|
}
|
||||||
|
}
|
@ -3,26 +3,56 @@ import 'package:json_annotation/json_annotation.dart';
|
|||||||
part 'thrones_character_dto.g.dart';
|
part 'thrones_character_dto.g.dart';
|
||||||
|
|
||||||
@JsonSerializable(createToJson: false)
|
@JsonSerializable(createToJson: false)
|
||||||
class ThronesCharacterDto {
|
class ThronesCharactersDto {
|
||||||
final int id;
|
final List<ThronesCharacterDataDto> characters;
|
||||||
final String firstName;
|
final InfoDto? info;
|
||||||
final String lastName;
|
|
||||||
final String fullName;
|
|
||||||
final String title;
|
|
||||||
final String family;
|
|
||||||
final String image;
|
|
||||||
final String imageUrl;
|
|
||||||
|
|
||||||
ThronesCharacterDto({
|
const ThronesCharactersDto({required this.characters, this.info});
|
||||||
required this.id,
|
|
||||||
required this.firstName,
|
|
||||||
required this.lastName,
|
|
||||||
required this.fullName,
|
|
||||||
required this.title,
|
|
||||||
required this.family,
|
|
||||||
required this.image,
|
|
||||||
required this.imageUrl,
|
|
||||||
});
|
|
||||||
|
|
||||||
factory ThronesCharacterDto.fromJson(Map<String, dynamic> json) => _$ThronesCharacterDtoFromJson(json);
|
factory ThronesCharactersDto.fromJson(List<dynamic> json) {
|
||||||
|
final List<ThronesCharacterDataDto> characters = json
|
||||||
|
.map((item) => ThronesCharacterDataDto.fromJson(item as Map<String, dynamic>))
|
||||||
|
.toList();
|
||||||
|
return ThronesCharactersDto(characters: characters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class ThronesCharacterDataDto {
|
||||||
|
final int? id;
|
||||||
|
final String? firstName;
|
||||||
|
final String? lastName;
|
||||||
|
final String? fullName;
|
||||||
|
final String? title;
|
||||||
|
final String? family;
|
||||||
|
final String? image;
|
||||||
|
final String? imageUrl;
|
||||||
|
|
||||||
|
const ThronesCharacterDataDto(
|
||||||
|
{this.id, this.firstName, this.lastName, this.fullName, this.title, this.family, this.image, this.imageUrl,});
|
||||||
|
|
||||||
|
factory ThronesCharacterDataDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$ThronesCharacterDataDtoFromJson(json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@JsonSerializable(createToJson: false)
|
||||||
|
class InfoDto {
|
||||||
|
final String? next;
|
||||||
|
final String? last;
|
||||||
|
final int? nextPage;
|
||||||
|
final int? lastPage;
|
||||||
|
|
||||||
|
InfoDto({this.next, this.last})
|
||||||
|
: nextPage = _extractPageNumber(next),
|
||||||
|
lastPage = _extractPageNumber(last);
|
||||||
|
|
||||||
|
static int? _extractPageNumber(String? url) {
|
||||||
|
if (url == null) return null;
|
||||||
|
final RegExp regExp = RegExp(r'page=(\d+)');
|
||||||
|
final Match? match = regExp.firstMatch(url);
|
||||||
|
return match != null ? int.parse(match.group(1)!) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
factory InfoDto.fromJson(Map<String, dynamic> json) =>
|
||||||
|
_$InfoDtoFromJson(json);
|
||||||
}
|
}
|
@ -6,14 +6,32 @@ part of 'thrones_character_dto.dart';
|
|||||||
// JsonSerializableGenerator
|
// JsonSerializableGenerator
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
ThronesCharacterDto _$ThronesCharacterDtoFromJson(Map<String, dynamic> json) =>
|
ThronesCharactersDto _$ThronesCharactersDtoFromJson(
|
||||||
ThronesCharacterDto(
|
Map<String, dynamic> json) =>
|
||||||
id: (json['id'] as num).toInt(),
|
ThronesCharactersDto(
|
||||||
firstName: json['firstName'] as String,
|
characters: (json['characters'] as List<dynamic>)
|
||||||
lastName: json['lastName'] as String,
|
.map((e) =>
|
||||||
fullName: json['fullName'] as String,
|
ThronesCharacterDataDto.fromJson(e as Map<String, dynamic>))
|
||||||
title: json['title'] as String,
|
.toList(),
|
||||||
family: json['family'] as String,
|
info: json['info'] == null
|
||||||
image: json['image'] as String,
|
? null
|
||||||
imageUrl: json['imageUrl'] as String,
|
: InfoDto.fromJson(json['info'] as Map<String, dynamic>),
|
||||||
|
);
|
||||||
|
|
||||||
|
ThronesCharacterDataDto _$ThronesCharacterDataDtoFromJson(
|
||||||
|
Map<String, dynamic> json) =>
|
||||||
|
ThronesCharacterDataDto(
|
||||||
|
id: (json['id'] as num?)?.toInt(),
|
||||||
|
firstName: json['firstName'] as String?,
|
||||||
|
lastName: json['lastName'] as String?,
|
||||||
|
fullName: json['fullName'] as String?,
|
||||||
|
title: json['title'] as String?,
|
||||||
|
family: json['family'] as String?,
|
||||||
|
image: json['image'] as String?,
|
||||||
|
imageUrl: json['imageUrl'] as String?,
|
||||||
|
);
|
||||||
|
|
||||||
|
InfoDto _$InfoDtoFromJson(Map<String, dynamic> json) => InfoDto(
|
||||||
|
next: json['next'] as String?,
|
||||||
|
last: json['last'] as String?,
|
||||||
);
|
);
|
||||||
|
@ -1,18 +1,19 @@
|
|||||||
import 'package:flutter_app/data/dtos/thrones_character_dto.dart';
|
import 'package:flutter_app/data/dtos/thrones_character_dto.dart';
|
||||||
import 'package:flutter_app/domain/models/card.dart';
|
import 'package:flutter_app/domain/models/card.dart';
|
||||||
|
import 'package:flutter_app/domain/models/home.dart';
|
||||||
|
|
||||||
const _imagePlaceholder =
|
const _imagePlaceholder =
|
||||||
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
||||||
|
|
||||||
extension ThronesCharacterDtoToModel on ThronesCharacterDto {
|
extension ThronesCharacterDtoToModel on ThronesCharacterDataDto {
|
||||||
CardData toDomain() => CardData(
|
CardData toDomain() => CardData(
|
||||||
descriptionText: _makeDescriptionText(title, family),
|
descriptionText: _makeDescriptionText(title, family),
|
||||||
imageUrl: imageUrl,
|
|
||||||
firstName: firstName,
|
firstName: firstName,
|
||||||
lastName: lastName,
|
lastName: lastName,
|
||||||
title: title,
|
title: title,
|
||||||
family: family,
|
family: family,
|
||||||
fullName: fullName,
|
fullName: fullName,
|
||||||
|
imageUrl: imageUrl,
|
||||||
text: '',
|
text: '',
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -25,4 +26,11 @@ extension ThronesCharacterDtoToModel on ThronesCharacterDto {
|
|||||||
? 'Family: $family'
|
? 'Family: $family'
|
||||||
: '';
|
: '';
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension ThronesCharactersDtoToModel on ThronesCharactersDto {
|
||||||
|
HomeData toDomain() => HomeData(
|
||||||
|
data: characters.map((e) => e.toDomain()).toList(),
|
||||||
|
nextPage: info?.nextPage,
|
||||||
|
);
|
||||||
}
|
}
|
@ -1,7 +1,8 @@
|
|||||||
import 'package:flutter_app/domain/models/card.dart';
|
import 'package:flutter_app/domain/models/card.dart';
|
||||||
|
import 'package:flutter_app/domain/models/home.dart';
|
||||||
|
|
||||||
typedef OnErrorCallback = void Function(String? error);
|
typedef OnErrorCallback = void Function(String? error);
|
||||||
|
|
||||||
abstract class ApiInterface {
|
abstract class ApiInterface {
|
||||||
Future<List<CardData>?> loadData({OnErrorCallback? onError});
|
Future<HomeData?> loadData({OnErrorCallback? onError});
|
||||||
}
|
}
|
@ -3,34 +3,48 @@ import 'package:flutter_app/data/dtos/thrones_character_dto.dart';
|
|||||||
import 'package:flutter_app/data/mappers/characters_mapper.dart';
|
import 'package:flutter_app/data/mappers/characters_mapper.dart';
|
||||||
import 'package:flutter_app/data/repositories/api_interface.dart';
|
import 'package:flutter_app/data/repositories/api_interface.dart';
|
||||||
import 'package:flutter_app/domain/models/card.dart';
|
import 'package:flutter_app/domain/models/card.dart';
|
||||||
|
import 'package:flutter_app/domain/models/home.dart';
|
||||||
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
||||||
|
|
||||||
class ThronesRepository extends ApiInterface {
|
class ThronesRepository extends ApiInterface {
|
||||||
static final Dio _dio = Dio();
|
static final Dio _dio = Dio()
|
||||||
|
..interceptors.add(PrettyDioLogger(
|
||||||
|
requestHeader: true,
|
||||||
|
requestBody: true,
|
||||||
|
));
|
||||||
|
|
||||||
static const String _baseUrl = 'https://thronesapi.com';
|
static const String _baseUrl = 'https://thronesapi.com';
|
||||||
|
|
||||||
static const _imagePlaceholder =
|
|
||||||
'https://upload.wikimedia.org/wikipedia/en/archive/b/b1/20210811082420%21Portrait_placeholder.png';
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<CardData>?> loadData({String? q, OnErrorCallback? onError}) async {
|
Future<HomeData?> loadData({
|
||||||
|
OnErrorCallback? onError,
|
||||||
|
String? q,
|
||||||
|
int page = 1,
|
||||||
|
}) async {
|
||||||
try {
|
try {
|
||||||
const String url = '$_baseUrl/api/v2/Characters';
|
const String url = '$_baseUrl/api/v2/Characters';
|
||||||
|
|
||||||
final Response<dynamic> response = await _dio.get<List<dynamic>>(url);
|
final Response<dynamic> response = await _dio.get<List<dynamic>>(
|
||||||
|
url,
|
||||||
|
);
|
||||||
|
|
||||||
final List<ThronesCharacterDto> characters = (response.data as List<dynamic>)
|
final List<ThronesCharacterDataDto> characters = (response.data as List<dynamic>)
|
||||||
.map((e) => ThronesCharacterDto.fromJson(e as Map<String, dynamic>))
|
.map((e) => ThronesCharacterDataDto.fromJson(e as Map<String, dynamic>))
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
final List<CardData> data = characters
|
final List<CardData> data = characters
|
||||||
.where((character) => q == null || character.fullName.toLowerCase().contains(q.toLowerCase()))
|
.where((character) => q == null || character.fullName!.toLowerCase().contains(q.toLowerCase()))
|
||||||
.map((e) => e.toDomain())
|
.map((e) => e.toDomain())
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
return data;
|
final HomeData homeData = HomeData(
|
||||||
|
data: data,
|
||||||
|
nextPage: null, // Если API не поддерживает пагинацию, то nextPage будет null
|
||||||
|
);
|
||||||
|
|
||||||
|
return homeData;
|
||||||
} on DioException catch (e) {
|
} on DioException catch (e) {
|
||||||
onError?.call(e.response?.statusMessage);
|
onError?.call(e.error?.toString());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,74 +2,74 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter_app/data/repositories/api_interface.dart';
|
import 'package:flutter_app/data/repositories/api_interface.dart';
|
||||||
import 'package:flutter_app/domain/models/card.dart';
|
import 'package:flutter_app/domain/models/card.dart';
|
||||||
|
|
||||||
class MockRepository extends ApiInterface {
|
// class MockRepository extends ApiInterface {
|
||||||
@override
|
// @override
|
||||||
Future<List<CardData>?> loadData({OnErrorCallback? onError}) async {
|
// Future<List<CardData>?> loadData({OnErrorCallback? onError}) async {
|
||||||
return [
|
// return [
|
||||||
CardData(
|
// CardData(
|
||||||
fullName: 'Daenerys Targaryen',
|
// fullName: 'Daenerys Targaryen',
|
||||||
descriptionText: 'Mother of Dragons',
|
// descriptionText: 'Mother of Dragons',
|
||||||
icon: Icons.favorite,
|
// icon: Icons.favorite,
|
||||||
imageUrl:
|
// imageUrl:
|
||||||
'https://thronesapi.com/assets/images/daenerys.jpg',
|
// 'https://thronesapi.com/assets/images/daenerys.jpg',
|
||||||
text: '', firstName: '', lastName: '', title: '', family: '',
|
// text: '', firstName: '', lastName: '', title: '', family: '',
|
||||||
),
|
// ),
|
||||||
CardData(
|
// CardData(
|
||||||
fullName: 'Samwell Tarly',
|
// fullName: 'Samwell Tarly',
|
||||||
descriptionText: 'Maester',
|
// descriptionText: 'Maester',
|
||||||
icon: Icons.favorite,
|
// icon: Icons.favorite,
|
||||||
imageUrl:
|
// imageUrl:
|
||||||
'https://thronesapi.com/assets/images/sam.jpg',
|
// 'https://thronesapi.com/assets/images/sam.jpg',
|
||||||
text: '', firstName: '', lastName: '', title: '', family: '',
|
// text: '', firstName: '', lastName: '', title: '', family: '',
|
||||||
),
|
// ),
|
||||||
CardData(
|
// CardData(
|
||||||
fullName: 'Jon Snow',
|
// fullName: 'Jon Snow',
|
||||||
descriptionText: 'King of the North',
|
// descriptionText: 'King of the North',
|
||||||
icon: Icons.favorite,
|
// icon: Icons.favorite,
|
||||||
imageUrl:
|
// imageUrl:
|
||||||
'https://thronesapi.com/assets/images/jon-snow.jpg',
|
// 'https://thronesapi.com/assets/images/jon-snow.jpg',
|
||||||
text: '', firstName: '', lastName: '', title: '', family: '',
|
// text: '', firstName: '', lastName: '', title: '', family: '',
|
||||||
),
|
// ),
|
||||||
CardData(
|
// CardData(
|
||||||
fullName: 'Arya Stark',
|
// fullName: 'Arya Stark',
|
||||||
descriptionText: 'No One',
|
// descriptionText: 'No One',
|
||||||
icon: Icons.favorite,
|
// icon: Icons.favorite,
|
||||||
imageUrl:
|
// imageUrl:
|
||||||
'https://thronesapi.com/assets/images/arya-stark.jpg',
|
// 'https://thronesapi.com/assets/images/arya-stark.jpg',
|
||||||
text: '', firstName: '', lastName: '', title: '', family: '',
|
// text: '', firstName: '', lastName: '', title: '', family: '',
|
||||||
),
|
// ),
|
||||||
CardData(
|
// CardData(
|
||||||
fullName: 'Sansa Stark',
|
// fullName: 'Sansa Stark',
|
||||||
descriptionText: 'Lady of Winterfell',
|
// descriptionText: 'Lady of Winterfell',
|
||||||
icon: Icons.favorite,
|
// icon: Icons.favorite,
|
||||||
imageUrl:
|
// imageUrl:
|
||||||
'https://thronesapi.com/assets/images/sansa-stark.jpeg',
|
// 'https://thronesapi.com/assets/images/sansa-stark.jpeg',
|
||||||
text: '', firstName: '', lastName: '', title: '', family: '',
|
// text: '', firstName: '', lastName: '', title: '', family: '',
|
||||||
),
|
// ),
|
||||||
CardData(
|
// CardData(
|
||||||
fullName: 'Brandon Stark',
|
// fullName: 'Brandon Stark',
|
||||||
descriptionText: 'Lord of Winterfell',
|
// descriptionText: 'Lord of Winterfell',
|
||||||
icon: Icons.favorite,
|
// icon: Icons.favorite,
|
||||||
imageUrl:
|
// imageUrl:
|
||||||
'https://thronesapi.com/assets/images/bran-stark.jpg',
|
// 'https://thronesapi.com/assets/images/bran-stark.jpg',
|
||||||
text: '', firstName: '', lastName: '', title: '', family: '',
|
// text: '', firstName: '', lastName: '', title: '', family: '',
|
||||||
),
|
// ),
|
||||||
CardData(
|
// CardData(
|
||||||
fullName: 'Ned Stark',
|
// fullName: 'Ned Stark',
|
||||||
descriptionText: 'Lord of Winterfell',
|
// descriptionText: 'Lord of Winterfell',
|
||||||
icon: Icons.favorite,
|
// icon: Icons.favorite,
|
||||||
imageUrl:
|
// imageUrl:
|
||||||
'https://thronesapi.com/assets/images/ned-stark.jpg',
|
// 'https://thronesapi.com/assets/images/ned-stark.jpg',
|
||||||
text: '', firstName: '', lastName: '', title: '', family: '',
|
// text: '', firstName: '', lastName: '', title: '', family: '',
|
||||||
),
|
// ),
|
||||||
CardData(
|
// CardData(
|
||||||
fullName: 'Robert Baratheon',
|
// fullName: 'Robert Baratheon',
|
||||||
descriptionText: 'Lord of the Seven Kingdoms',
|
// descriptionText: 'Lord of the Seven Kingdoms',
|
||||||
icon: Icons.favorite,
|
// icon: Icons.favorite,
|
||||||
imageUrl:
|
// imageUrl:
|
||||||
'https://thronesapi.com/assets/images/robert-baratheon.jpeg',
|
// 'https://thronesapi.com/assets/images/robert-baratheon.jpeg',
|
||||||
text: '', firstName: '', lastName: '', title: '', family: '',
|
// text: '', firstName: '', lastName: '', title: '', family: '',
|
||||||
),
|
// ),
|
||||||
];
|
// ];
|
||||||
}
|
// }
|
||||||
}
|
// }
|
@ -5,11 +5,11 @@ class CardData {
|
|||||||
final String descriptionText;
|
final String descriptionText;
|
||||||
final IconData icon;
|
final IconData icon;
|
||||||
final String? imageUrl;
|
final String? imageUrl;
|
||||||
final String firstName;
|
final String? firstName;
|
||||||
final String lastName;
|
final String? lastName;
|
||||||
final String fullName;
|
final String? fullName;
|
||||||
final String title;
|
final String? title;
|
||||||
final String family;
|
final String? family;
|
||||||
|
|
||||||
CardData({
|
CardData({
|
||||||
required this.text,
|
required this.text,
|
||||||
|
8
flutter_app/lib/domain/models/home.dart
Normal file
8
flutter_app/lib/domain/models/home.dart
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import 'card.dart';
|
||||||
|
|
||||||
|
class HomeData {
|
||||||
|
final List<CardData> data;
|
||||||
|
final int? nextPage;
|
||||||
|
|
||||||
|
HomeData({required this.data, this.nextPage});
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_app/data/repositories/got_repository.dart';
|
||||||
|
import 'package:flutter_app/presentation/home_page/bloc/bloc.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'presentation/home_page/home_page.dart';
|
import 'presentation/home_page/home_page.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
@ -17,11 +19,15 @@ class MyApp extends StatelessWidget {
|
|||||||
colorScheme: ColorScheme.fromSeed(seedColor: const Color.fromARGB(255, 20, 40, 150)),
|
colorScheme: ColorScheme.fromSeed(seedColor: const Color.fromARGB(255, 20, 40, 150)),
|
||||||
useMaterial3: true,
|
useMaterial3: true,
|
||||||
),
|
),
|
||||||
darkTheme: ThemeData.dark().copyWith(
|
home: RepositoryProvider<ThronesRepository>(
|
||||||
colorScheme: ColorScheme.fromSeed(seedColor: const Color.fromARGB(255, 20, 40, 150)),
|
lazy: true,
|
||||||
),
|
create: (_) => ThronesRepository(),
|
||||||
themeMode: ThemeMode.light, // Можно изменить на ThemeMode.light или ThemeMode.dark
|
child: BlocProvider<HomeBloc>(
|
||||||
home: const MyHomePage(title: '7 Kingdoms'),
|
lazy: false,
|
||||||
|
create: (context) => HomeBloc(context.read<ThronesRepository>()),
|
||||||
|
child: const MyHomePage(title: '7 Kingdoms'),
|
||||||
|
),
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ class DetailsPage extends StatelessWidget {
|
|||||||
Padding(
|
Padding(
|
||||||
padding: const EdgeInsets.only(bottom: 4.0),
|
padding: const EdgeInsets.only(bottom: 4.0),
|
||||||
child: Text(
|
child: Text(
|
||||||
data.fullName,
|
data.fullName ?? 'Default Name',
|
||||||
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
||||||
fontSize: 27.0,
|
fontSize: 27.0,
|
||||||
fontFamily: 'Times New Roman',
|
fontFamily: 'Times New Roman',
|
||||||
|
39
flutter_app/lib/presentation/home_page/bloc/bloc.dart
Normal file
39
flutter_app/lib/presentation/home_page/bloc/bloc.dart
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import 'package:flutter_app/data/repositories/got_repository.dart';
|
||||||
|
import 'package:flutter_app/presentation/home_page/bloc/events.dart';
|
||||||
|
import 'package:flutter_app/presentation/home_page/bloc/state.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
|
||||||
|
class HomeBloc extends Bloc<HomeEvent, HomeState> {
|
||||||
|
final ThronesRepository 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,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
10
flutter_app/lib/presentation/home_page/bloc/events.dart
Normal file
10
flutter_app/lib/presentation/home_page/bloc/events.dart
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
abstract class HomeEvent {
|
||||||
|
const HomeEvent();
|
||||||
|
}
|
||||||
|
|
||||||
|
class HomeLoadDataEvent extends HomeEvent {
|
||||||
|
final String? search;
|
||||||
|
final int? nextPage;
|
||||||
|
|
||||||
|
const HomeLoadDataEvent({this.search, this.nextPage});
|
||||||
|
}
|
43
flutter_app/lib/presentation/home_page/bloc/state.dart
Normal file
43
flutter_app/lib/presentation/home_page/bloc/state.dart
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import 'package:flutter_app/domain/models/card.dart';
|
||||||
|
import 'package:flutter_app/domain/models/home.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:copy_with_extension/copy_with_extension.dart';
|
||||||
|
|
||||||
|
part 'state.g.dart';
|
||||||
|
|
||||||
|
@CopyWith()
|
||||||
|
class HomeState extends Equatable {
|
||||||
|
final HomeData? data;
|
||||||
|
final bool isLoading;
|
||||||
|
final bool isPaginationLoading;
|
||||||
|
final String? error;
|
||||||
|
|
||||||
|
const HomeState({
|
||||||
|
this.data,
|
||||||
|
this.isLoading = false,
|
||||||
|
this.isPaginationLoading = false,
|
||||||
|
this.error,
|
||||||
|
});
|
||||||
|
|
||||||
|
HomeState copyWith({
|
||||||
|
HomeData? data,
|
||||||
|
bool? isLoading,
|
||||||
|
bool? isPaginationLoading,
|
||||||
|
String? error,
|
||||||
|
}) =>
|
||||||
|
HomeState(
|
||||||
|
data: data ?? this.data,
|
||||||
|
isLoading: isLoading ?? this.isLoading,
|
||||||
|
isPaginationLoading: isPaginationLoading ?? this.isPaginationLoading,
|
||||||
|
error: error ?? this.error,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
data,
|
||||||
|
isLoading,
|
||||||
|
isPaginationLoading,
|
||||||
|
error,
|
||||||
|
];
|
||||||
|
}
|
92
flutter_app/lib/presentation/home_page/bloc/state.g.dart
Normal file
92
flutter_app/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);
|
||||||
|
}
|
@ -5,7 +5,7 @@ typedef OnLikeCallback = void Function(String title, bool isLiked)?;
|
|||||||
class _Card extends StatefulWidget {
|
class _Card extends StatefulWidget {
|
||||||
final String text;
|
final String text;
|
||||||
final String descriptionText;
|
final String descriptionText;
|
||||||
final String fullName;
|
final String? fullName;
|
||||||
final IconData icon;
|
final IconData icon;
|
||||||
final String? imageUrl;
|
final String? imageUrl;
|
||||||
final OnLikeCallback onLike;
|
final OnLikeCallback onLike;
|
||||||
@ -70,7 +70,7 @@ class _CardState extends State<_Card> {
|
|||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(
|
Text(
|
||||||
widget.fullName,
|
widget.fullName ?? 'Default Name',
|
||||||
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
style: Theme.of(context).textTheme.bodyLarge?.copyWith(
|
||||||
fontSize: 20.0,
|
fontSize: 20.0,
|
||||||
fontFamily: 'Times New Roman',
|
fontFamily: 'Times New Roman',
|
||||||
@ -96,7 +96,7 @@ class _CardState extends State<_Card> {
|
|||||||
setState(() {
|
setState(() {
|
||||||
isLiked = !isLiked;
|
isLiked = !isLiked;
|
||||||
});
|
});
|
||||||
widget.onLike?.call(widget.fullName, isLiked);
|
widget.onLike?.call(widget.fullName ?? 'Default Name', isLiked);
|
||||||
},
|
},
|
||||||
child: AnimatedSwitcher(
|
child: AnimatedSwitcher(
|
||||||
duration: const Duration(milliseconds: 300),
|
duration: const Duration(milliseconds: 300),
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_app/components/utils/debounce.dart';
|
||||||
import 'package:flutter_app/data/repositories/got_repository.dart';
|
import 'package:flutter_app/data/repositories/got_repository.dart';
|
||||||
import 'package:flutter_app/domain/models/card.dart';
|
import 'package:flutter_app/domain/models/card.dart';
|
||||||
|
import 'package:flutter_app/presentation/home_page/bloc/bloc.dart';
|
||||||
|
import 'package:flutter_app/presentation/home_page/bloc/events.dart';
|
||||||
|
import 'package:flutter_app/presentation/home_page/bloc/state.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import '../details_page/details_page.dart';
|
import '../details_page/details_page.dart';
|
||||||
|
|
||||||
part 'card.dart';
|
part 'card.dart';
|
||||||
@ -30,21 +35,35 @@ class Body extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _BodyState extends State<Body> {
|
class _BodyState extends State<Body> {
|
||||||
late TextEditingController searchController;
|
final searchController = TextEditingController();
|
||||||
late Future<List<CardData>?> data;
|
final scrollController = ScrollController();
|
||||||
|
|
||||||
final repo = ThronesRepository();
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
data = repo.loadData();
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
searchController = TextEditingController();
|
context.read<HomeBloc>().add(const HomeLoadDataEvent());
|
||||||
data = repo.loadData();
|
});
|
||||||
|
scrollController.addListener(_onNextPageListener);
|
||||||
|
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onNextPageListener() {
|
||||||
|
if (scrollController.offset > scrollController.position.maxScrollExtent) {
|
||||||
|
final bloc = context.read<HomeBloc>();
|
||||||
|
if (!bloc.state.isPaginationLoading) {
|
||||||
|
bloc.add(HomeLoadDataEvent(
|
||||||
|
search: searchController.text,
|
||||||
|
nextPage: bloc.state.data?.nextPage,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
searchController.dispose();
|
searchController.dispose();
|
||||||
|
scrollController.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,51 +78,55 @@ class _BodyState extends State<Body> {
|
|||||||
child: CupertinoSearchTextField(
|
child: CupertinoSearchTextField(
|
||||||
controller: searchController,
|
controller: searchController,
|
||||||
onChanged: (search) {
|
onChanged: (search) {
|
||||||
setState(() {
|
Debounce.run(() => context.read<HomeBloc>().add(HomeLoadDataEvent(search: search)));
|
||||||
data = repo.loadData(q: search);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Expanded(
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
child: Center(
|
builder: (context, state) => state.error != null
|
||||||
child: FutureBuilder<List<CardData>?>(
|
? Text(
|
||||||
future: data,
|
state.error ?? '',
|
||||||
builder: (context, snapshot) {
|
style: Theme.of(context).textTheme.headlineSmall?.copyWith(color: Colors.red),
|
||||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
)
|
||||||
return const CircularProgressIndicator();
|
: state.isLoading
|
||||||
} else if (snapshot.hasError) {
|
? const CircularProgressIndicator()
|
||||||
return Center(
|
: Expanded(
|
||||||
child: Text('Error: ${snapshot.error}'),
|
child: RefreshIndicator(
|
||||||
);
|
onRefresh: _onRefresh,
|
||||||
} else if (!snapshot.hasData || snapshot.data!.isEmpty) {
|
child: ListView.builder(
|
||||||
return const Center(
|
controller: scrollController,
|
||||||
child: Text('No data found'),
|
padding: EdgeInsets.zero,
|
||||||
);
|
itemCount: state.data?.data?.length ?? 0,
|
||||||
} else {
|
itemBuilder: (context, index) {
|
||||||
return ListView.builder(
|
final data = state.data?.data?[index];
|
||||||
shrinkWrap: true,
|
return data != null
|
||||||
itemCount: snapshot.data!.length,
|
? _Card.fromData(
|
||||||
itemBuilder: (context, index) {
|
data,
|
||||||
final cardData = snapshot.data![index];
|
onLike: (title, isLiked) =>
|
||||||
return _Card.fromData(
|
_showSnackBar(context, title, isLiked),
|
||||||
cardData,
|
onTap: () => _navToDetails(context, data),
|
||||||
onLike: (String title, bool isLiked) =>
|
)
|
||||||
_showSnackBar(context, title, isLiked),
|
: const SizedBox.shrink();
|
||||||
onTap: () => _navToDetails(context, cardData),
|
},
|
||||||
);
|
),
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
),
|
||||||
|
BlocBuilder<HomeBloc, HomeState>(
|
||||||
|
builder: (context, state) => state.isPaginationLoading
|
||||||
|
? const CircularProgressIndicator()
|
||||||
|
: const SizedBox.shrink(),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _onRefresh() {
|
||||||
|
context.read<HomeBloc>().add(HomeLoadDataEvent(search: searchController.text));
|
||||||
|
return Future.value(null);
|
||||||
|
}
|
||||||
|
|
||||||
void _navToDetails(BuildContext context, CardData data) {
|
void _navToDetails(BuildContext context, CardData data) {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
|
@ -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.1"
|
version: "3.1.1"
|
||||||
|
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:
|
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 main"
|
||||||
|
description:
|
||||||
|
name: equatable
|
||||||
|
sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "2.0.5"
|
||||||
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 main"
|
||||||
|
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:
|
||||||
|
@ -34,6 +34,9 @@ dependencies:
|
|||||||
dio: ^5.4.2+1
|
dio: ^5.4.2+1
|
||||||
pretty_dio_logger: ^1.3.1
|
pretty_dio_logger: ^1.3.1
|
||||||
cupertino_icons: ^1.0.8
|
cupertino_icons: ^1.0.8
|
||||||
|
flutter_bloc: ^8.1.6
|
||||||
|
equatable: ^2.0.5
|
||||||
|
copy_with_extension_gen: ^5.0.4
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
Loading…
Reference in New Issue
Block a user