-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloc_view.dart
73 lines (70 loc) · 2.71 KB
/
bloc_view.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
73
import 'package:enhanced_paginated_view/enhanced_paginated_view.dart';
import 'package:example/bloc_example/bloc/paginated_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class BlocView extends StatefulWidget {
const BlocView({super.key});
@override
State<BlocView> createState() => _BlocViewState();
}
class _BlocViewState extends State<BlocView> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Bloc Example')),
body: BlocProvider(
create: (context) => PaginatedBloc()..add(const FetchDataEvent()),
child: BlocBuilder<PaginatedBloc, PaginatedState>(
builder: (context, state) {
if (state.status == PaginatedStatus.initial) {
return const Center(child: CircularProgressIndicator());
}
return SafeArea(
child: EnhancedPaginatedView<int>(
listOfData: state.listOfData,
itemsPerPage: 10,
showLoading: state.status == PaginatedStatus.loading,
showError: state.status == PaginatedStatus.error,
isMaxReached: state.isMaxReached,
onLoadMore: (page) {
context.read<PaginatedBloc>().add(FetchDataEvent(page: page));
},
errorWidget: (page) => Column(
children: [
Center(child: Text(' ${state.error}')),
ElevatedButton(
onPressed: () {
context
.read<PaginatedBloc>()
.add(FetchDataEvent(page: page));
},
child: const Text('Retry'),
),
],
),
builder: (physics, items, shrinkWrap, chatMode) {
return ListView.separated(
// here we must pass the physics, items and shrinkWrap
// that came from the builder function
physics: physics,
shrinkWrap: shrinkWrap,
itemCount: items.length,
separatorBuilder: (BuildContext context, int index) {
return const Divider(height: 16);
},
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text('Item ${index + 1}'),
subtitle: Text('Item ${items[index]}'),
);
},
);
},
),
);
},
),
),
);
}
}