go router full

This commit is contained in:
mso7nueh
2024-06-06 16:02:39 +03:00
parent 7b55204659
commit 647e9e9133
42 changed files with 483 additions and 1018 deletions

View File

@@ -55,7 +55,7 @@ class PlanCreateBloc extends Bloc<PlanCreateEvent, PlanCreateState> {
);
if (response.statusCode == 200) {
NavigatorService.pushNamed(AppRoutes.planSelectScreen);
AppRouter.navigatorKey.currentContext?.goNamed('planSelectScreen');
} else {
print(
'Failed to create blog post. Status Code: ${response.statusCode} ${response.body}');

View File

@@ -10,7 +10,9 @@ import 'dart:convert';
import '/core/app_export.dart';
import 'package:eatvio_app/presentation/plans/plan_edit_screen/models/plan_edit_model.dart';
import 'package:go_router/go_router.dart';
part 'plan_edit_event.dart';
part 'plan_edit_state.dart';
/// A bloc that manages the state of a PlanEdit according to the event that is dispatched to it.
@@ -53,8 +55,7 @@ class PlanEditBloc extends Bloc<PlanEditEvent, PlanEditState> {
plan: data,
name: TextEditingController(text: data['name']),
details: TextEditingController(text: data['details']),
duration:
TextEditingController(text: data['duration'].toString())));
duration: TextEditingController(text: data['duration'].toString())));
}
} else {
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
@@ -75,15 +76,15 @@ class PlanEditBloc extends Bloc<PlanEditEvent, PlanEditState> {
var currentState = state as PlanEditLoadedState;
int duration = int.parse(currentState.duration!.text) * 7;
print(duration);
var response = await sendPlan(currentState.plan, currentState.name?.text,
currentState.details?.text, duration.toString());
var response = await sendPlan(currentState.plan, currentState.name?.text, currentState.details?.text, duration.toString());
if (response.statusCode == 200) {
NavigatorService.pushNamed(AppRoutes.planScreen,
arguments: currentState.plan?['id'].toString());
AppRouter.navigatorKey.currentContext?.goNamed(
'planScreen',
extra: currentState.plan?['id'].toString(),
);
} else {
print(
'Failed to edit blog post. Status Code: ${response.statusCode} ${response.body}');
print('Failed to edit blog post. Status Code: ${response.statusCode} ${response.body}');
}
}
}

View File

@@ -13,10 +13,7 @@ import 'package:flutter/material.dart';
class PlanSelectScreen extends StatelessWidget {
static Widget builder(BuildContext context) {
return BlocProvider<PlanSelectBloc>(
create: (context) => PlanSelectBloc(
PlanSelectState(myProfileModelObj: PlanSelectModel()))
..add(PlanSelectInitialEvent()),
child: PlanSelectScreen());
create: (context) => PlanSelectBloc(PlanSelectState(myProfileModelObj: PlanSelectModel()))..add(PlanSelectInitialEvent()), child: PlanSelectScreen());
}
Widget build(BuildContext context) {
@@ -80,8 +77,7 @@ class PlanSelectScreen extends StatelessWidget {
leadingWidth: 28.h,
leading: AppbarLeadingImage(
imagePath: ImageConstant.imgArrowLeft,
margin: EdgeInsets.only(
left: 16.h, top: 15.v, bottom: 15.v),
margin: EdgeInsets.only(left: 16.h, top: 15.v, bottom: 15.v),
onTap: () => GoRouter.of(context).pop(),
),
title: Padding(
@@ -90,18 +86,15 @@ class PlanSelectScreen extends StatelessWidget {
children: [
AppbarSubtitle(
text: "Планы",
margin: EdgeInsets.only(
left: 80.h, top: 12.v, bottom: 9.v),
margin: EdgeInsets.only(left: 80.h, top: 12.v, bottom: 9.v),
),
Spacer(),
Padding(
padding: const EdgeInsets.only(right: 20.0),
child: GestureDetector(
onTap: () => Navigator.pushNamed(
context,
AppRoutes.planCreateScreen,
),
child: Icon(Icons.create_outlined)),
onTap: () => GoRouter.of(context).goNamed('planCreateScreen'),
child: Icon(Icons.create_outlined),
),
)
],
),
@@ -125,51 +118,51 @@ class PlanSelectScreen extends StatelessWidget {
itemBuilder: (context, index) {
final post = state.posts?[index];
return Container(
margin: EdgeInsets.only(bottom: 10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.all(Radius.circular(12)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 2,
blurRadius: 5,
offset: Offset(0, 3),
),
],
margin: EdgeInsets.only(bottom: 10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: const BorderRadius.all(Radius.circular(12)),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 2,
blurRadius: 5,
offset: Offset(0, 3),
),
],
),
child: ListTile(
leading: CachedNetworkImage(
imageUrl: dotenv.env['STORAGE_URL']! + post['image'],
imageBuilder: (context, imageProvider) => CircleAvatar(
backgroundImage: imageProvider,
radius: 25,
),
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => CircleAvatar(
child: Icon(Icons.person),
backgroundColor: Colors.white,
radius: 25,
),
),
child: ListTile(
leading: CachedNetworkImage(
imageUrl: dotenv.env['STORAGE_URL']! + post['image'],
imageBuilder: (context, imageProvider) => CircleAvatar(
backgroundImage: imageProvider,
radius: 25,
),
placeholder: (context, url) => CircularProgressIndicator(),
errorWidget: (context, url, error) => CircleAvatar(
child: Icon(Icons.person),
backgroundColor: Colors.white,
radius: 25,
),
title: Text(
post?['name'] ?? '',
style: CustomTextStyles.titleMediumSemiBold,
),
subtitle: Text(post?['details'] ?? ''),
onTap: () => GoRouter.of(context).goNamed(
'planScreen',
extra: post?['id'].toString(),
),
trailing: GestureDetector(
onTap: () => GoRouter.of(context).goNamed(
'planEditScreen',
extra: post?['id'].toString(),
),
title: Text(
post?['name'] ?? '',
style: CustomTextStyles.titleMediumSemiBold,
),
subtitle: Text(post?['details'] ?? ''),
onTap: () => Navigator.pushNamed(
context,
AppRoutes.planScreen,
arguments: post?['id'].toString(),
),
trailing: GestureDetector(
onTap: () => Navigator.pushNamed(
context,
AppRoutes.planEditScreen,
arguments: post?['id'].toString(),
),
child: Icon(Icons.edit)),
));
child: Icon(Icons.edit),
),
),
);
},
);
}

View File

@@ -10,7 +10,9 @@ import 'dart:convert';
import '/core/app_export.dart';
import 'package:eatvio_app/presentation/plans/plan_show_screen/models/plan_show_model.dart';
import 'package:go_router/go_router.dart';
part 'plan_show_event.dart';
part 'plan_show_state.dart';
/// A bloc that manages the state of a Plan according to the event that is dispatched to it.
@@ -42,9 +44,7 @@ class PlanBloc extends Bloc<PlanEvent, PlanState> {
if (data is Map<String, dynamic>) {
if (state is PlanLoadedState) {
var currentState = state as PlanLoadedState;
emit(currentState.copyWith(
planModelObj: currentState.planModelObj,
plan: currentState.plan));
emit(currentState.copyWith(planModelObj: currentState.planModelObj, plan: currentState.plan));
} else {
emit(PlanLoadedState(plan: data));
}
@@ -62,8 +62,7 @@ class PlanBloc extends Bloc<PlanEvent, PlanState> {
}
}
FutureOr<void> _deleteEntry(
DeleteEntry event, Emitter<PlanState> emit) async {
FutureOr<void> _deleteEntry(DeleteEntry event, Emitter<PlanState> emit) async {
final box = await Hive.openBox('token');
final token = box.get('token');
if (token == null) {
@@ -77,7 +76,7 @@ class PlanBloc extends Bloc<PlanEvent, PlanState> {
},
);
if (response.statusCode == 200) {
NavigatorService.pushNamed(AppRoutes.planSelectScreen);
AppRouter.navigatorKey.currentContext?.goNamed('planSelectScreen');
} else {
// Статус ответа не равен 200, обрабатываем ошибку
emit(PlanErrorState('Ошибка: ${response.statusCode}'));

View File

@@ -1,3 +1,4 @@
import 'bloc/plan_show_bloc.dart';
import 'models/plan_show_model.dart';
import 'package:eatvio_app/core/app_export.dart';
@@ -117,7 +118,7 @@ class PlanScreen extends StatelessWidget {
String currentRoute,
) {
switch (currentRoute) {
case AppRoutes.planScreen:
case '/plan':
return PlanScreen.builder(context);
default:
return PlanScreen();