Update CategoryListPage to use CategoryCubit with userId
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import 'package:budget_app/l10n/app_localizations.dart';
|
||||
import 'package:budget_app/logic/category/category_cubit.dart';
|
||||
import 'package:budget_app/logic/category/category_state.dart';
|
||||
import 'package:budget_app/models/category.dart';
|
||||
import 'package:budget_app/pages/category/category_edit_page.dart';
|
||||
import 'package:budget_app/pages/category/widgets/add_category_button.dart';
|
||||
import 'package:budget_app/pages/category/widgets/category_list_item.dart';
|
||||
import 'package:budget_app/logic/user/user_cubit.dart'; // Импортируем UserCubit
|
||||
import 'package:budget_app/logic/user/user_cubit.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
@@ -17,41 +18,38 @@ class CategoryListPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _CategoryListPageState extends State<CategoryListPage> {
|
||||
final GlobalKey<AnimatedListState> _listKey = GlobalKey();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// Оборачиваем Scaffold в BlocProvider, чтобы все дочерние виджеты,
|
||||
// включая floatingActionButton, имели доступ к CategoryCubit.
|
||||
return BlocProvider(
|
||||
create: (context) => GetIt.instance<CategoryCubit>()..loadCategories(),
|
||||
// Используем Builder для получения нового контекста, который "видит" BlocProvider.
|
||||
child: Builder(builder: (context) {
|
||||
final localizations = AppLocalizations.of(context)!;
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(localizations.editCategories)),
|
||||
body: BlocBuilder<CategoryCubit, List<Category>>(
|
||||
builder: (context, categories) {
|
||||
return AnimatedList(
|
||||
key: _listKey,
|
||||
initialItemCount: categories.length,
|
||||
itemBuilder: (context, index, animation) {
|
||||
final category = categories[index];
|
||||
return SizeTransition(
|
||||
sizeFactor: animation,
|
||||
child: CategoryListItem(
|
||||
body: BlocBuilder<CategoryCubit, CategoryState>(
|
||||
builder: (context, state) {
|
||||
if (state is CategoryLoading) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
} else if (state is CategoryError) {
|
||||
return Center(child: Text(state.message));
|
||||
} else if (state is CategoryLoaded) {
|
||||
return ListView.builder(
|
||||
itemCount: state.categories.length,
|
||||
itemBuilder: (context, index) {
|
||||
final category = state.categories[index];
|
||||
return CategoryListItem(
|
||||
category: category,
|
||||
onEdit: () => _editCategory(context, category),
|
||||
onDelete: () =>
|
||||
_deleteCategory(context, category, index),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
onDelete: () => _deleteCategory(context, category),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
// CategoryInitial
|
||||
return const Center(child: Text('Нет категорий'));
|
||||
},
|
||||
),
|
||||
floatingActionButton: AddCategoryButton(
|
||||
// Теперь этот context имеет доступ к CategoryCubit
|
||||
onPressed: () => _addCategory(context),
|
||||
),
|
||||
);
|
||||
@@ -65,13 +63,9 @@ class _CategoryListPageState extends State<CategoryListPage> {
|
||||
MaterialPageRoute(
|
||||
builder: (_) => CategoryEditPage(
|
||||
onSave: (name, color, icon, isIncome) {
|
||||
// Получаем ID текущего пользователя из UserCubit
|
||||
final userState = context.read<UserCubit>().state;
|
||||
if (userState is! UserLoaded || userState.user == null) {
|
||||
// Обработка случая, когда пользователь не аутентифицирован
|
||||
// Возможно, показать сообщение об ошибке или перенаправить на страницу входа
|
||||
return;
|
||||
}
|
||||
if (userState is! UserLoaded || userState.user == null) return;
|
||||
|
||||
final currentUserId = userState.user!.id;
|
||||
final newCategory = Category(
|
||||
name: name,
|
||||
@@ -80,8 +74,8 @@ class _CategoryListPageState extends State<CategoryListPage> {
|
||||
isIncome: isIncome,
|
||||
userId: currentUserId,
|
||||
);
|
||||
|
||||
context.read<CategoryCubit>().addCategory(newCategory);
|
||||
_listKey.currentState?.insertItem(0);
|
||||
},
|
||||
),
|
||||
),
|
||||
@@ -95,12 +89,17 @@ class _CategoryListPageState extends State<CategoryListPage> {
|
||||
builder: (_) => CategoryEditPage(
|
||||
category: category,
|
||||
onSave: (name, color, icon, isIncome) {
|
||||
final userState = context.read<UserCubit>().state;
|
||||
if (userState is! UserLoaded || userState.user == null) return;
|
||||
|
||||
final currentUserId = userState.user!.id;
|
||||
final updatedCategory = category.copyWith(
|
||||
name: name,
|
||||
color: color,
|
||||
icon: icon,
|
||||
isIncome: isIncome,
|
||||
);
|
||||
|
||||
context.read<CategoryCubit>().updateCategory(updatedCategory);
|
||||
},
|
||||
),
|
||||
@@ -108,18 +107,11 @@ class _CategoryListPageState extends State<CategoryListPage> {
|
||||
);
|
||||
}
|
||||
|
||||
void _deleteCategory(BuildContext context, Category category, int index) {
|
||||
void _deleteCategory(BuildContext context, Category category) {
|
||||
final userState = context.read<UserCubit>().state;
|
||||
if (userState is! UserLoaded || userState.user == null) return;
|
||||
|
||||
final currentUserId = userState.user!.id;
|
||||
context.read<CategoryCubit>().deleteCategory(category.id);
|
||||
_listKey.currentState?.removeItem(
|
||||
index,
|
||||
(context, animation) => SizeTransition(
|
||||
sizeFactor: animation,
|
||||
child: CategoryListItem(
|
||||
category: category,
|
||||
onEdit: () {},
|
||||
onDelete: () {},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user