Skip to content

Commit 3eba994

Browse files
committed
refactor(app): further improved navigation and localization
1 parent cea552c commit 3eba994

File tree

13 files changed

+99
-77
lines changed

13 files changed

+99
-77
lines changed

lib/app/bloc/app_bloc.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'package:bloc/bloc.dart';
2+
import 'package:meta/meta.dart';
3+
4+
part 'app_event.dart';
5+
part 'app_state.dart';
6+
7+
class AppBloc extends Bloc<AppEvent, AppState> {
8+
AppBloc() : super(const AppState()) {
9+
on<AppNavigationIndexChanged>((event, emit) {
10+
emit(state.copyWith(selectedIndex: event.index));
11+
});
12+
}
13+
}

lib/app/bloc/app_event.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
part of 'app_bloc.dart';
2+
3+
@immutable
4+
sealed class AppEvent {}
5+
6+
final class AppNavigationIndexChanged extends AppEvent {
7+
AppNavigationIndexChanged({required this.index});
8+
9+
final int index;
10+
}

lib/app/bloc/app_state.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
part of 'app_bloc.dart';
2+
3+
@immutable
4+
class AppState {
5+
const AppState({this.selectedIndex = 0});
6+
final int selectedIndex;
7+
8+
AppState copyWith({int? selectedIndex}) {
9+
return AppState(
10+
selectedIndex: selectedIndex ?? this.selectedIndex,
11+
);
12+
}
13+
}

lib/app/view/app.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_bloc/flutter_bloc.dart';
3+
import 'package:ht_main/l10n/l10n.dart';
34
import 'package:ht_main/router/router.dart';
45
import 'package:ht_main/theme/cubit/theme_cubit.dart';
56

@@ -15,6 +16,8 @@ class App extends StatelessWidget {
1516
return MaterialApp.router(
1617
theme: state.themeData,
1718
routerConfig: appRouter,
19+
localizationsDelegates: AppLocalizations.localizationsDelegates,
20+
supportedLocales: AppLocalizations.supportedLocales,
1821
);
1922
},
2023
),

lib/app/view/app_scaffold.dart

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_adaptive_scaffold/flutter_adaptive_scaffold.dart';
3-
import 'package:go_router/go_router.dart';
4-
import 'package:ht_main/router/routes.dart';
3+
import 'package:flutter_bloc/flutter_bloc.dart';
4+
import 'package:ht_main/app/bloc/app_bloc.dart';
55

66
class AppScaffold extends StatelessWidget {
77
const AppScaffold({required this.child, super.key});
@@ -10,33 +10,44 @@ class AppScaffold extends StatelessWidget {
1010

1111
@override
1212
Widget build(BuildContext context) {
13-
return AdaptiveScaffold(
14-
useDrawer: false,
15-
destinations: const [
16-
NavigationDestination(
17-
icon: Icon(Icons.view_headline),
18-
label: 'Headlines',
19-
selectedIcon: Icon(Icons.view_headline),
20-
),
21-
NavigationDestination(
22-
icon: Icon(Icons.search),
23-
label: 'Search',
24-
selectedIcon: Icon(Icons.search),
25-
),
26-
],
27-
smallBody: (_) => child,
28-
body: (_) => child,
29-
largeBody: (_) => child,
30-
smallSecondaryBody: AdaptiveScaffold.emptyBuilder,
31-
secondaryBody: AdaptiveScaffold.emptyBuilder,
32-
largeSecondaryBody: AdaptiveScaffold.emptyBuilder,
33-
onSelectedIndexChange: (index) {
34-
if (index == 0) {
35-
context.go(Routes.headlines);
36-
} else if (index == 1) {
37-
context.go(Routes.search);
38-
}
39-
},
13+
return BlocProvider(
14+
create: (context) => AppBloc(),
15+
child: BlocBuilder<AppBloc, AppState>(
16+
builder: (context, state) {
17+
return AdaptiveScaffold(
18+
useDrawer: false,
19+
smallBody: (_) => child,
20+
body: (_) => child,
21+
largeBody: (_) => child,
22+
smallSecondaryBody: AdaptiveScaffold.emptyBuilder,
23+
secondaryBody: AdaptiveScaffold.emptyBuilder,
24+
largeSecondaryBody: AdaptiveScaffold.emptyBuilder,
25+
selectedIndex: state.selectedIndex,
26+
onSelectedIndexChange: (index) {
27+
context
28+
.read<AppBloc>()
29+
.add(AppNavigationIndexChanged(index: index));
30+
},
31+
destinations: const [
32+
NavigationDestination(
33+
icon: Icon(Icons.view_headline_outlined),
34+
selectedIcon: Icon(Icons.view_headline),
35+
label: 'Headlines',
36+
),
37+
NavigationDestination(
38+
icon: Icon(Icons.search_outlined),
39+
selectedIcon: Icon(Icons.search),
40+
label: 'Search',
41+
),
42+
NavigationDestination(
43+
icon: Icon(Icons.account_circle_outlined),
44+
selectedIcon: Icon(Icons.account_circle),
45+
label: 'Account',
46+
),
47+
],
48+
);
49+
},
50+
),
4051
);
4152
}
4253
}

lib/router/router.dart

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import 'package:flutter/material.dart';
22
import 'package:go_router/go_router.dart';
33
import 'package:ht_main/app/view/app_scaffold.dart';
4+
import 'package:ht_main/headlines/view/headlines_page.dart';
45
import 'package:ht_main/router/routes.dart';
5-
import 'package:ht_main/search/search.dart';
66

77
final appRouter = GoRouter(
8+
initialLocation: Routes.headlines,
89
routes: [
910
ShellRoute(
1011
builder: (context, state, child) {
@@ -14,13 +15,27 @@ final appRouter = GoRouter(
1415
GoRoute(
1516
path: Routes.headlines,
1617
builder: (BuildContext context, GoRouterState state) {
17-
return const Placeholder(); // Use Placeholder for Headlines
18+
return const HeadlinesPage();
1819
},
1920
),
2021
GoRoute(
2122
path: Routes.search,
2223
builder: (BuildContext context, GoRouterState state) {
23-
return const SearchPage();
24+
return const Placeholder(
25+
child: Center(
26+
child: Text('SEARCH PAGE'),
27+
),
28+
);
29+
},
30+
),
31+
GoRoute(
32+
path: Routes.account,
33+
builder: (BuildContext context, GoRouterState state) {
34+
return const Placeholder(
35+
child: Center(
36+
child: Text('ACCOUNT PAGE'),
37+
),
38+
);
2439
},
2540
),
2641
],

lib/router/routes.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ abstract final class Routes {
22
static const home = '/';
33
static const headlines = '/headlines';
44
static const search = '/search';
5+
static const account = '/account';
56
}

lib/search/bloc/search_bloc.dart

Lines changed: 0 additions & 13 deletions
This file was deleted.

lib/search/bloc/search_event.dart

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/search/bloc/search_state.dart

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)