Skip to content

Commit 201e593

Browse files
committed
changed
1 parent 236ee50 commit 201e593

File tree

3 files changed

+81
-29
lines changed

3 files changed

+81
-29
lines changed

lib/presentation/pages/posts/posts_page.dart

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter/services.dart';
33
import 'package:flutter_bloc/flutter_bloc.dart';
4+
5+
import '../../../bloc/post/post_cubit.dart';
6+
import '../../../utils/extensions/waitable_cubit_ext.dart';
47
import 'widgets/home_bottom_bar.dart';
58
import 'widgets/post_item.dart';
6-
import '../../../bloc/post/post_cubit.dart';
79

810
class PostsPage extends StatelessWidget {
911
@override
@@ -25,38 +27,48 @@ class PostsPage extends StatelessWidget {
2527
),
2628
],
2729
),
28-
BlocBuilder<PostCubit, DataState<List<Post>>>(
29-
builder: (_, state) {
30-
if (state.isInProgress) {
31-
return SliverFillRemaining(
32-
child: Center(
33-
child: CircularProgressIndicator(),
34-
),
35-
);
36-
}
30+
SliverFillRemaining(
31+
child: RefreshIndicator(
32+
onRefresh: () {
33+
context.read<PostCubit>().fetch();
34+
return context
35+
.read<PostCubit>()
36+
.waitFor<DataState<List<Post>>>(
37+
predicate: (state) => state.isSuccess,
38+
);
39+
},
40+
child: BlocBuilder<PostCubit, DataState<List<Post>>>(
41+
builder: (_, state) {
42+
if (state.isInProgress) {
43+
return Center(
44+
child: CircularProgressIndicator(),
45+
);
46+
}
3747

38-
if (state.isSuccess) {
39-
final posts = state.data!..shuffle();
48+
if (state.isSuccess) {
49+
final posts = state.data!..shuffle();
4050

41-
return SliverList(
42-
delegate: SliverChildBuilderDelegate(
43-
(_, i) {
44-
final post = posts[i];
45-
return PostItem(post: post);
46-
},
47-
childCount: state.data!.length,
48-
),
49-
);
50-
}
51+
return ListView.builder(
52+
itemBuilder: (_, i) {
53+
final post = posts[i];
54+
return PostItem(post: post);
55+
},
56+
itemCount: posts.length,
57+
);
58+
}
5159

52-
if (state.isFailure) {
53-
return SliverFillRemaining(
54-
child: Text('Error occured!'),
55-
);
56-
}
60+
if (state.isFailure) {
61+
return Text('Error occurred!');
62+
}
5763

58-
return SliverToBoxAdapter(child: SizedBox());
59-
},
64+
return Expanded(
65+
child: Container(
66+
color: Colors.white,
67+
),
68+
);
69+
},
70+
),
71+
),
6072
),
6173
],
6274
),

lib/utils/extensions/.gitkeep

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter_bloc/flutter_bloc.dart';
4+
5+
typedef Predicate<T> = bool Function(T);
6+
7+
const _defaultTimeout = Duration(seconds: 20);
8+
9+
extension WaitableCubitExt<State> on Cubit<State> {
10+
Future<T> waitFor<T extends State>({
11+
Predicate<T>? predicate,
12+
Duration timeout = _defaultTimeout,
13+
}) {
14+
final completer = Completer<T>();
15+
late StreamSubscription subscription;
16+
17+
subscription = stream.listen(
18+
(state) {
19+
if (state is T) {
20+
if (predicate != null && predicate(state)) {
21+
subscription.cancel();
22+
completer.complete(state);
23+
}
24+
}
25+
},
26+
);
27+
28+
Future.delayed(timeout, () {
29+
if (!completer.isCompleted) {
30+
subscription.cancel();
31+
completer.completeError(TimeoutException(
32+
'Cubit have not emit required state.',
33+
timeout,
34+
));
35+
}
36+
});
37+
38+
return completer.future;
39+
}
40+
}

0 commit comments

Comments
 (0)