-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlist_app.dart
52 lines (48 loc) · 1.64 KB
/
list_app.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
import 'package:example/src/common/error_message.dart';
import 'package:example/src/common/loading_indicator.dart';
import 'package:example/src/post/model/post.dart';
import 'package:example/src/post/model/post_repository.dart';
import 'package:example/src/post/ui/posts_list.dart';
import 'package:example/src/post/ui/posts_list_empty.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_bloc_patterns/base_list.dart';
import 'package:flutter_bloc_patterns/view.dart';
void main() => runApp(ListSampleApp());
typedef PostsBloc = ListBloc<Post>;
class ListSampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'List Sample App',
theme: ThemeData(primarySwatch: Colors.green),
home: BlocProvider(
create: (_) => ListBloc<Post>(
PostListRepository(),
)..loadItems(),
child: PostsPage(),
),
);
}
}
class PostsPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Posts')),
body: ViewStateBuilder<List<Post>, PostsBloc>(
loading: (context) => const LoadingIndicator(),
data: (context, posts) => PostsList(
posts,
onRefresh: context.read<PostsBloc>().refreshItems,
),
refreshing: (context, posts) => PostsList(
posts,
onRefresh: context.read<PostsBloc>().refreshItems,
),
empty: (context) => const PostsListEmpty(),
error: (context, error) => ErrorMessage(error: error),
),
);
}
}