PMU_PIbd32_Kamcharova_K.A/lib/main.dart
2024-12-10 13:41:28 +04:00

214 lines
6.9 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<HomeBloc>(
create: (_) => HomeBloc(CharacterService()),
),
BlocProvider<DebouncedSearchCubit>(
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<HomeBloc>().add(HomeLoadDataEvent());
return Scaffold(
appBar: AppBar(
title: Text(title),
actions: [
IconButton(
icon: Icon(Icons.search),
onPressed: () {
showSearch(
context: context,
delegate: CharacterSearchDelegate(),
);
},
),
],
),
body: 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;
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<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();
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>();
debouncedSearchCubit.search(query);
return BlocBuilder<DebouncedSearchCubit, String>(
builder: (context, searchQuery) {
if (searchQuery.isNotEmpty) {
context.read<HomeBloc>().add(HomeLoadDataEvent(searchQuery: searchQuery));
}
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,
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<Widget> 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);
},
);
}
}