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