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 '../components/locale/l10n/app_locale.dart'; import '../pages/character_detail_page.dart'; class CharacterSearchDelegate extends SearchDelegate { @override List? buildActions(BuildContext context) { return [ IconButton( icon: const Icon(Icons.clear), onPressed: () { query = ''; }, ), ]; } @override Widget? buildLeading(BuildContext context) { return IconButton( icon: const Icon(Icons.arrow_back), onPressed: () { close(context, null); }, ); } @override Widget buildResults(BuildContext context) { context.read().add(HomeSearchEvent(query)); return BlocBuilder( builder: (context, state) { if (state.status == HomeStatus.loading) { return const Center(child: CircularProgressIndicator()); } else if (state.status == HomeStatus.error) { return Center( child: Text( AppLocale.of(context)?.errorMessage(state.errorMessage) ?? 'Error', ), ); } else if (state.status == HomeStatus.loaded) { final characters = state.characters; if (characters.isEmpty) { return Center( child: Text(AppLocale.of(context)?.noData ?? 'No characters found.'), ); } 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 const SizedBox.shrink(); } }, ); } @override Widget buildSuggestions(BuildContext context) { return const SizedBox.shrink(); } }