Skip to content

Commit 822dcce

Browse files
committed
feat(app): navigate using go_router in AppBloc
- Pass context to AppNavigationIndexChanged - Navigate using goNamed in AppBloc
1 parent e6f91c4 commit 822dcce

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
import 'package:bloc/bloc.dart';
22
import 'package:equatable/equatable.dart';
33
import 'package:flutter/material.dart';
4+
import 'package:go_router/go_router.dart';
5+
import 'package:ht_main/router/routes.dart';
46

57
part 'app_event.dart';
68
part 'app_state.dart';
79

810
class AppBloc extends Bloc<AppEvent, AppState> {
911
AppBloc() : super(const AppState()) {
10-
on<AppNavigationIndexChanged>((event, emit) {
11-
emit(state.copyWith(selectedBottomNavigationIndex: event.index));
12-
});
13-
on<AppThemeChanged>((event, emit) {
14-
emit(
15-
state.copyWith(
16-
themeMode: state.themeMode == ThemeMode.light
17-
? ThemeMode.dark
18-
: ThemeMode.light,
19-
),
20-
);
21-
});
12+
on<AppNavigationIndexChanged>(_onAppNavigationIndexChanged);
13+
on<AppThemeChanged>(_onAppThemeChanged);
14+
}
15+
16+
void _onAppNavigationIndexChanged(
17+
AppNavigationIndexChanged event,
18+
Emitter<AppState> emit,
19+
) {
20+
emit(state.copyWith(selectedBottomNavigationIndex: event.index));
21+
event.context.goNamed(Routes.getRouteNameByIndex(event.index));
22+
}
23+
24+
void _onAppThemeChanged(
25+
AppThemeChanged event,
26+
Emitter<AppState> emit,
27+
) {
28+
emit(
29+
state.copyWith(
30+
themeMode: state.themeMode == ThemeMode.light
31+
? ThemeMode.dark
32+
: ThemeMode.light,
33+
),
34+
);
2235
}
2336
}

lib/app/bloc/app_event.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ part of 'app_bloc.dart';
44
sealed class AppEvent {}
55

66
final class AppNavigationIndexChanged extends AppEvent {
7-
AppNavigationIndexChanged({required this.index});
7+
AppNavigationIndexChanged({required this.index, required this.context});
88

99
final int index;
10+
final BuildContext context;
1011
}
1112

1213
final class AppThemeChanged extends AppEvent {}

lib/app/view/app_scaffold.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class AppScaffold extends StatelessWidget {
2424
largeSecondaryBody: AdaptiveScaffold.emptyBuilder,
2525
selectedIndex: state.selectedBottomNavigationIndex,
2626
onSelectedIndexChange: (index) {
27-
context
28-
.read<AppBloc>()
29-
.add(AppNavigationIndexChanged(index: index));
27+
context.read<AppBloc>().add(
28+
AppNavigationIndexChanged(index: index, context: context),
29+
);
3030
},
3131
destinations: const [
3232
NavigationDestination(

lib/router/routes.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ abstract final class Routes {
88
static const account = '/account';
99
static const accountName = 'account';
1010
static const articleDetailsName = 'articleDetails'; // For the sub-route
11+
12+
static String getRouteNameByIndex(int index) {
13+
switch (index) {
14+
case 0:
15+
return headlinesFeedName;
16+
case 1:
17+
return searchName;
18+
case 2:
19+
return accountName;
20+
default:
21+
throw ArgumentError('Invalid index: $index');
22+
}
23+
}
1124
}

memory-bank/.clinerules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
- Explicitly document no-op operations with a comment explaining the reason.
2323
- Document exceptions associated with calling a function in its documentation comments.
2424
- Define descriptive exceptions by implementing `Exception` with descriptive names, rather than throwing generic `Exception`.
25+
- When using `go_router` for navigation, `BuildContext` is required for methods like `context.goNamed()`. Since BLoCs don't have inherent access to `BuildContext`, it needs to be passed through the event triggering the navigation.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: ht_main
22
description: main headlines toolkit mobile app.
3-
version: 0.15.1
3+
version: 0.17.0
44
publish_to: none
55
repository: https://github.com/headlines-toolkit/ht-main
66
environment:

0 commit comments

Comments
 (0)