Skip to content

Commit 985e2bd

Browse files
committed
fix(account): move InAppNotificationCenterBlocProvider to router
- Move BlocProvider from InAppNotificationCenterPage to router - Ensure InAppNotificationCenterBloc is available in BuildContext when InAppNotificationCenterPage's initState runs - Refactor InAppNotificationCenterPage widget build
1 parent 0cf7337 commit 985e2bd

File tree

2 files changed

+64
-61
lines changed

2 files changed

+64
-61
lines changed

lib/account/view/in_app_notification_center_page.dart

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,8 @@ class _InAppNotificationCenterPageState
5555
Widget build(BuildContext context) {
5656
final l10n = AppLocalizationsX(context).l10n;
5757

58-
return BlocProvider(
59-
create: (context) => InAppNotificationCenterBloc(
60-
inAppNotificationRepository: context
61-
.read<DataRepository<InAppNotification>>(),
62-
appBloc: context.read<AppBloc>(),
63-
logger: context.read<Logger>(),
64-
)..add(const InAppNotificationCenterSubscriptionRequested()),
65-
child: Scaffold(
66-
appBar: AppBar(
58+
return Scaffold(
59+
appBar: AppBar(
6760
title: Text(l10n.notificationCenterPageTitle),
6861
actions: [
6962
BlocBuilder<
@@ -93,59 +86,57 @@ class _InAppNotificationCenterPageState
9386
],
9487
),
9588
),
96-
body:
97-
BlocConsumer<
98-
InAppNotificationCenterBloc,
99-
InAppNotificationCenterState
100-
>(
101-
listener: (context, state) {
102-
if (state.status == InAppNotificationCenterStatus.failure &&
103-
state.error != null) {
104-
ScaffoldMessenger.of(context)
105-
..hideCurrentSnackBar()
106-
..showSnackBar(
107-
SnackBar(
108-
content: Text(state.error!.message),
109-
backgroundColor: Theme.of(context).colorScheme.error,
110-
),
111-
);
112-
}
113-
},
114-
builder: (context, state) {
115-
if (state.status == InAppNotificationCenterStatus.loading) {
116-
return LoadingStateWidget(
117-
icon: Icons.notifications_none_outlined,
118-
headline: l10n.notificationCenterLoadingHeadline,
119-
subheadline: l10n.notificationCenterLoadingSubheadline,
120-
);
121-
}
122-
123-
if (state.status == InAppNotificationCenterStatus.failure) {
124-
return FailureStateWidget(
125-
exception:
126-
state.error ??
127-
OperationFailedException(
128-
l10n.notificationCenterFailureHeadline,
129-
),
130-
onRetry: () {
131-
context.read<InAppNotificationCenterBloc>().add(
132-
const InAppNotificationCenterSubscriptionRequested(),
133-
);
134-
},
135-
);
136-
}
137-
138-
return TabBarView(
139-
controller: _tabController,
140-
children: [
141-
_NotificationList(
142-
notifications: state.breakingNewsNotifications,
143-
),
144-
_NotificationList(notifications: state.digestNotifications),
145-
],
89+
body: BlocConsumer<
90+
InAppNotificationCenterBloc,
91+
InAppNotificationCenterState
92+
>(
93+
listener: (context, state) {
94+
if (state.status == InAppNotificationCenterStatus.failure &&
95+
state.error != null) {
96+
ScaffoldMessenger.of(context)
97+
..hideCurrentSnackBar()
98+
..showSnackBar(
99+
SnackBar(
100+
content: Text(state.error!.message),
101+
backgroundColor: Theme.of(context).colorScheme.error,
102+
),
103+
);
104+
}
105+
},
106+
builder: (context, state) {
107+
if (state.status == InAppNotificationCenterStatus.loading) {
108+
return LoadingStateWidget(
109+
icon: Icons.notifications_none_outlined,
110+
headline: l10n.notificationCenterLoadingHeadline,
111+
subheadline: l10n.notificationCenterLoadingSubheadline,
112+
);
113+
}
114+
115+
if (state.status == InAppNotificationCenterStatus.failure) {
116+
return FailureStateWidget(
117+
exception:
118+
state.error ??
119+
OperationFailedException(
120+
l10n.notificationCenterFailureHeadline,
121+
),
122+
onRetry: () {
123+
context.read<InAppNotificationCenterBloc>().add(
124+
const InAppNotificationCenterSubscriptionRequested(),
146125
);
147126
},
148-
),
127+
);
128+
}
129+
130+
return TabBarView(
131+
controller: _tabController,
132+
children: [
133+
_NotificationList(
134+
notifications: state.breakingNewsNotifications,
135+
),
136+
_NotificationList(notifications: state.digestNotifications),
137+
],
138+
);
139+
},
149140
),
150141
);
151142
}

lib/router/router.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:core/core.dart' hide AppStatus;
33
import 'package:data_repository/data_repository.dart';
44
import 'package:flutter/material.dart';
55
import 'package:flutter_bloc/flutter_bloc.dart';
6+
import 'package:flutter_news_app_mobile_client_full_source_code/account/bloc/in_app_notification_center_bloc.dart';
67
import 'package:flutter_news_app_mobile_client_full_source_code/account/view/account_page.dart';
78
import 'package:flutter_news_app_mobile_client_full_source_code/account/view/followed_contents/countries/add_country_to_follow_page.dart';
89
import 'package:flutter_news_app_mobile_client_full_source_code/account/view/followed_contents/countries/followed_countries_list_page.dart';
@@ -235,7 +236,18 @@ GoRouter createRouter({
235236
GoRoute(
236237
path: Routes.notificationsCenter,
237238
name: Routes.notificationsCenterName,
238-
builder: (context, state) => const InAppNotificationCenterPage(),
239+
builder: (context, state) {
240+
// Provide the InAppNotificationCenterBloc here so it's available
241+
// in the BuildContext when InAppNotificationCenterPage's initState runs.
242+
return BlocProvider(
243+
create: (context) => InAppNotificationCenterBloc(
244+
inAppNotificationRepository: context.read<DataRepository<InAppNotification>>(),
245+
appBloc: context.read<AppBloc>(),
246+
logger: context.read<Logger>(),
247+
)..add(const InAppNotificationCenterSubscriptionRequested()),
248+
child: const InAppNotificationCenterPage(),
249+
);
250+
},
239251
),
240252
// The settings section within the account modal. It uses a
241253
// ShellRoute to provide a SettingsBloc to all its children.

0 commit comments

Comments
 (0)