77 lines
2.9 KiB
Dart
77 lines
2.9 KiB
Dart
|
import 'package:flutter/material.dart';
|
||
|
import 'package:flutter_bloc/flutter_bloc.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) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
|
title: const Text('Heroes'),
|
||
|
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(
|
||
|
hintText: 'Search for a hero...',
|
||
|
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) {
|
||
|
return ListView.builder(
|
||
|
itemCount: searchState.heroes.length,
|
||
|
itemBuilder: (context, index) {
|
||
|
return HeroCard(hero: searchState.heroes[index]);
|
||
|
},
|
||
|
);
|
||
|
} 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) {
|
||
|
return ListView.builder(
|
||
|
itemCount: listState.heroes.length,
|
||
|
itemBuilder: (context, index) {
|
||
|
return HeroCard(hero: listState.heroes[index]);
|
||
|
},
|
||
|
);
|
||
|
} else if (listState is HeroListError) {
|
||
|
return Center(child: Text('Error: ${listState.message}'));
|
||
|
}
|
||
|
return const Center(child: Text('No heroes available.'));
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
return const Center(child: Text('No heroes found.'));
|
||
|
},
|
||
|
),
|
||
|
);
|
||
|
}
|
||
|
}
|