From fa3238a126407f723e5a60769bc4e82709caa13b Mon Sep 17 00:00:00 2001 From: Andrea Bizzotto Date: Thu, 4 Aug 2022 11:08:09 +0100 Subject: [PATCH] Prefill the form data with Consumer and initState --- .../leave_review_screen.dart | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_screen.dart b/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_screen.dart index c1921705..bb34fe19 100644 --- a/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_screen.dart +++ b/ecommerce_app/lib/src/features/reviews/presentation/leave_review_screen/leave_review_screen.dart @@ -1,5 +1,7 @@ +import 'package:ecommerce_app/src/common_widgets/async_value_widget.dart'; import 'package:ecommerce_app/src/constants/breakpoints.dart'; import 'package:ecommerce_app/src/features/products/domain/product.dart'; +import 'package:ecommerce_app/src/features/reviews/application/reviews_service.dart'; import 'package:ecommerce_app/src/features/reviews/presentation/leave_review_screen/leave_review_controller.dart'; import 'package:ecommerce_app/src/features/reviews/presentation/product_reviews/product_rating_bar.dart'; import 'package:ecommerce_app/src/localization/string_hardcoded.dart'; @@ -18,8 +20,6 @@ class LeaveReviewScreen extends StatelessWidget { @override Widget build(BuildContext context) { - // TODO: Read from data source - const review = null; return Scaffold( appBar: AppBar( title: Text('Leave a review'.hardcoded), @@ -27,7 +27,16 @@ class LeaveReviewScreen extends StatelessWidget { body: ResponsiveCenter( maxContentWidth: Breakpoint.tablet, padding: const EdgeInsets.all(Sizes.p16), - child: LeaveReviewForm(productId: productId, review: review), + child: Consumer( + builder: (context, ref, child) { + final reviewValue = ref.watch(userReviewFutureProvider(productId)); + return AsyncValueWidget( + value: reviewValue, + data: (review) => + LeaveReviewForm(productId: productId, review: review), + ); + }, + ), ), ); } @@ -53,7 +62,11 @@ class _LeaveReviewFormState extends ConsumerState { @override void initState() { super.initState(); - // TODO: Initialize state + final review = widget.review; + if (review != null) { + _controller.text = review.comment; + _rating = review.rating; + } } @override