import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'bloc.dart'; import 'events.dart'; import 'state.dart'; import 'character_service.dart'; import 'character.dart'; import 'pages/character_detail_page.dart'; void main() { runApp( MultiBlocProvider( providers: [ BlocProvider( create: (_) => HomeBloc(CharacterService()), ), BlocProvider( create: (_) => DebouncedSearchCubit(), ), ], child: MyApp(), ), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'My App', home: MyHomePage(title: 'Identity'), ); } } class MyHomePage extends StatelessWidget { final String title; MyHomePage({required this.title}); @override Widget build(BuildContext context) { // Загружаем данные сразу после инициализации страницы context.read().add(HomeLoadDataEvent()); return Scaffold( appBar: AppBar( title: Text(title), actions: [ IconButton( icon: Icon(Icons.search), onPressed: () { showSearch( context: context, delegate: CharacterSearchDelegate(), ); }, ), ], ), body: BlocBuilder( builder: (context, state) { if (state.status == HomeStatus.loading) { return Center(child: CircularProgressIndicator()); } else if (state.status == HomeStatus.error) { return Center(child: Text('Ошибка: ${state.errorMessage}')); } else if (state.status == HomeStatus.loaded) { final characters = state.characters; return ListView.builder( itemCount: characters.length, itemBuilder: (context, index) { final character = characters[index]; return ListTile( leading: SizedBox( width: 40, child: Image.network(character.imageUrl, width: 50, height: 50), ), title: Text(character.name), subtitle: Text(character.typeString), onTap: () { // Переход на страницу с деталями персонажа Navigator.push( context, MaterialPageRoute( builder: (context) => CharacterDetailPage(character: character), ), ); }, ); }, ); } else { return Center(child: Text('Нет данных.')); } }, ), ); } } class CharacterSearchDelegate extends SearchDelegate { @override Widget buildSuggestions(BuildContext context) { // Показываем предложения на основе текущего запроса return BlocProvider( create: (context) => HomeBloc(CharacterService()), child: BlocBuilder( builder: (context, state) { final suggestions = state.characters .where((character) => character.name.toLowerCase().contains(query.toLowerCase())) .toList(); return ListView.builder( itemCount: suggestions.length, itemBuilder: (context, index) { final character = suggestions[index]; return ListTile( title: Text(character.name), subtitle: Text(character.typeString), onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => CharacterDetailPage(character: character), ), ); }, ); }, ); }, ), ); } @override Widget buildResults(BuildContext context) { // Отправляем запрос с задержкой final debouncedSearchCubit = context.read(); debouncedSearchCubit.search(query); return BlocBuilder( builder: (context, searchQuery) { // Отправляем запрос на сервер через HomeBloc if (searchQuery.isNotEmpty) { context.read().add(HomeLoadDataEvent(searchQuery: searchQuery)); } return BlocBuilder( builder: (context, state) { if (state.status == HomeStatus.loading) { return Center(child: CircularProgressIndicator()); } else if (state.status == HomeStatus.error) { return Center(child: Text('Ошибка: ${state.errorMessage}')); } else if (state.status == HomeStatus.loaded) { final characters = state.characters .where((character) => character.name.toLowerCase().contains(searchQuery.toLowerCase())) .toList(); return ListView.builder( itemCount: characters.length, itemBuilder: (context, index) { final character = characters[index]; return ListTile( title: Text(character.name), subtitle: Text(character.typeString), onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => CharacterDetailPage(character: character), ), ); }, ); }, ); } else { return Center(child: Text('Нет результатов')); } }, ); }, ); } @override List buildActions(BuildContext context) { return [ IconButton( icon: Icon(Icons.clear), onPressed: () { query = ''; // Очистить запрос showSuggestions(context); // Показать предложения }, ), ]; } @override Widget buildLeading(BuildContext context) { return IconButton( icon: Icon(Icons.arrow_back), onPressed: () { close(context, null); // Закрыть поиск }, ); } }