mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
implement example
This commit is contained in:
16
examples/postly/lib/data/model/post_model.dart
Normal file
16
examples/postly/lib/data/model/post_model.dart
Normal file
@@ -0,0 +1,16 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
part 'post_model.freezed.dart';
|
||||
part 'post_model.g.dart';
|
||||
|
||||
@freezed
|
||||
class PostModel with _$PostModel {
|
||||
const factory PostModel({
|
||||
required int id,
|
||||
required String title,
|
||||
required String body,
|
||||
}) = _PostModel;
|
||||
|
||||
factory PostModel.fromJson(Map<String, dynamic> json) =>
|
||||
_$PostModelFromJson(json);
|
||||
}
|
||||
17
examples/postly/lib/data/network/json_placeholder_api.dart
Normal file
17
examples/postly/lib/data/network/json_placeholder_api.dart
Normal file
@@ -0,0 +1,17 @@
|
||||
import 'package:retrofit/retrofit.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../model/post_model.dart';
|
||||
|
||||
part 'json_placeholder_api.g.dart';
|
||||
|
||||
@RestApi(baseUrl: 'https://jsonplaceholder.typicode.com/')
|
||||
abstract class JsonPlaceholderApi {
|
||||
factory JsonPlaceholderApi(Dio dio, {String baseUrl}) = _JsonPlaceholderApi;
|
||||
|
||||
@GET('/posts')
|
||||
Future<List<PostModel>> getPosts();
|
||||
|
||||
@GET('/posts/{id}')
|
||||
Future<PostModel> getPost(@Path('id') int id);
|
||||
}
|
||||
32
examples/postly/lib/data/post_repository_impl.dart
Normal file
32
examples/postly/lib/data/post_repository_impl.dart
Normal file
@@ -0,0 +1,32 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../domain/entity/post.dart';
|
||||
import '../domain/repository/post_repository.dart';
|
||||
import 'network/json_placeholder_api.dart';
|
||||
|
||||
class PostRepositoryImpl implements PostRepository {
|
||||
final JsonPlaceholderApi api;
|
||||
|
||||
PostRepositoryImpl(this.api);
|
||||
|
||||
@override
|
||||
Future<Either<Exception, List<Post>>> getPosts() async {
|
||||
try {
|
||||
final posts = await api.getPosts();
|
||||
return Right(posts
|
||||
.map((e) => Post(id: e.id, title: e.title, body: e.body))
|
||||
.toList());
|
||||
} catch (e) {
|
||||
return Left(Exception(e.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Either<Exception, Post>> getPost(int id) async {
|
||||
try {
|
||||
final post = await api.getPost(id);
|
||||
return Right(Post(id: post.id, title: post.title, body: post.body));
|
||||
} catch (e) {
|
||||
return Left(Exception(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
12
examples/postly/lib/domain/entity/post.dart
Normal file
12
examples/postly/lib/domain/entity/post.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
part 'post.freezed.dart';
|
||||
|
||||
@freezed
|
||||
class Post with _$Post {
|
||||
const factory Post({
|
||||
required int id,
|
||||
required String title,
|
||||
required String body,
|
||||
}) = _Post;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
import '../entity/post.dart';
|
||||
|
||||
abstract class PostRepository {
|
||||
Future<Either<Exception, List<Post>>> getPosts();
|
||||
Future<Either<Exception, Post>> getPost(int id);
|
||||
}
|
||||
35
examples/postly/lib/main.dart
Normal file
35
examples/postly/lib/main.dart
Normal file
@@ -0,0 +1,35 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'data/network/json_placeholder_api.dart';
|
||||
import 'data/post_repository_impl.dart';
|
||||
import 'domain/repository/post_repository.dart';
|
||||
import 'presentation/bloc/post_bloc.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'router/app_router.dart';
|
||||
|
||||
void main() {
|
||||
final dio = Dio();
|
||||
final api = JsonPlaceholderApi(dio);
|
||||
final repository = PostRepositoryImpl(api);
|
||||
|
||||
runApp(MyApp(repository: repository));
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
final PostRepository repository;
|
||||
final _appRouter = AppRouter();
|
||||
|
||||
MyApp({super.key, required this.repository});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocProvider(
|
||||
create: (_) => PostBloc(repository),
|
||||
child: MaterialApp.router(
|
||||
routeInformationParser: _appRouter.defaultRouteParser(),
|
||||
routerDelegate: _appRouter.delegate(),
|
||||
theme: ThemeData.light(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
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')),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
12
examples/postly/lib/router/app_router.dart
Normal file
12
examples/postly/lib/router/app_router.dart
Normal file
@@ -0,0 +1,12 @@
|
||||
import 'package:auto_route/auto_route.dart';
|
||||
|
||||
import 'app_router.gr.dart';
|
||||
|
||||
@AutoRouterConfig()
|
||||
class AppRouter extends $AppRouter {
|
||||
@override
|
||||
List<AutoRoute> get routes => [
|
||||
AutoRoute(page: PostsRoute.page, initial: true),
|
||||
AutoRoute(page: PostDetailsRoute.page),
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user