import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.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'; 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 characterDTO = characters[index]; // Убедитесь, что это CharacterDTO return ListTile( leading: SizedBox( width: 40, child: Image.network(characterDTO.imageUrl, width: 50, height: 50), ), title: Text(characterDTO.name), subtitle: Text(characterDTO.typeString), // Используем typeString из CharacterDTO onTap: () { // Переход на страницу с деталями персонажа, передаем CharacterDTO Navigator.push( context, MaterialPageRoute( builder: (context) => CharacterDetailPage(characterDTO: characterDTO), // Передаем CharacterDTO ), ); }, ); }, ); } 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(characterDTO: character), // Передаем правильный параметр ), ); }, ); }, ); }, ), ); } @override Widget buildResults(BuildContext context) { final debouncedSearchCubit = context.read(); debouncedSearchCubit.search(query); return BlocBuilder( builder: (context, searchQuery) { 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 characterDTO = characters[index]; // Здесь определяем characterDTO return ListTile( leading: SizedBox( width: 40, child: Image.network(characterDTO.imageUrl, width: 50, height: 50), ), title: Text(characterDTO.name), subtitle: Text(characterDTO.typeString), // Используем typeString onTap: () { // Переход на страницу с деталями персонажа Navigator.push( context, MaterialPageRoute( builder: (context) => CharacterDetailPage(characterDTO: characterDTO), // Передаем characterDTO ), ); }, ); } ); } 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); }, ); } }