my plans and my diets

This commit is contained in:
Username1548
2024-12-23 15:36:00 +07:00
parent 15a6ff44ef
commit 5ef1f3a660
52 changed files with 3857 additions and 573 deletions

View File

@@ -0,0 +1,250 @@
import 'package:easy_debounce/easy_debounce.dart';
import 'package:eatvio_app/core/const.dart';
import 'package:eatvio_app/features/my_diets/presentation/cubit/my_diets_state.dart';
import 'package:eatvio_app/features/my_diets/repositories/my_diets_repository.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class MyDietsCubit extends Cubit<MyDietsState> {
final MyDietsRepository myDietsRepository;
int currentDraftPage = 1;
int currentPersonalPage = 1;
int currentActivePage = 1;
late int lastDraftPage;
late int lastPersonalPage;
late int lastActivePage;
bool isLoadingDraft = false;
bool isLoadingPersonal = false;
bool isLoadingActive = false;
String? _activeQuery;
String? _personalQuery;
String? _draftQuery;
static const List<String?> _sortingValues = [
null,
'chosen_diets_count:desc',
'created_at:desc',
'created_at',
'name',
'name:desc',
];
static const List<int?> _durationValues = [null, ...durationValues];
static const List<String?> _types = [null, ...dietTypeValues];
MyDietsCubit({required this.myDietsRepository}) : super(MyDietsState());
void changeTypeFilter(int index) {
if (state.selectedStateIndex == 0) {
emit(state.copyWith(activeFilterState: state.activeFilterState.copyWith(typeIndex: index), error: null));
EasyDebounce.debounce(
'loadDiets',
Duration(seconds: 1),
() => loadDiets(
query: _activeQuery,
),
);
} else if (state.selectedStateIndex == 1) {
emit(state.copyWith(draftFilterState: state.draftFilterState.copyWith(typeIndex: index), error: null));
EasyDebounce.debounce(
'loadDiets',
Duration(seconds: 1),
() => loadDiets(
query: _draftQuery,
),
);
} else if (state.selectedStateIndex == 2) {
emit(state.copyWith(personalFilterState: state.personalFilterState.copyWith(typeIndex: index), error: null));
EasyDebounce.debounce(
'loadDiets',
Duration(seconds: 1),
() => loadDiets(
query: _personalQuery,
),
);
}
}
void changeSortingType(int index) {
if (state.selectedStateIndex == 0) {
emit(state.copyWith(activeFilterState: state.activeFilterState.copyWith(sortingIndex: index), error: null));
EasyDebounce.debounce(
'loadDiets',
Duration(seconds: 1),
() => loadDiets(
query: _activeQuery,
),
);
} else if (state.selectedStateIndex == 1) {
emit(state.copyWith(draftFilterState: state.draftFilterState.copyWith(sortingIndex: index), error: null));
EasyDebounce.debounce(
'loadDiets',
Duration(seconds: 1),
() => loadDiets(
query: _draftQuery,
),
);
} else if (state.selectedStateIndex == 2) {
emit(state.copyWith(personalFilterState: state.personalFilterState.copyWith(sortingIndex: index), error: null));
EasyDebounce.debounce(
'loadDiets',
Duration(seconds: 1),
() => loadDiets(
query: _personalQuery,
),
);
}
}
void changeDurationFilter(int index) {
if (state.selectedStateIndex == 0) {
emit(state.copyWith(activeFilterState: state.activeFilterState.copyWith(durationIndex: index), error: null));
EasyDebounce.debounce(
'loadDiets',
Duration(seconds: 1),
() => loadDiets(
query: _activeQuery,
),
);
} else if (state.selectedStateIndex == 1) {
emit(state.copyWith(draftFilterState: state.draftFilterState.copyWith(durationIndex: index), error: null));
EasyDebounce.debounce(
'loadDiets',
Duration(seconds: 1),
() => loadDiets(
query: _draftQuery,
),
);
} else if (state.selectedStateIndex == 2) {
emit(state.copyWith(personalFilterState: state.personalFilterState.copyWith(durationIndex: index), error: null));
EasyDebounce.debounce(
'loadDiets',
Duration(seconds: 1),
() => loadDiets(
query: _personalQuery,
),
);
}
}
void changeSelectedStateIndex(int index) {
emit(state.copyWith(selectedStateIndex: index));
}
Future<void> loadDiets({
String? query,
}) async {
switch (state.selectedStateIndex) {
case 0:
await _loadActiveDiets(query: query);
break;
case 1:
await _loadDraftDiets(query: query);
break;
case 2:
await _loadPersonalDiets(query: query);
break;
}
}
Future<void> loadAllDiets() async {
emit(state.copyWith(isLoading: true));
await _loadActiveDiets();
await _loadDraftDiets();
await _loadPersonalDiets();
emit(state.copyWith(isLoading: false));
}
Future<void> _loadActiveDiets({String? query}) async {
try {
_activeQuery = query;
final type = _types[state.activeFilterState.typeIndex];
final duration = _durationValues[state.activeFilterState.durationIndex];
final sorting = _sortingValues[state.activeFilterState.sortingIndex];
final dietsPagination = await myDietsRepository.getDiets(
query: query,
types: type,
duration: duration,
sorting: sorting,
);
lastActivePage = dietsPagination.meta.lastPage;
emit(
state.copyWith(
activeDiets: dietsPagination.data,
isLoading: isLoadingActive,
error: null,
),
);
} catch (e) {
emit(
state.copyWith(error: e.toString()),
);
}
}
Future<void> _loadDraftDiets({String? query}) async {
try {
_draftQuery = query;
final type = _types[state.draftFilterState.typeIndex];
final duration = _durationValues[state.draftFilterState.durationIndex];
final sorting = _sortingValues[state.draftFilterState.sortingIndex];
final dietsPagination = await myDietsRepository.getDiets(
query: query,
types: type,
duration: duration,
sorting: sorting,
);
lastActivePage = dietsPagination.meta.lastPage;
emit(
state.copyWith(
draftDiets: dietsPagination.data,
isLoading: isLoadingDraft,
error: null,
),
);
} catch (e) {
emit(
state.copyWith(error: e.toString()),
);
}
}
Future<void> _loadPersonalDiets({String? query}) async {
try {
_personalQuery = query;
final type = _types[state.personalFilterState.typeIndex];
final duration = _durationValues[state.personalFilterState.durationIndex];
final sorting = _sortingValues[state.personalFilterState.sortingIndex];
final dietsPagination = await myDietsRepository.getDiets(
query: query,
types: type,
duration: duration,
sorting: sorting,
);
lastActivePage = dietsPagination.meta.lastPage;
emit(
state.copyWith(
personalDiets: dietsPagination.data,
isLoading: isLoadingPersonal,
error: null,
),
);
} catch (e) {
emit(
state.copyWith(error: e.toString()),
);
}
}
Future<bool> deleteDiet(int dietId) async {
try {
await myDietsRepository.deleteDiet(dietId);
await loadAllDiets();
return true;
} catch (e) {
emit(
state.copyWith(error: e.toString()),
);
return false;
}
}
}

View File

@@ -0,0 +1,41 @@
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:eatvio_app/features/feed/presentation/bloc/diets_cubit/diets_state.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'my_diets_state.freezed.dart';
@freezed
class MyDietsState with _$MyDietsState {
factory MyDietsState({
@Default([]) List<Diet> activeDiets,
@Default([]) List<Diet> draftDiets,
@Default([]) List<Diet> personalDiets,
@Default(false) bool isLoading,
@Default(null) String? error,
@Default(0) int selectedStateIndex,
@Default(
const DietsFilterState(
durationIndex: 0,
sortingIndex: 0,
typeIndex: 0,
),
)
DietsFilterState activeFilterState,
@Default(
const DietsFilterState(
durationIndex: 0,
sortingIndex: 0,
typeIndex: 0,
),
)
DietsFilterState draftFilterState,
@Default(
const DietsFilterState(
durationIndex: 0,
sortingIndex: 0,
typeIndex: 0,
),
)
DietsFilterState personalFilterState,
}) = _MyDietsState;
}

View File

@@ -0,0 +1,400 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'my_diets_state.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
/// @nodoc
mixin _$MyDietsState {
List<Diet> get activeDiets => throw _privateConstructorUsedError;
List<Diet> get draftDiets => throw _privateConstructorUsedError;
List<Diet> get personalDiets => throw _privateConstructorUsedError;
bool get isLoading => throw _privateConstructorUsedError;
String? get error => throw _privateConstructorUsedError;
int get selectedStateIndex => throw _privateConstructorUsedError;
DietsFilterState get activeFilterState => throw _privateConstructorUsedError;
DietsFilterState get draftFilterState => throw _privateConstructorUsedError;
DietsFilterState get personalFilterState =>
throw _privateConstructorUsedError;
/// Create a copy of MyDietsState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$MyDietsStateCopyWith<MyDietsState> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $MyDietsStateCopyWith<$Res> {
factory $MyDietsStateCopyWith(
MyDietsState value, $Res Function(MyDietsState) then) =
_$MyDietsStateCopyWithImpl<$Res, MyDietsState>;
@useResult
$Res call(
{List<Diet> activeDiets,
List<Diet> draftDiets,
List<Diet> personalDiets,
bool isLoading,
String? error,
int selectedStateIndex,
DietsFilterState activeFilterState,
DietsFilterState draftFilterState,
DietsFilterState personalFilterState});
$DietsFilterStateCopyWith<$Res> get activeFilterState;
$DietsFilterStateCopyWith<$Res> get draftFilterState;
$DietsFilterStateCopyWith<$Res> get personalFilterState;
}
/// @nodoc
class _$MyDietsStateCopyWithImpl<$Res, $Val extends MyDietsState>
implements $MyDietsStateCopyWith<$Res> {
_$MyDietsStateCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of MyDietsState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? activeDiets = null,
Object? draftDiets = null,
Object? personalDiets = null,
Object? isLoading = null,
Object? error = freezed,
Object? selectedStateIndex = null,
Object? activeFilterState = null,
Object? draftFilterState = null,
Object? personalFilterState = null,
}) {
return _then(_value.copyWith(
activeDiets: null == activeDiets
? _value.activeDiets
: activeDiets // ignore: cast_nullable_to_non_nullable
as List<Diet>,
draftDiets: null == draftDiets
? _value.draftDiets
: draftDiets // ignore: cast_nullable_to_non_nullable
as List<Diet>,
personalDiets: null == personalDiets
? _value.personalDiets
: personalDiets // ignore: cast_nullable_to_non_nullable
as List<Diet>,
isLoading: null == isLoading
? _value.isLoading
: isLoading // ignore: cast_nullable_to_non_nullable
as bool,
error: freezed == error
? _value.error
: error // ignore: cast_nullable_to_non_nullable
as String?,
selectedStateIndex: null == selectedStateIndex
? _value.selectedStateIndex
: selectedStateIndex // ignore: cast_nullable_to_non_nullable
as int,
activeFilterState: null == activeFilterState
? _value.activeFilterState
: activeFilterState // ignore: cast_nullable_to_non_nullable
as DietsFilterState,
draftFilterState: null == draftFilterState
? _value.draftFilterState
: draftFilterState // ignore: cast_nullable_to_non_nullable
as DietsFilterState,
personalFilterState: null == personalFilterState
? _value.personalFilterState
: personalFilterState // ignore: cast_nullable_to_non_nullable
as DietsFilterState,
) as $Val);
}
/// Create a copy of MyDietsState
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$DietsFilterStateCopyWith<$Res> get activeFilterState {
return $DietsFilterStateCopyWith<$Res>(_value.activeFilterState, (value) {
return _then(_value.copyWith(activeFilterState: value) as $Val);
});
}
/// Create a copy of MyDietsState
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$DietsFilterStateCopyWith<$Res> get draftFilterState {
return $DietsFilterStateCopyWith<$Res>(_value.draftFilterState, (value) {
return _then(_value.copyWith(draftFilterState: value) as $Val);
});
}
/// Create a copy of MyDietsState
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$DietsFilterStateCopyWith<$Res> get personalFilterState {
return $DietsFilterStateCopyWith<$Res>(_value.personalFilterState, (value) {
return _then(_value.copyWith(personalFilterState: value) as $Val);
});
}
}
/// @nodoc
abstract class _$$MyDietsStateImplCopyWith<$Res>
implements $MyDietsStateCopyWith<$Res> {
factory _$$MyDietsStateImplCopyWith(
_$MyDietsStateImpl value, $Res Function(_$MyDietsStateImpl) then) =
__$$MyDietsStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call(
{List<Diet> activeDiets,
List<Diet> draftDiets,
List<Diet> personalDiets,
bool isLoading,
String? error,
int selectedStateIndex,
DietsFilterState activeFilterState,
DietsFilterState draftFilterState,
DietsFilterState personalFilterState});
@override
$DietsFilterStateCopyWith<$Res> get activeFilterState;
@override
$DietsFilterStateCopyWith<$Res> get draftFilterState;
@override
$DietsFilterStateCopyWith<$Res> get personalFilterState;
}
/// @nodoc
class __$$MyDietsStateImplCopyWithImpl<$Res>
extends _$MyDietsStateCopyWithImpl<$Res, _$MyDietsStateImpl>
implements _$$MyDietsStateImplCopyWith<$Res> {
__$$MyDietsStateImplCopyWithImpl(
_$MyDietsStateImpl _value, $Res Function(_$MyDietsStateImpl) _then)
: super(_value, _then);
/// Create a copy of MyDietsState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? activeDiets = null,
Object? draftDiets = null,
Object? personalDiets = null,
Object? isLoading = null,
Object? error = freezed,
Object? selectedStateIndex = null,
Object? activeFilterState = null,
Object? draftFilterState = null,
Object? personalFilterState = null,
}) {
return _then(_$MyDietsStateImpl(
activeDiets: null == activeDiets
? _value._activeDiets
: activeDiets // ignore: cast_nullable_to_non_nullable
as List<Diet>,
draftDiets: null == draftDiets
? _value._draftDiets
: draftDiets // ignore: cast_nullable_to_non_nullable
as List<Diet>,
personalDiets: null == personalDiets
? _value._personalDiets
: personalDiets // ignore: cast_nullable_to_non_nullable
as List<Diet>,
isLoading: null == isLoading
? _value.isLoading
: isLoading // ignore: cast_nullable_to_non_nullable
as bool,
error: freezed == error
? _value.error
: error // ignore: cast_nullable_to_non_nullable
as String?,
selectedStateIndex: null == selectedStateIndex
? _value.selectedStateIndex
: selectedStateIndex // ignore: cast_nullable_to_non_nullable
as int,
activeFilterState: null == activeFilterState
? _value.activeFilterState
: activeFilterState // ignore: cast_nullable_to_non_nullable
as DietsFilterState,
draftFilterState: null == draftFilterState
? _value.draftFilterState
: draftFilterState // ignore: cast_nullable_to_non_nullable
as DietsFilterState,
personalFilterState: null == personalFilterState
? _value.personalFilterState
: personalFilterState // ignore: cast_nullable_to_non_nullable
as DietsFilterState,
));
}
}
/// @nodoc
class _$MyDietsStateImpl implements _MyDietsState {
_$MyDietsStateImpl(
{final List<Diet> activeDiets = const [],
final List<Diet> draftDiets = const [],
final List<Diet> personalDiets = const [],
this.isLoading = false,
this.error = null,
this.selectedStateIndex = 0,
this.activeFilterState = const DietsFilterState(
durationIndex: 0, sortingIndex: 0, typeIndex: 0),
this.draftFilterState = const DietsFilterState(
durationIndex: 0, sortingIndex: 0, typeIndex: 0),
this.personalFilterState = const DietsFilterState(
durationIndex: 0, sortingIndex: 0, typeIndex: 0)})
: _activeDiets = activeDiets,
_draftDiets = draftDiets,
_personalDiets = personalDiets;
final List<Diet> _activeDiets;
@override
@JsonKey()
List<Diet> get activeDiets {
if (_activeDiets is EqualUnmodifiableListView) return _activeDiets;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_activeDiets);
}
final List<Diet> _draftDiets;
@override
@JsonKey()
List<Diet> get draftDiets {
if (_draftDiets is EqualUnmodifiableListView) return _draftDiets;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_draftDiets);
}
final List<Diet> _personalDiets;
@override
@JsonKey()
List<Diet> get personalDiets {
if (_personalDiets is EqualUnmodifiableListView) return _personalDiets;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_personalDiets);
}
@override
@JsonKey()
final bool isLoading;
@override
@JsonKey()
final String? error;
@override
@JsonKey()
final int selectedStateIndex;
@override
@JsonKey()
final DietsFilterState activeFilterState;
@override
@JsonKey()
final DietsFilterState draftFilterState;
@override
@JsonKey()
final DietsFilterState personalFilterState;
@override
String toString() {
return 'MyDietsState(activeDiets: $activeDiets, draftDiets: $draftDiets, personalDiets: $personalDiets, isLoading: $isLoading, error: $error, selectedStateIndex: $selectedStateIndex, activeFilterState: $activeFilterState, draftFilterState: $draftFilterState, personalFilterState: $personalFilterState)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$MyDietsStateImpl &&
const DeepCollectionEquality()
.equals(other._activeDiets, _activeDiets) &&
const DeepCollectionEquality()
.equals(other._draftDiets, _draftDiets) &&
const DeepCollectionEquality()
.equals(other._personalDiets, _personalDiets) &&
(identical(other.isLoading, isLoading) ||
other.isLoading == isLoading) &&
(identical(other.error, error) || other.error == error) &&
(identical(other.selectedStateIndex, selectedStateIndex) ||
other.selectedStateIndex == selectedStateIndex) &&
(identical(other.activeFilterState, activeFilterState) ||
other.activeFilterState == activeFilterState) &&
(identical(other.draftFilterState, draftFilterState) ||
other.draftFilterState == draftFilterState) &&
(identical(other.personalFilterState, personalFilterState) ||
other.personalFilterState == personalFilterState));
}
@override
int get hashCode => Object.hash(
runtimeType,
const DeepCollectionEquality().hash(_activeDiets),
const DeepCollectionEquality().hash(_draftDiets),
const DeepCollectionEquality().hash(_personalDiets),
isLoading,
error,
selectedStateIndex,
activeFilterState,
draftFilterState,
personalFilterState);
/// Create a copy of MyDietsState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$MyDietsStateImplCopyWith<_$MyDietsStateImpl> get copyWith =>
__$$MyDietsStateImplCopyWithImpl<_$MyDietsStateImpl>(this, _$identity);
}
abstract class _MyDietsState implements MyDietsState {
factory _MyDietsState(
{final List<Diet> activeDiets,
final List<Diet> draftDiets,
final List<Diet> personalDiets,
final bool isLoading,
final String? error,
final int selectedStateIndex,
final DietsFilterState activeFilterState,
final DietsFilterState draftFilterState,
final DietsFilterState personalFilterState}) = _$MyDietsStateImpl;
@override
List<Diet> get activeDiets;
@override
List<Diet> get draftDiets;
@override
List<Diet> get personalDiets;
@override
bool get isLoading;
@override
String? get error;
@override
int get selectedStateIndex;
@override
DietsFilterState get activeFilterState;
@override
DietsFilterState get draftFilterState;
@override
DietsFilterState get personalFilterState;
/// Create a copy of MyDietsState
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$MyDietsStateImplCopyWith<_$MyDietsStateImpl> get copyWith =>
throw _privateConstructorUsedError;
}

View File

@@ -0,0 +1,194 @@
import 'package:easy_debounce/easy_debounce.dart';
import 'package:eatvio_app/core/locator_service.dart';
import 'package:eatvio_app/core/theme/custom_text_style.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/widgets/add_item_button.dart';
import 'package:eatvio_app/core/widgets/custom_search_bar.dart';
import 'package:eatvio_app/core/widgets/loader.dart';
import 'package:eatvio_app/features/create_diet/presentation/create_diet_screen.dart';
import 'package:eatvio_app/features/feed/presentation/widgets/diet_filter_bottom_sheet.dart';
import 'package:eatvio_app/features/feed/presentation/widgets/filter_badge.dart';
import 'package:eatvio_app/features/my_diets/presentation/cubit/my_diets_cubit.dart';
import 'package:eatvio_app/features/my_diets/presentation/cubit/my_diets_state.dart';
import 'package:eatvio_app/features/my_diets/presentation/widgets/my_diet_widget.dart';
import 'package:eatvio_app/features/my_diets/presentation/widgets/my_diets_tab.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
class MyDietsScreen extends StatelessWidget {
const MyDietsScreen({super.key});
static const name = 'myDietsScreen';
static const path = '/my-diets-screen';
@override
Widget build(BuildContext context) {
return BlocProvider(create: (context) => sl<MyDietsCubit>()..loadAllDiets(), child: MyDietsScreenContent());
}
}
class MyDietsScreenContent extends StatelessWidget {
MyDietsScreenContent({super.key});
final activeDietsScrollController = ScrollController();
final draftDietsScrollController = ScrollController();
final personalDietsScrollController = ScrollController();
final activeDietsSearchController = SearchController();
final draftDietsSearchController = SearchController();
final personalDietsSearchController = SearchController();
@override
Widget build(BuildContext context) {
final s = S.of(context);
return Scaffold(
backgroundColor: appTheme.gray100,
appBar: AppBar(
centerTitle: true,
title: Text(
s.my_diets,
maxLines: 2,
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
softWrap: true,
style: TextStyles.headingXLSemibold.copyWith(color: appTheme.black),
),
),
body: BlocBuilder<MyDietsCubit, MyDietsState>(
buildWhen: (previous, current) => previous.selectedStateIndex != current.selectedStateIndex,
builder: (context, state) {
return NestedScrollView(
controller: switch (state.selectedStateIndex) {
1 => draftDietsScrollController,
2 => personalDietsScrollController,
_ => activeDietsScrollController
},
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
SliverAppBar(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(bottom: Radius.circular(12)),
),
backgroundColor: appTheme.gray100,
foregroundColor: appTheme.gray100,
automaticallyImplyLeading: false,
stretchTriggerOffset: 1,
floating: true,
primary: false,
snap: true,
scrolledUnderElevation: 0,
bottom: PreferredSize(
preferredSize: Size(
MediaQuery.sizeOf(context).width,
64,
),
child: Padding(
padding: const EdgeInsets.only(bottom: 0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 8),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: MyDietsTab(),
),
SizedBox(height: 8),
Row(
children: [
SizedBox(width: 4),
Expanded(
child: CustomSearchBar(
enableIcon: false,
backgroundColor: appTheme.whiteA700,
borderRadius: 100,
searchController: switch (state.selectedStateIndex) {
1 => draftDietsSearchController,
2 => personalDietsSearchController,
_ => activeDietsSearchController
},
onChanged: (query) {
EasyDebounce.debounce(
'searchDiets',
Duration(milliseconds: 500),
() {
context.read<MyDietsCubit>().loadDiets(query: query == '' ? null : query);
},
);
},
),
),
SizedBox(width: 12),
AddItemButton(
onTap: () => GoRouter.of(context).pushNamed(CreateDietScreen.name),
),
SizedBox(width: 4),
],
),
SizedBox(height: 16),
],
),
),
),
),
];
},
body: RefreshIndicator(
onRefresh: () => context.read<MyDietsCubit>().loadDiets(),
child: BlocBuilder<MyDietsCubit, MyDietsState>(
builder: (BuildContext context, MyDietsState state) {
if (state.isLoading) {
return Scaffold(
backgroundColor: Colors.grey.shade200,
body: const Loader(),
);
} else if (state.error != null) {
return Scaffold(
backgroundColor: Colors.grey.shade200,
body: Padding(
padding: const EdgeInsets.all(16),
child: Center(
child: Text(state.error ?? ''),
),
),
);
} else {
final diets = switch (state.selectedStateIndex) {
1 => state.draftDiets,
2 => state.personalDiets,
_ => state.activeDiets,
};
return Stack(
children: [
SizedBox.expand(),
ListView.separated(
shrinkWrap: true,
physics: BouncingScrollPhysics(),
itemBuilder: (context, index) {
return MyDietWidget(diet: diets[index]);
},
separatorBuilder: (context, index) {
return SizedBox(height: 20);
},
itemCount: diets.length,
),
Positioned(
right: 0,
bottom: 74,
child: FilterBadge(
filterBottomSheet: DietFilterBottomSheet(
myDietsCubit: context.read<MyDietsCubit>(),
),
),
),
],
);
}
},
),
),
);
},
),
);
}
}

View File

@@ -0,0 +1,252 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:eatvio_app/core/const.dart';
import 'package:eatvio_app/core/theme/custom_text_style.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/widgets/bottom_sheet_handler_bar.dart';
import 'package:eatvio_app/core/widgets/bottom_sheet_with_blur.dart';
import 'package:eatvio_app/core/widgets/custom_outlined_button.dart';
import 'package:eatvio_app/core/widgets/info_row.dart';
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
import 'package:eatvio_app/features/my_diets/presentation/cubit/my_diets_cubit.dart';
import 'package:eatvio_app/features/my_diets/presentation/cubit/my_diets_state.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_svg/svg.dart';
import 'package:go_router/go_router.dart';
import 'package:share_plus/share_plus.dart';
class MyDietWidget extends StatefulWidget {
final Diet diet;
const MyDietWidget({
super.key,
required this.diet,
});
@override
State<MyDietWidget> createState() => _MyDietWidgetState();
}
class _MyDietWidgetState extends State<MyDietWidget> {
@override
Widget build(BuildContext context) {
final s = S.of(context);
return DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: Colors.white,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Stack(
children: [
InkWell(
onTap: () => GoRouter.of(context).pushNamed('aboutDietScreen', extra: {'diet': widget.diet, 'navigateToComments': false}),
child: ClipRRect(
borderRadius: BorderRadius.vertical(top: Radius.circular(12)),
child: CachedNetworkImage(
imageUrl: baseUrl + '/storage/' + widget.diet.image,
fit: BoxFit.fitWidth,
width: MediaQuery.of(context).size.width,
),
),
),
Positioned(
bottom: 16,
child: SizedBox(
width: MediaQuery.sizeOf(context).width,
child: InfoRow(
diet: widget.diet,
itemsBackgroundColor: appTheme.whiteA700,
disableDescriptionOfTypes: true,
),
),
),
],
),
SizedBox(height: 12),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
widget.diet.name,
style: TextStyles.textMSemibold.copyWith(color: appTheme.black),
),
),
if (widget.diet.details.isNotEmpty) ...[
SizedBox(
height: 12,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Html(
data: widget.diet.details.length > 200 ? widget.diet.details.substring(0, 200) + '...' : widget.diet.details,
style: {
"*": Style(
margin: Margins.zero,
color: appTheme.black,
fontSize: FontSize(TextStyles.textSMedium.fontSize ?? 14),
fontWeight: TextStyles.textSMedium.fontWeight ?? FontWeight.w400,
),
},
),
),
],
],
),
SizedBox(height: 20),
BlocBuilder<MyDietsCubit, MyDietsState>(
builder: (context, state) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
children: [
Row(
children: [
Expanded(
child: CustomOutlinedButton(
text: switch (state.selectedStateIndex) {
1 => s.publish,
2 => s.toOffer,
_ => s.edit,
},
height: 52,
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.whiteA700),
buttonStyle: ButtonStyle(
shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))),
backgroundColor: WidgetStatePropertyAll(appTheme.blueA400),
side: WidgetStatePropertyAll(BorderSide.none),
),
),
),
if (state.selectedStateIndex == 0) ...[
SizedBox(width: 6),
CustomOutlinedButton(
onPressed: () async {
await Share.shareUri(
Uri.parse(
baseUrl + '/ru/diet/' + widget.diet.id.toString(),
),
);
},
height: 52,
width: 52,
leftIcon: SvgPicture.asset(
'assets/icons/share2.svg',
width: 20,
height: 20,
colorFilter: ColorFilter.mode(appTheme.whiteA700, BlendMode.srcIn),
),
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.red),
buttonStyle: ButtonStyle(
shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))),
backgroundColor: WidgetStatePropertyAll(appTheme.blueA400),
side: WidgetStatePropertyAll(BorderSide.none),
),
text: '',
),
],
],
),
SizedBox(height: 8),
Row(
children: [
Expanded(
child: CustomOutlinedButton(
text: switch (state.selectedStateIndex) {
0 => s.removeFromActive,
_ => s.edit,
},
height: 52,
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.black),
buttonStyle: ButtonStyle(
shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))),
backgroundColor: WidgetStatePropertyAll(appTheme.lightGray0),
side: WidgetStatePropertyAll(BorderSide.none),
),
),
),
SizedBox(width: 6),
CustomOutlinedButton(
onPressed: () async {
final result = await context.read<MyDietsCubit>().deleteDiet(widget.diet.id);
if (result)
Future.delayed(Duration(seconds: 3), () {
showBottomSheetWithBlur(
context: context,
child: Column(
children: [
SizedBox(
height: 20,
),
BottomSheetHandlerBar(),
SizedBox(
height: 20,
),
Text(
'${widget.diet.name}',
style: TextStyles.headingXLSemibold.copyWith(color: appTheme.black),
),
Text(
s.deleted,
style: TextStyles.headingXLSemibold.copyWith(color: appTheme.black),
),
SizedBox(
height: 20,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: CustomOutlinedButton(
onPressed: () {
context.pop();
},
text: 'Хорошо',
height: 52,
buttonTextStyle: TextStyles.textSSemibold.copyWith(color: appTheme.whiteA700),
buttonStyle: ButtonStyle(
side: WidgetStatePropertyAll(BorderSide.none),
backgroundColor: WidgetStatePropertyAll(appTheme.blueA400),
),
),
),
SizedBox(
height: 20,
),
],
),
);
});
},
height: 52,
width: 52,
leftIcon: SvgPicture.asset(
'assets/icons/trash_can.svg',
width: 20,
height: 20,
),
buttonTextStyle: TextStyles.textMSemibold.copyWith(color: appTheme.red),
buttonStyle: ButtonStyle(
shape: WidgetStatePropertyAll(RoundedRectangleBorder(borderRadius: BorderRadius.circular(20))),
backgroundColor: WidgetStatePropertyAll(appTheme.lightGray0),
side: WidgetStatePropertyAll(BorderSide.none),
),
text: '',
),
],
),
SizedBox(height: 8),
],
),
);
},
),
],
),
);
}
}

View File

@@ -0,0 +1,74 @@
import 'package:eatvio_app/core/theme/custom_text_style.dart';
import 'package:eatvio_app/core/theme/theme_helper.dart';
import 'package:eatvio_app/core/widgets/animated_option_button.dart';
import 'package:eatvio_app/features/my_diets/presentation/cubit/my_diets_cubit.dart';
import 'package:eatvio_app/features/my_diets/presentation/cubit/my_diets_state.dart';
import 'package:eatvio_app/generated/l10n.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class MyDietsTab extends StatefulWidget {
const MyDietsTab({super.key, this.additionalOnTap});
final void Function()? additionalOnTap;
@override
State<MyDietsTab> createState() => _MyDietsTabState();
}
class _MyDietsTabState extends State<MyDietsTab> {
final List<String> tabTitles = [];
@override
void didChangeDependencies() {
final s = S.of(context);
if (tabTitles.isEmpty) {
tabTitles.add(s.published);
tabTitles.add(s.drafted);
tabTitles.add(s.personal);
}
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
return BlocBuilder<MyDietsCubit, MyDietsState>(
builder: (context, state) {
return Align(
alignment: Alignment.centerLeft,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
Container(
width: 484,
height: 48,
decoration: BoxDecoration(
color: appTheme.whiteA700,
borderRadius: BorderRadius.circular(100),
),
padding: const EdgeInsets.all(4),
child: Row(
children: [
...List.generate(
3,
(index) => Expanded(
child: AnimatedOptionButton(
textStyle: TextStyles.textSMedium,
isSelected: state.selectedStateIndex == index,
text: tabTitles[index],
onTap: () {
widget.additionalOnTap?.call();
context.read<MyDietsCubit>().changeSelectedStateIndex(index);
},
),
),
),
],
),
),
],
),
),
);
},
);
}
}