-
Notifications
You must be signed in to change notification settings - Fork 0
/
posts_screen.dart
72 lines (66 loc) · 2.61 KB
/
posts_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import 'package:common_presentation/mixins/widget_alert_mixin.dart';
import 'package:common_presentation/widgets/connection_status_view.dart';
import 'package:common_presentation/widgets/screen_state/screen_state_bloc_builder.dart';
import 'package:common_presentation/widgets/scroll_to_end_listener.dart';
import 'package:features_reddit_posts_presentation/l10n/app_localizations.g.dart';
import 'package:features_reddit_posts_presentation/src/data/post_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:widgets_modifiers/style/styling_widgets_modifiers.dart';
import 'post_tile.dart';
import 'posts_cubit.dart';
/// A PostsView Page.
class PostsScreen extends StatelessWidget with WidgetAlertMixin {
const PostsScreen({super.key});
@override
Widget build(BuildContext context) => BlocProvider(
create: (_) => PostsCubit()..refresh(),
child: Scaffold(
appBar: AppBar(title: Text(AppLocalizations.of(context)!.posts_remote_first)),
body: _buildBody(context),
),
);
Widget _buildBody(BuildContext context) {
final cubit = context.read<PostsCubit>();
onBuild(context, cubit);
return ScreenStateBlocBuilder<PostsCubit, PostsPageState>(
builder: (context, data) => ConnectionStatusView.withChild(
Center(child: _list(context, data.data)),
onBackOnline: cubit.refresh,
),
);
}
Widget _list(BuildContext context, Iterable<PostItemState> posts) {
final postList = posts.toList();
final cubit = context.read<PostsCubit>();
return RefreshIndicator(
onRefresh: cubit.refresh,
child: ScrollToEndListener(
onScrolledToEnd: cubit.loadNextPage,
child: (controller) => CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(),
controller: controller,
slivers: [
SliverPadding(
padding: const EdgeInsets.all(8),
sliver: SliverList.builder(
itemCount: postList.length,
itemBuilder: (context, index) {
return PostTile(
postList[index],
() => cubit.onPostTap(postList[index]),
);
},
),
),
SliverToBoxAdapter(child: _pageLoadingIndicator(cubit.stateData?.paginationState?.isLoadingPage ?? false)),
],
),
),
);
}
Widget _pageLoadingIndicator(bool isLoadingPage) => Visibility(
visible: isLoadingPage,
child: const Center(child: CircularProgressIndicator()).padding(all: 8),
);
}