2024-12-16 22:27:12 +04:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2024-12-17 01:12:29 +04:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../../Components/locale/l10n/app_locale.dart';
|
|
|
|
import '../../main.dart';
|
2024-12-16 22:27:12 +04:00
|
|
|
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) {
|
2024-12-17 01:12:29 +04:00
|
|
|
final locale = AppLocale.of(context)!; // Получаем текущую локализацию
|
2024-12-17 20:15:30 +04:00
|
|
|
|
2024-12-16 22:27:12 +04:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-12-17 20:15:30 +04:00
|
|
|
title: Text(locale.heroListTitle),
|
2024-12-17 01:12:29 +04:00
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
icon: const Icon(Icons.language),
|
|
|
|
onPressed: () {
|
|
|
|
// Переключение локали
|
|
|
|
context.read<LocaleNotifier>().toggleLocale();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
2024-12-16 22:27:12 +04:00
|
|
|
bottom: PreferredSize(
|
|
|
|
preferredSize: const Size.fromHeight(60),
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: TextField(
|
|
|
|
onChanged: (query) {
|
|
|
|
// Обновляем поиск при вводе текста
|
|
|
|
context.read<HeroSearchBloc>().add(SearchHeroes(query));
|
|
|
|
},
|
|
|
|
decoration: InputDecoration(
|
2024-12-17 01:12:29 +04:00
|
|
|
hintText: locale.search,
|
2024-12-16 22:27:12 +04:00
|
|
|
prefixIcon: const Icon(Icons.search),
|
|
|
|
border: OutlineInputBorder(
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
body: BlocBuilder<HeroSearchBloc, HeroSearchState>(
|
|
|
|
builder: (context, searchState) {
|
|
|
|
if (searchState is HeroSearchLoading) {
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
} else if (searchState is HeroSearchLoaded) {
|
2024-12-17 20:15:30 +04:00
|
|
|
return RefreshIndicator(
|
|
|
|
onRefresh: () async {
|
|
|
|
// Обновляем список героев
|
|
|
|
context.read<HeroListBloc>().add(FetchHeroes());
|
2024-12-16 22:27:12 +04:00
|
|
|
},
|
2024-12-17 20:15:30 +04:00
|
|
|
child: ListView.builder(
|
|
|
|
itemCount: searchState.heroes.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return HeroCard(hero: searchState.heroes[index]);
|
|
|
|
},
|
|
|
|
),
|
2024-12-16 22:27:12 +04:00
|
|
|
);
|
|
|
|
} else if (searchState is HeroSearchError) {
|
|
|
|
return Center(child: Text('Error: ${searchState.message}'));
|
|
|
|
} else if (searchState is HeroSearchInitial) {
|
|
|
|
return BlocBuilder<HeroListBloc, HeroListState>(
|
|
|
|
builder: (context, listState) {
|
|
|
|
if (listState is HeroListLoading) {
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
} else if (listState is HeroListLoaded) {
|
2024-12-17 20:15:30 +04:00
|
|
|
return RefreshIndicator(
|
|
|
|
onRefresh: () async {
|
|
|
|
// Выполняем новый запрос к API
|
|
|
|
context.read<HeroListBloc>().add(FetchHeroes());
|
2024-12-16 22:27:12 +04:00
|
|
|
},
|
2024-12-17 20:15:30 +04:00
|
|
|
child: ListView.builder(
|
|
|
|
itemCount: listState.heroes.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return HeroCard(hero: listState.heroes[index]);
|
|
|
|
},
|
|
|
|
),
|
2024-12-16 22:27:12 +04:00
|
|
|
);
|
|
|
|
} else if (listState is HeroListError) {
|
|
|
|
return Center(child: Text('Error: ${listState.message}'));
|
|
|
|
}
|
2024-12-17 20:15:30 +04:00
|
|
|
return Center(child: Text(locale.noHeroesAvailable));
|
2024-12-16 22:27:12 +04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2024-12-17 20:15:30 +04:00
|
|
|
return Center(child: Text(locale.noHeroesFound));
|
2024-12-16 22:27:12 +04:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-12-17 01:12:29 +04:00
|
|
|
}
|