From 753419dbf3114d91f7f69f7b523561956b59e0dc Mon Sep 17 00:00:00 2001 From: MaD Date: Tue, 17 Dec 2024 01:12:29 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BB=D0=BE=D0=BA=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../screens/hero_detail_screen.dart | 55 +++++---- lib/Components/screens/hero_list_screen.dart | 21 +++- lib/main.dart | 110 +++++++++++------- 3 files changed, 109 insertions(+), 77 deletions(-) diff --git a/lib/Components/screens/hero_detail_screen.dart b/lib/Components/screens/hero_detail_screen.dart index d9de819..a644004 100644 --- a/lib/Components/screens/hero_detail_screen.dart +++ b/lib/Components/screens/hero_detail_screen.dart @@ -3,7 +3,6 @@ import 'package:flutter_bloc/flutter_bloc.dart'; import '../../presentation/home_page/bloc/hero_detail_bloc.dart'; import '../../data/dtos/hero_dto.dart'; import '../../data/repositories/hero_repository.dart'; -import '../../Components/locale/l10n/app_locale.dart'; class HeroDetailScreen extends StatelessWidget { final int heroId; @@ -14,45 +13,43 @@ class HeroDetailScreen extends StatelessWidget { Widget build(BuildContext context) { final heroRepository = context.read(); - // Получаем текущую локализацию - final locale = AppLocale.of(context)!; - return BlocProvider( create: (_) => HeroDetailBloc(heroRepository)..add(FetchHeroDetails(heroId)), child: Scaffold( - appBar: AppBar( - title: Text(locale.heroDetailsTitle), - ), + appBar: AppBar(title: Text('Hero Details')), body: BlocBuilder( builder: (context, state) { if (state is HeroDetailLoading) { - return const Center(child: CircularProgressIndicator()); + return Center(child: CircularProgressIndicator()); } else if (state is HeroDetailLoaded) { final hero = state.hero; - return Column( - children: [ - hero.portraitUrl != null - ? Image.network(hero.portraitUrl!) - : Column( - children: [ - const Icon(Icons.image, size: 100), - Text(locale.heroNoImage), - ], - ), - Text( - hero.name, - style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold), - ), - Text( - hero.description ?? locale.heroNoDescription, - style: const TextStyle(fontSize: 16), - ), - ], + return Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Center( + child: hero.portraitUrl != null + ? Image.network(hero.portraitUrl!, height: 150) + : Icon(Icons.image, size: 150), + ), + SizedBox(height: 16), + Text( + hero.name, + style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold), + ), + SizedBox(height: 16), + Text( + hero.description ?? 'No description available', + style: TextStyle(fontSize: 16), + ), + ], + ), ); } else if (state is HeroDetailError) { - return Center(child: Text(state.message)); + return Center(child: Text('Error: ${state.message}')); } - return const SizedBox.shrink(); + return SizedBox.shrink(); }, ), ), diff --git a/lib/Components/screens/hero_list_screen.dart b/lib/Components/screens/hero_list_screen.dart index ccf5944..1ad2b88 100644 --- a/lib/Components/screens/hero_list_screen.dart +++ b/lib/Components/screens/hero_list_screen.dart @@ -1,21 +1,30 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:provider/provider.dart'; +import '../../Components/locale/l10n/app_locale.dart'; +import '../../main.dart'; import '../../presentation/home_page/bloc/hero_list_bloc.dart'; import '../../widgets/hero_card.dart'; import '../../presentation/home_page/bloc/hero_search_bloc.dart'; - - - - class HeroListScreen extends StatelessWidget { const HeroListScreen({Key? key}) : super(key: key); @override Widget build(BuildContext context) { + final locale = AppLocale.of(context)!; // Получаем текущую локализацию return Scaffold( appBar: AppBar( title: const Text('Heroes'), + actions: [ + IconButton( + icon: const Icon(Icons.language), + onPressed: () { + // Переключение локали + context.read().toggleLocale(); + }, + ), + ], bottom: PreferredSize( preferredSize: const Size.fromHeight(60), child: Padding( @@ -26,7 +35,7 @@ class HeroListScreen extends StatelessWidget { context.read().add(SearchHeroes(query)); }, decoration: InputDecoration( - hintText: 'Search for a hero...', + hintText: locale.search, prefixIcon: const Icon(Icons.search), border: OutlineInputBorder( borderRadius: BorderRadius.circular(8), @@ -74,4 +83,4 @@ class HeroListScreen extends StatelessWidget { ), ); } -} \ No newline at end of file +} diff --git a/lib/main.dart b/lib/main.dart index 9f149c0..e6615d8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,17 +1,12 @@ import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:provider/provider.dart'; import 'data/repositories/hero_repository.dart'; import 'services/api_service.dart'; import 'presentation/home_page/bloc/hero_list_bloc.dart'; -import 'components/screens/hero_list_screen.dart'; -import 'package:provider/provider.dart'; import 'presentation/home_page/bloc/hero_search_bloc.dart'; -import 'package:flutter/material.dart'; -import 'package:flutter_localizations/flutter_localizations.dart'; -import '../../Components/locale/l10n/app_locale.dart'; - - - +import 'components/screens/hero_list_screen.dart'; +import 'Components/locale/l10n/app_locale.dart'; void main() { final apiService = ApiService(baseUrl: 'https://assets.deadlock-api.com'); @@ -20,6 +15,18 @@ void main() { runApp(MyApp(heroRepository: heroRepository)); } +class LocaleNotifier with ChangeNotifier { + Locale _currentLocale = const Locale('en'); // Начальный язык - английский + + Locale get currentLocale => _currentLocale; + + void toggleLocale() { + _currentLocale = + _currentLocale.languageCode == 'en' ? const Locale('ru') : const Locale('en'); + notifyListeners(); + } +} + class MyApp extends StatelessWidget { final HeroRepository heroRepository; @@ -27,41 +34,60 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return Provider( - create: (_) => heroRepository, - child: BlocProvider( - create: (_) => HeroListBloc(heroRepository)..add(FetchHeroes()), - child: Builder( - builder: (context) { - final heroListBloc = context.read(); - return BlocBuilder( - builder: (context, state) { - if (state is HeroListLoaded) { - return MultiBlocProvider( - providers: [ - BlocProvider( - create: (_) => HeroSearchBloc(allHeroes: state.heroes), - ), - ], - child: MaterialApp( - home: HeroListScreen(), - ), - ); - } - return MaterialApp( - localizationsDelegates: AppLocale.localizationsDelegates, - supportedLocales: AppLocale.supportedLocales, - home: Scaffold( - body: Center( - child: CircularProgressIndicator(), - ), - ), - ); - }, - ); - }, + return ChangeNotifierProvider( + create: (_) => LocaleNotifier(), + child: Provider( + create: (_) => heroRepository, + child: BlocProvider( + create: (_) => HeroListBloc(heroRepository)..add(FetchHeroes()), + child: Consumer( + builder: (context, localeNotifier, child) { + return MaterialApp( + locale: localeNotifier.currentLocale, + localizationsDelegates: AppLocale.localizationsDelegates, + supportedLocales: AppLocale.supportedLocales, + home: Builder( + builder: (context) { + final heroListBloc = context.read(); + return BlocBuilder( + builder: (context, state) { + if (state is HeroListLoaded) { + return MultiBlocProvider( + providers: [ + BlocProvider( + create: (_) => + HeroSearchBloc(allHeroes: state.heroes), + ), + ], + child: const HeroListScreen(), + ); + } else if (state is HeroListLoading) { + return const Scaffold( + body: Center( + child: CircularProgressIndicator(), + ), + ); + } else if (state is HeroListError) { + return const Scaffold( + body: Center( + child: Text('Failed to load heroes.'), + ), + ); + } + return const Scaffold( + body: Center( + child: Text('Initializing...'), + ), + ); + }, + ); + }, + ), + ); + }, + ), ), ), ); } -} \ No newline at end of file +}