mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
implement example
This commit is contained in:
38
examples/postly/lib/presentation/bloc/post_bloc.dart
Normal file
38
examples/postly/lib/presentation/bloc/post_bloc.dart
Normal file
@@ -0,0 +1,38 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import '../../domain/entity/post.dart';
|
||||
import '../../domain/repository/post_repository.dart';
|
||||
|
||||
part 'post_bloc.freezed.dart';
|
||||
|
||||
@freezed
|
||||
class PostEvent with _$PostEvent {
|
||||
const factory PostEvent.fetchAll() = _FetchAll;
|
||||
}
|
||||
|
||||
@freezed
|
||||
class PostState with _$PostState {
|
||||
const factory PostState.initial() = _Initial;
|
||||
const factory PostState.loading() = _Loading;
|
||||
const factory PostState.loaded(List<Post> posts) = _Loaded;
|
||||
const factory PostState.failure(String message) = _Failure;
|
||||
}
|
||||
|
||||
class PostBloc extends Bloc<PostEvent, PostState> {
|
||||
final PostRepository repository;
|
||||
|
||||
PostBloc(this.repository) : super(const PostState.initial()) {
|
||||
on<PostEvent>((event, emit) async {
|
||||
await event.map(
|
||||
fetchAll: (e) async {
|
||||
emit(const PostState.loading());
|
||||
final result = await repository.getPosts();
|
||||
result.fold(
|
||||
(l) => emit(PostState.failure(l.toString())),
|
||||
(r) => emit(PostState.loaded(r)),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../domain/entity/post.dart';
|
||||
|
||||
@RoutePage()
|
||||
class PostDetailsPage extends StatelessWidget {
|
||||
final Post post;
|
||||
|
||||
const PostDetailsPage({super.key, required this.post});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text('Post #${post.id}')),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Text(post.body),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
42
examples/postly/lib/presentation/pages/posts_page.dart
Normal file
42
examples/postly/lib/presentation/pages/posts_page.dart
Normal file
@@ -0,0 +1,42 @@
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
import '../../router/app_router.gr.dart';
|
||||
import '../bloc/post_bloc.dart';
|
||||
|
||||
@RoutePage()
|
||||
class PostsPage extends StatelessWidget {
|
||||
const PostsPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (context) =>
|
||||
context.read<PostBloc>()..add(const PostEvent.fetchAll()),
|
||||
child: Scaffold(
|
||||
appBar: AppBar(title: const Text('Posts')),
|
||||
body: BlocBuilder<PostBloc, PostState>(
|
||||
builder: (context, state) {
|
||||
return state.when(
|
||||
initial: () => const SizedBox.shrink(),
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
loaded: (posts) => ListView.builder(
|
||||
itemCount: posts.length,
|
||||
itemBuilder: (ctx, i) => ListTile(
|
||||
title: Text(posts[i].title),
|
||||
subtitle: Text(posts[i].body),
|
||||
onTap: () {
|
||||
AutoRouter.of(context)
|
||||
.push(PostDetailsRoute(post: posts[i]));
|
||||
},
|
||||
),
|
||||
),
|
||||
failure: (msg) => Center(child: Text('Error: $msg')),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user