PMU_PIbd32_Kamcharova_K.A/lib/main.dart

171 lines
5.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'bloc/bloc.dart';
import 'bloc/events.dart';
import 'bloc/state.dart';
import 'utils/character_service.dart';
import 'models/character.dart';
import 'pages/character_detail_page.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'l10n/messages_all.dart'; // Обновите на путь к сгенерированным локализациям
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Load saved language preference
final prefs = await SharedPreferences.getInstance();
final savedLocale = prefs.getString('locale') ?? 'en';
runApp(
MultiBlocProvider(
providers: [
BlocProvider<HomeBloc>(
create: (_) => HomeBloc(CharacterService()),
),
BlocProvider<DebouncedSearchCubit>(
create: (_) => DebouncedSearchCubit(),
),
],
child: MyApp(savedLocale: savedLocale),
),
);
}
class MyApp extends StatefulWidget {
final String savedLocale;
const MyApp({required this.savedLocale, super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
late Locale _locale;
@override
void initState() {
super.initState();
_locale = Locale(widget.savedLocale);
}
void _changeLanguage(String languageCode) async {
setState(() {
_locale = Locale(languageCode);
});
// Save selected language to preferences
final prefs = await SharedPreferences.getInstance();
prefs.setString('locale', languageCode);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
localizationsDelegates: [
S.delegate, // Используйте сгенерированные локализации
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''),
const Locale('ru', ''),
],
locale: _locale,
localeResolutionCallback: (locale, supportedLocales) {
return supportedLocales.contains(locale) ? locale : const Locale('en');
},
home: MyHomePage(
title: S.of(context).appTitle, // Локализованная строка
onLanguageChanged: _changeLanguage,
),
);
}
}
class MyHomePage extends StatelessWidget {
final String title;
final Function(String) onLanguageChanged;
const MyHomePage({
required this.title,
required this.onLanguageChanged,
super.key,
});
@override
Widget build(BuildContext context) {
context.read<HomeBloc>().add(HomeLoadDataEvent());
return Scaffold(
appBar: AppBar(
title: Text(title),
actions: [
PopupMenuButton<String>(
onSelected: (languageCode) {
onLanguageChanged(languageCode);
},
icon: const Icon(Icons.language),
itemBuilder: (context) => [
PopupMenuItem(
value: 'en',
child: Text(S.of(context).languageEnglish),
),
PopupMenuItem(
value: 'ru',
child: Text(S.of(context).languageRussian),
),
],
),
IconButton(
icon: const Icon(Icons.search),
onPressed: () {
showSearch(
context: context,
delegate: CharacterSearchDelegate(),
);
},
),
],
),
body: BlocBuilder<HomeBloc, HomeState>(
builder: (context, state) {
if (state.status == HomeStatus.loading) {
return const Center(child: CircularProgressIndicator());
} else if (state.status == HomeStatus.error) {
return Center(child: Text(S.of(context).errorMessage(state.errorMessage)));
} else if (state.status == HomeStatus.loaded) {
final characters = state.characters;
return ListView.builder(
itemCount: characters.length,
itemBuilder: (context, index) {
final characterDTO = characters[index];
return ListTile(
leading: SizedBox(
width: 40,
child: Image.network(characterDTO.imageUrl, width: 50, height: 50),
),
title: Text(characterDTO.name),
subtitle: Text(characterDTO.typeString),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => CharacterDetailPage(characterDTO: characterDTO),
),
);
},
);
},
);
} else {
return Center(child: Text(S.of(context).noData));
}
},
),
);
}
}