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

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 '../../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<HeroRepository>();
// Получаем текущую локализацию
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<HeroDetailBloc, HeroDetailState>(
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();
},
),
),

View File

@ -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<LocaleNotifier>().toggleLocale();
},
),
],
bottom: PreferredSize(
preferredSize: const Size.fromHeight(60),
child: Padding(
@ -26,7 +35,7 @@ class HeroListScreen extends StatelessWidget {
context.read<HeroSearchBloc>().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 {
),
);
}
}
}

View File

@ -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<HeroRepository>(
create: (_) => heroRepository,
child: BlocProvider(
create: (_) => HeroListBloc(heroRepository)..add(FetchHeroes()),
child: Builder(
builder: (context) {
final heroListBloc = context.read<HeroListBloc>();
return BlocBuilder<HeroListBloc, HeroListState>(
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<HeroRepository>(
create: (_) => heroRepository,
child: BlocProvider(
create: (_) => HeroListBloc(heroRepository)..add(FetchHeroes()),
child: Consumer<LocaleNotifier>(
builder: (context, localeNotifier, child) {
return MaterialApp(
locale: localeNotifier.currentLocale,
localizationsDelegates: AppLocale.localizationsDelegates,
supportedLocales: AppLocale.supportedLocales,
home: Builder(
builder: (context) {
final heroListBloc = context.read<HeroListBloc>();
return BlocBuilder<HeroListBloc, HeroListState>(
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...'),
),
);
},
);
},
),
);
},
),
),
),
);
}
}
}