локализация готова

This commit is contained in:
MaD 2024-12-17 01:12:29 +04:00
parent 5c056797a0
commit 753419dbf3
3 changed files with 109 additions and 77 deletions

View File

@ -3,7 +3,6 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import '../../presentation/home_page/bloc/hero_detail_bloc.dart'; import '../../presentation/home_page/bloc/hero_detail_bloc.dart';
import '../../data/dtos/hero_dto.dart'; import '../../data/dtos/hero_dto.dart';
import '../../data/repositories/hero_repository.dart'; import '../../data/repositories/hero_repository.dart';
import '../../Components/locale/l10n/app_locale.dart';
class HeroDetailScreen extends StatelessWidget { class HeroDetailScreen extends StatelessWidget {
final int heroId; final int heroId;
@ -14,45 +13,43 @@ class HeroDetailScreen extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final heroRepository = context.read<HeroRepository>(); final heroRepository = context.read<HeroRepository>();
// Получаем текущую локализацию
final locale = AppLocale.of(context)!;
return BlocProvider( return BlocProvider(
create: (_) => HeroDetailBloc(heroRepository)..add(FetchHeroDetails(heroId)), create: (_) => HeroDetailBloc(heroRepository)..add(FetchHeroDetails(heroId)),
child: Scaffold( child: Scaffold(
appBar: AppBar( appBar: AppBar(title: Text('Hero Details')),
title: Text(locale.heroDetailsTitle),
),
body: BlocBuilder<HeroDetailBloc, HeroDetailState>( body: BlocBuilder<HeroDetailBloc, HeroDetailState>(
builder: (context, state) { builder: (context, state) {
if (state is HeroDetailLoading) { if (state is HeroDetailLoading) {
return const Center(child: CircularProgressIndicator()); return Center(child: CircularProgressIndicator());
} else if (state is HeroDetailLoaded) { } else if (state is HeroDetailLoaded) {
final hero = state.hero; final hero = state.hero;
return Column( return Padding(
children: [ padding: const EdgeInsets.all(16.0),
hero.portraitUrl != null child: Column(
? Image.network(hero.portraitUrl!) crossAxisAlignment: CrossAxisAlignment.start,
: Column( children: [
children: [ Center(
const Icon(Icons.image, size: 100), child: hero.portraitUrl != null
Text(locale.heroNoImage), ? Image.network(hero.portraitUrl!, height: 150)
], : Icon(Icons.image, size: 150),
), ),
Text( SizedBox(height: 16),
hero.name, Text(
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold), hero.name,
), style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
Text( ),
hero.description ?? locale.heroNoDescription, SizedBox(height: 16),
style: const TextStyle(fontSize: 16), Text(
), hero.description ?? 'No description available',
], style: TextStyle(fontSize: 16),
),
],
),
); );
} else if (state is HeroDetailError) { } 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();
}, },
), ),
), ),

View File

@ -1,21 +1,30 @@
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:provider/provider.dart';
import '../../Components/locale/l10n/app_locale.dart';
import '../../main.dart';
import '../../presentation/home_page/bloc/hero_list_bloc.dart'; import '../../presentation/home_page/bloc/hero_list_bloc.dart';
import '../../widgets/hero_card.dart'; import '../../widgets/hero_card.dart';
import '../../presentation/home_page/bloc/hero_search_bloc.dart'; import '../../presentation/home_page/bloc/hero_search_bloc.dart';
class HeroListScreen extends StatelessWidget { class HeroListScreen extends StatelessWidget {
const HeroListScreen({Key? key}) : super(key: key); const HeroListScreen({Key? key}) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final locale = AppLocale.of(context)!; // Получаем текущую локализацию
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('Heroes'), title: const Text('Heroes'),
actions: [
IconButton(
icon: const Icon(Icons.language),
onPressed: () {
// Переключение локали
context.read<LocaleNotifier>().toggleLocale();
},
),
],
bottom: PreferredSize( bottom: PreferredSize(
preferredSize: const Size.fromHeight(60), preferredSize: const Size.fromHeight(60),
child: Padding( child: Padding(
@ -26,7 +35,7 @@ class HeroListScreen extends StatelessWidget {
context.read<HeroSearchBloc>().add(SearchHeroes(query)); context.read<HeroSearchBloc>().add(SearchHeroes(query));
}, },
decoration: InputDecoration( decoration: InputDecoration(
hintText: 'Search for a hero...', hintText: locale.search,
prefixIcon: const Icon(Icons.search), prefixIcon: const Icon(Icons.search),
border: OutlineInputBorder( border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8), borderRadius: BorderRadius.circular(8),

View File

@ -1,17 +1,12 @@
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:provider/provider.dart';
import 'data/repositories/hero_repository.dart'; import 'data/repositories/hero_repository.dart';
import 'services/api_service.dart'; import 'services/api_service.dart';
import 'presentation/home_page/bloc/hero_list_bloc.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 'presentation/home_page/bloc/hero_search_bloc.dart';
import 'package:flutter/material.dart'; import 'components/screens/hero_list_screen.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'Components/locale/l10n/app_locale.dart';
import '../../Components/locale/l10n/app_locale.dart';
void main() { void main() {
final apiService = ApiService(baseUrl: 'https://assets.deadlock-api.com'); final apiService = ApiService(baseUrl: 'https://assets.deadlock-api.com');
@ -20,6 +15,18 @@ void main() {
runApp(MyApp(heroRepository: heroRepository)); 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 { class MyApp extends StatelessWidget {
final HeroRepository heroRepository; final HeroRepository heroRepository;
@ -27,39 +34,58 @@ class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Provider<HeroRepository>( return ChangeNotifierProvider(
create: (_) => heroRepository, create: (_) => LocaleNotifier(),
child: BlocProvider( child: Provider<HeroRepository>(
create: (_) => HeroListBloc(heroRepository)..add(FetchHeroes()), create: (_) => heroRepository,
child: Builder( child: BlocProvider(
builder: (context) { create: (_) => HeroListBloc(heroRepository)..add(FetchHeroes()),
final heroListBloc = context.read<HeroListBloc>(); child: Consumer<LocaleNotifier>(
return BlocBuilder<HeroListBloc, HeroListState>( builder: (context, localeNotifier, child) {
builder: (context, state) { return MaterialApp(
if (state is HeroListLoaded) { locale: localeNotifier.currentLocale,
return MultiBlocProvider( localizationsDelegates: AppLocale.localizationsDelegates,
providers: [ supportedLocales: AppLocale.supportedLocales,
BlocProvider( home: Builder(
create: (_) => HeroSearchBloc(allHeroes: state.heroes), builder: (context) {
), final heroListBloc = context.read<HeroListBloc>();
], return BlocBuilder<HeroListBloc, HeroListState>(
child: MaterialApp( builder: (context, state) {
home: HeroListScreen(), if (state is HeroListLoaded) {
), return MultiBlocProvider(
); providers: [
} BlocProvider(
return MaterialApp( create: (_) =>
localizationsDelegates: AppLocale.localizationsDelegates, HeroSearchBloc(allHeroes: state.heroes),
supportedLocales: AppLocale.supportedLocales, ),
home: Scaffold( ],
body: Center( child: const HeroListScreen(),
child: CircularProgressIndicator(), );
), } 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...'),
),
);
},
);
},
),
);
},
),
), ),
), ),
); );