7 лаба готова, держимся

This commit is contained in:
kamilia 2024-10-17 00:38:03 +04:00
parent b42249b048
commit 73fcfc283d
11 changed files with 307 additions and 109 deletions

View File

@ -1,6 +1,6 @@
arb-dir: l10n arb-dir: l10n
template-arb-file: app_ru.arb template-arb-file: app_ru.arb
output-localization-file: app_locale.dart output-localization-file: app_locale.dart
output-dir: lib/components/locale/l10n output-dir: lib/components/l10n
output-class: AppLocale output-class: AppLocale
synthetic-package: false synthetic-package: false

View File

@ -0,0 +1,6 @@
import 'package:flutter/widgets.dart';
import '../locale/l10n/app_locale.dart';
extension LocalContextX on BuildContext {
AppLocale get locale => AppLocale.of(this)!;
}

View File

@ -17,6 +17,7 @@ extension CharacterDataDtoToModel on CharacterDataDto {
attributes?.name ?? 'UNKNOWN', attributes?.name ?? 'UNKNOWN',
imageUrl: attributes?.image ?? _imagePlaceholder, imageUrl: attributes?.image ?? _imagePlaceholder,
descriptionText: _makeDescriptionText(attributes?.born, attributes?.died), descriptionText: _makeDescriptionText(attributes?.born, attributes?.died),
id: id,
); );
String _makeDescriptionText(String? born, String? died) { String _makeDescriptionText(String? born, String? died) {

View File

@ -5,11 +5,13 @@ class CardData {
final String descriptionText; final String descriptionText;
final IconData icon; final IconData icon;
final String? imageUrl; final String? imageUrl;
final String? id;
CardData( CardData(
this.text, { this.text, {
required this.descriptionText, required this.descriptionText,
this.icon = Icons.catching_pokemon, this.icon = Icons.catching_pokemon,
this.imageUrl, this.imageUrl,
this.id,
}); });
} }

View File

@ -1,7 +1,13 @@
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pmu/presentation/home_page/bloc/bloc.dart'; import 'package:pmu/presentation/home_page/bloc/bloc.dart';
import 'package:pmu/presentation/home_page/home_page.dart'; import 'package:pmu/presentation/home_page/home_page.dart';
import 'package:pmu/presentation/like_bloc/like_bloc.dart';
import 'package:pmu/presentation/locale_bloc/locale_bloc.dart';
import 'package:pmu/presentation/locale_bloc/locale_state.dart';
import 'components/locale/l10n/app_locale.dart';
import 'data/repositories/potter_repository.dart'; import 'data/repositories/potter_repository.dart';
void main() { void main() {
@ -13,22 +19,37 @@ class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider<LocaleBloc>(
lazy: false,
create: (context) => LocaleBloc(Locale(Platform.localeName)),
child: BlocBuilder<LocaleBloc, LocaleState>(
builder: (context, state) {
return MaterialApp( return MaterialApp(
title: 'Flutter Demo', title: 'Flutter Demo',
locale: state.currentLocale,
localizationsDelegates: AppLocale.localizationsDelegates,
supportedLocales: AppLocale.supportedLocales,
debugShowCheckedModeBanner: false, debugShowCheckedModeBanner: false,
theme: ThemeData( theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.orange), colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true, useMaterial3: true,
), ),
home: RepositoryProvider<PotterRepository>( home: RepositoryProvider<PotterRepository>(
lazy: true, lazy: true,
create: (_) => PotterRepository(), create: (_) => PotterRepository(),
child: BlocProvider<LikeBloc>(
lazy: false,
create: (context) => LikeBloc(),
child: BlocProvider<HomeBloc>( child: BlocProvider<HomeBloc>(
lazy: false, lazy: false,
create: (context) => HomeBloc(context.read<PotterRepository>()), create: (context) => HomeBloc(context.read<PotterRepository>()),
child: const MyHomePage(title: 'Сафиулова Камилия Наилевна'), child: const MyHomePage(title: 'Сафиулова Камилия Наилевна'),
), ),
), ),
),
);
},
),
); );
} }
} }

View File

@ -1,28 +1,33 @@
part of 'home_page.dart'; part of 'home_page.dart';
typedef OnLikeCallback = void Function(String title, bool isLiked)?; typedef OnLikeCallback = void Function(String? id, String title, bool isLiked)?;
class _Card extends StatefulWidget { class _Card extends StatelessWidget {
final String text; final String text;
final String descriptionText; final String descriptionText;
final IconData icon; final IconData icon;
final String? imageUrl; final String? imageUrl;
final OnLikeCallback onLike; final OnLikeCallback onLike;
final VoidCallback? onTap; final VoidCallback? onTap;
final String? id;
final bool isLiked;
const _Card( const _Card(
this.text, { this.text, {
this.icon = Icons.catching_pokemon, this.icon = Icons.ac_unit_outlined,
required this.descriptionText, required this.descriptionText,
this.imageUrl, this.imageUrl,
this.onLike, this.onLike,
this.onTap, this.onTap,
this.id,
this.isLiked = false,
}); });
factory _Card.fromData( factory _Card.fromData(
CardData data, { CardData data, {
OnLikeCallback onLike, OnLikeCallback onLike,
VoidCallback? onTap, VoidCallback? onTap,
bool isLiked = false,
}) => }) =>
_Card( _Card(
data.text, data.text,
@ -31,26 +36,28 @@ class _Card extends StatefulWidget {
imageUrl: data.imageUrl, imageUrl: data.imageUrl,
onLike: onLike, onLike: onLike,
onTap: onTap, onTap: onTap,
isLiked: isLiked,
id: data.id,
); );
@override
State<_Card> createState() => _CardState();
}
class _CardState extends State<_Card> {
bool isLiked = false;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GestureDetector( return GestureDetector(
onTap: widget.onTap, onTap: onTap,
child: Container( child: Container(
margin: const EdgeInsets.all(16), margin: const EdgeInsets.all(16),
constraints: const BoxConstraints(minHeight: 140), constraints: const BoxConstraints(minHeight: 160),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white70, color: Colors.white70,
borderRadius: BorderRadius.circular(20), borderRadius: BorderRadius.circular(20),
border: Border.all(color: Colors.grey.shade200), boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(.5),
spreadRadius: 4,
offset: const Offset(0, 5),
blurRadius: 8,
),
],
), ),
child: IntrinsicHeight( child: IntrinsicHeight(
child: Row( child: Row(
@ -63,13 +70,19 @@ class _CardState extends State<_Card> {
), ),
child: SizedBox( child: SizedBox(
height: double.infinity, height: double.infinity,
width: 160, width: 120,
child: Stack(
children: [
Positioned.fill(
child: Image.network( child: Image.network(
widget.imageUrl ?? '', imageUrl ?? '',
fit: BoxFit.cover, fit: BoxFit.cover,
errorBuilder: (_, __, ___) => const Placeholder(), errorBuilder: (_, __, ___) => const Placeholder(),
), ),
), ),
],
),
),
), ),
Expanded( Expanded(
child: Padding( child: Padding(
@ -78,13 +91,13 @@ class _CardState extends State<_Card> {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text( Text(
widget.text, text,
style: Theme.of(context).textTheme.headlineLarge, style: Theme.of(context).textTheme.headlineSmall,
), ),
Text( Text(
widget.descriptionText, descriptionText,
style: Theme.of(context).textTheme.bodyLarge, style: Theme.of(context).textTheme.bodyLarge,
), )
], ],
), ),
), ),
@ -92,20 +105,12 @@ class _CardState extends State<_Card> {
Align( Align(
alignment: Alignment.bottomRight, alignment: Alignment.bottomRight,
child: Padding( child: Padding(
padding: const EdgeInsets.only( padding:
left: 8.0, const EdgeInsets.only(left: 8, right: 16, bottom: 16),
right: 16.0,
bottom: 16.0,
),
child: GestureDetector( child: GestureDetector(
onTap: () { onTap: () => onLike?.call(id, text, isLiked),
setState(() {
isLiked = !isLiked;
});
widget.onLike?.call(widget.text, isLiked);
},
child: AnimatedSwitcher( child: AnimatedSwitcher(
duration: const Duration(milliseconds: 300), duration: const Duration(milliseconds: 200),
child: isLiked child: isLiked
? const Icon( ? const Icon(
Icons.favorite, Icons.favorite,

View File

@ -1,6 +1,7 @@
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:pmu/components/extensions/context_x.dart';
import 'package:pmu/components/utils/debounce.dart'; import 'package:pmu/components/utils/debounce.dart';
import 'package:pmu/data/repositories/potter_repository.dart'; import 'package:pmu/data/repositories/potter_repository.dart';
import 'package:pmu/domain/models/card.dart'; import 'package:pmu/domain/models/card.dart';
@ -9,8 +10,13 @@ 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/bloc.dart';
import 'package:pmu/presentation/home_page/bloc/events.dart'; import 'package:pmu/presentation/home_page/bloc/events.dart';
import 'package:pmu/presentation/home_page/bloc/state.dart'; import 'package:pmu/presentation/home_page/bloc/state.dart';
import '../common/svg_objects.dart'; import '../common/svg_objects.dart';
import '../like_bloc/like_bloc.dart';
import '../like_bloc/like_event.dart';
import '../like_bloc/like_state.dart';
import '../locale_bloc/locale_bloc.dart';
import '../locale_bloc/locale_events.dart';
import '../locale_bloc/locale_state.dart';
part 'card.dart'; part 'card.dart';
@ -43,10 +49,13 @@ class BodyState extends State<Body> {
@override @override
void initState() { void initState() {
SvgObjects.init();
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
context.read<HomeBloc>().add(const HomeLoadDataEvent()); context.read<HomeBloc>().add(const HomeLoadDataEvent());
context.read<LikeBloc>().add(const LoadLikesEvent());
}); });
SvgObjects.init();
scrollController.addListener(_onNextPageListener); scrollController.addListener(_onNextPageListener);
super.initState(); super.initState();
@ -77,15 +86,40 @@ class BodyState extends State<Body> {
padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top), padding: EdgeInsets.only(top: MediaQuery.of(context).padding.top),
child: Column( child: Column(
children: [ children: [
Padding( Row(
children: [
Expanded(
flex: 4,
child: Padding(
padding: const EdgeInsets.all(12), padding: const EdgeInsets.all(12),
child: CupertinoSearchTextField( child: CupertinoSearchTextField(
controller: searchController, controller: searchController,
placeholder: context.locale.search,
onChanged: (search) { onChanged: (search) {
Debounce.run(() => context.read<HomeBloc>().add(HomeLoadDataEvent(search: search))); Debounce.run(
() => context.read<HomeBloc>().add(HomeLoadDataEvent(search: search)));
}, },
), ),
), ),
),
GestureDetector(
onTap: () => context.read<LocaleBloc>().add(const ChangeLocaleEvent()),
child: SizedBox.square(
dimension: 50,
child: Padding(
padding: const EdgeInsets.only(right: 12),
child: BlocBuilder<LocaleBloc, LocaleState>(
builder: (context, state) {
return state.currentLocale.languageCode == 'ru'
? const SvgRu()
: const SvgUk();
},
),
),
),
),
],
),
BlocBuilder<HomeBloc, HomeState>( BlocBuilder<HomeBloc, HomeState>(
builder: (context, state) => state.error != null builder: (context, state) => state.error != null
? Text( ? Text(
@ -94,7 +128,9 @@ class BodyState extends State<Body> {
) )
: state.isLoading : state.isLoading
? const CircularProgressIndicator() ? const CircularProgressIndicator()
: Expanded( : BlocBuilder<LikeBloc, LikeState>(
builder: (context, likeState) {
return Expanded(
child: RefreshIndicator( child: RefreshIndicator(
onRefresh: _onRefresh, onRefresh: _onRefresh,
child: ListView.builder( child: ListView.builder(
@ -106,14 +142,16 @@ class BodyState extends State<Body> {
return data != null return data != null
? _Card.fromData( ? _Card.fromData(
data, data,
onLike: (title, isLiked) => onLike: _onLike,
_showSnackBar(context, title, isLiked), isLiked: likeState.likedIds?.contains(data.id) == true,
onTap: () => _navToDetails(context, data), onTap: () => _navToDetails(context, data),
) )
: const SizedBox.shrink(); : const SizedBox.shrink();
}, },
), ),
), ),
);
},
), ),
), ),
BlocBuilder<HomeBloc, HomeState>( BlocBuilder<HomeBloc, HomeState>(
@ -142,7 +180,7 @@ class BodyState extends State<Body> {
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar( ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text( content: Text(
'$title ${isLiked ? 'liked!' : 'disliked :('}', '$title ${isLiked ? context.locale.liked : context.locale.disliked}',
style: Theme.of(context).textTheme.bodyLarge, style: Theme.of(context).textTheme.bodyLarge,
), ),
backgroundColor: Colors.orangeAccent, backgroundColor: Colors.orangeAccent,
@ -150,4 +188,11 @@ class BodyState extends State<Body> {
)); ));
}); });
} }
void _onLike(String? id, String title, bool isLiked) {
if (id != null) {
context.read<LikeBloc>().add(ChangeLikeEvent(id));
_showSnackBar(context, title, !isLiked);
}
}
} }

View File

@ -20,5 +20,4 @@ res:
make format make format
loc: loc:
flutter gen-l10n; \ flutter gen-l10n
make format

View File

@ -254,6 +254,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.3.1" version: "1.3.1"
ffi:
dependency: transitive
description:
name: ffi
sha256: "16ed7b077ef01ad6170a3d0c57caa4a112a38d7a2ed5602e0aca9ca6f3d98da6"
url: "https://pub.dev"
source: hosted
version: "2.1.3"
file: file:
dependency: transitive dependency: transitive
description: description:
@ -317,6 +325,11 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
frontend_server_client: frontend_server_client:
dependency: transitive dependency: transitive
description: description:
@ -525,6 +538,30 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.1" version: "1.0.1"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
sha256: f7a1fe3a634fe7734c8d3f2766ad746ae2a2884abe22e241a8b301bf5cac3279
url: "https://pub.dev"
source: hosted
version: "2.2.1"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
sha256: "88f5779f72ba699763fa3a3b06aa4bf6de76c8e5de842cf6f29e2e06476c2334"
url: "https://pub.dev"
source: hosted
version: "2.1.2"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
sha256: bd6f00dbd873bfb70d0761682da2b3a2c2fccc2b9e84c495821639601d81afe7
url: "https://pub.dev"
source: hosted
version: "2.3.0"
petitparser: petitparser:
dependency: transitive dependency: transitive
description: description:
@ -533,6 +570,22 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "6.0.2" version: "6.0.2"
platform:
dependency: transitive
description:
name: platform
sha256: "9b71283fc13df574056616011fb138fd3b793ea47cc509c189a6c3fa5f8a1a65"
url: "https://pub.dev"
source: hosted
version: "3.1.5"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.8"
pool: pool:
dependency: transitive dependency: transitive
description: description:
@ -573,6 +626,62 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.3.0" version: "1.3.0"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
sha256: d3bbe5553a986e83980916ded2f0b435ef2e1893dfaa29d5a7a790d0eca12180
url: "https://pub.dev"
source: hosted
version: "2.2.3"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
sha256: "3b9febd815c9ca29c9e3520d50ec32f49157711e143b7a4ca039eb87e8ade5ab"
url: "https://pub.dev"
source: hosted
version: "2.3.3"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: "07e050c7cd39bad516f8d64c455f04508d09df104be326d8c02551590a0d513d"
url: "https://pub.dev"
source: hosted
version: "2.5.3"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
sha256: "580abfd40f415611503cae30adf626e6656dfb2f0cee8f465ece7b6defb40f2f"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: "57cbf196c486bc2cf1f02b85784932c6094376284b3ad5779d1b1c6c6a816b80"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
sha256: d2ca4132d3946fec2184261726b355836a82c33d7d5b67af32692aff18a4684e
url: "https://pub.dev"
source: hosted
version: "2.4.2"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
sha256: "94ef0f72b2d71bc3e700e025db3710911bd51a71cefb65cc609dd0d9a982e3c1"
url: "https://pub.dev"
source: hosted
version: "2.4.1"
shelf: shelf:
dependency: transitive dependency: transitive
description: description:
@ -754,6 +863,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.0.1" version: "3.0.1"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
sha256: "7a3f37b05d989967cdddcbb571f1ea834867ae2faa29725fd085180e0883aa15"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
xml: xml:
dependency: transitive dependency: transitive
description: description:
@ -772,4 +889,4 @@ packages:
version: "3.1.2" version: "3.1.2"
sdks: sdks:
dart: ">=3.5.3 <4.0.0" dart: ">=3.5.3 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54" flutter: ">=3.24.0"

View File

@ -27,6 +27,8 @@ dependencies:
intl: ^0.19.0 intl: ^0.19.0
shared_preferences: 2.2.3
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: