Skip to content

Commit

Permalink
fix component
Browse files Browse the repository at this point in the history
  • Loading branch information
crosbydoo committed Feb 16, 2024
1 parent c12051e commit 553176e
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 57 deletions.
14 changes: 12 additions & 2 deletions lib/resources/widgets/common_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,44 @@ class CommonButton extends StatelessWidget {
final String text;
final VoidCallback onPressed;
final bool outlined;
final bool disabled; // New property for button disabled state

const CommonButton({
super.key,
Key? key,
required this.text,
required this.onPressed,
this.outlined = false,
this.disabled = false, // Default value is false (button is enabled)
});

factory CommonButton.normalButton({
Key? key,
required String text,
required VoidCallback onPressed,
bool disabled = false, // New parameter with default value
}) {
return CommonButton(
key: key,
text: text,
onPressed: onPressed,
disabled:
disabled, // Assign the disabled parameter to the disabled property
);
}

factory CommonButton.outlinedButton({
Key? key,
required String text,
required VoidCallback onPressed,
bool disabled = false, // New parameter with default value
}) {
return CommonButton(
key: key,
text: text,
onPressed: onPressed,
outlined: true,
disabled:
disabled, // Assign the disabled parameter to the disabled property
);
}

Expand All @@ -43,7 +51,9 @@ class CommonButton extends StatelessWidget {
return SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: onPressed,
onPressed: disabled
? null
: onPressed, // Disable the button if disabled is true
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
Expand Down
3 changes: 3 additions & 0 deletions lib/resources/widgets/common_textformfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ class CommonTextForm extends StatelessWidget {
this.controller,
this.suffixIcon,
this.onpress,
this.validator,
});

final String hint;
final bool obscured;
final IconData? icon;
final IconData? suffixIcon;
final VoidCallback? onpress;
final String Function(String?)? validator;
final TextEditingController? controller;

@override
Expand All @@ -36,6 +38,7 @@ class CommonTextForm extends StatelessWidget {
borderRadius: BorderRadius.circular(16),
),
),
validator: validator,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ class RestaurantBloc extends Bloc<RestaurantEvent, RestaurantState> {
AddReviewEvent event,
Emitter<RestaurantState> emit,
) async {
var request =
AddReviewRequest(id: event.id, name: event.name, review: event.review);
var request = AddReviewRequest(
id: event.id,
name: event.name,
review: event.review,
);

var resultSearch = await reviewUsecase.execute(request);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,64 @@
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';

import 'package:restaurant_app/resources/styles/typograph.dart';
import 'package:restaurant_app/resources/widgets/common_button.dart';
import 'package:restaurant_app/src/restaurant/presentation/bloc/restaurant/restaurant_bloc.dart';
import 'package:restaurant_app/resources/widgets/common_snackbar.dart';
import 'package:restaurant_app/resources/widgets/common_textformfield.dart';
import 'package:restaurant_app/src/restaurant/presentation/bloc/restaurant/restaurant_bloc.dart';
import 'package:restaurant_app/resources/widgets/common_button.dart';
import 'package:restaurant_app/resources/styles/typograph.dart';
import 'package:gap/gap.dart';

class FormReviewComponent extends StatefulWidget {
class FormReviewComponent extends HookWidget {
const FormReviewComponent({super.key, required this.id});

final String id;

@override
State<FormReviewComponent> createState() => _FormReviewComponentState();
}

class _FormReviewComponentState extends State<FormReviewComponent> {
final TextEditingController nameInput = TextEditingController();
final TextEditingController reviewInput = TextEditingController();
Widget build(BuildContext context) {
final nameInput = useTextEditingController();
final reviewInput = useTextEditingController();
final formKey = useMemoized(() => GlobalKey<FormState>());

final _formKey = GlobalKey<FormState>();
late RestaurantBloc bloc;
bool isSubmitDisabled = nameInput.text.isEmpty || reviewInput.text.isEmpty;

@override
void initState() {
bloc = RestaurantBloc(
detailUsecase: GetIt.instance(),
usecase: GetIt.instance(),
reviewUsecase: GetIt.instance(),
);
super.initState();
}
useEffect(() {
final subscription =
context.read<RestaurantBloc>().stream.listen((state) {
if (state is ReviewSuccessState) {
CommonSnackbar.showSuccessSnackbar(
context: context,
title: 'Success',
message: 'Successfully adding review',
);
context.pop(); // Kembali ke halaman sebelumnya
}
});
// Membersihkan langganan saat komponen di-unmount
return subscription.cancel;
}, []);

@override
Widget build(BuildContext context) {
return Container(
height: MediaQuery.sizeOf(context).height * 0.45,
height: MediaQuery.of(context).size.height * 0.45,
width: double.infinity,
padding: EdgeInsets.symmetric(horizontal: 24, vertical: 20),
child: Form(
key: _formKey,
key: formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Add Your Review',
style: StyleTypograph.heading3.bold,
),
Gap(20),
const Gap(20),
CommonTextForm(
controller: nameInput,
obscured: false,
hint: 'Your name',
),
Gap(12),
const Gap(12),
CommonTextForm(
controller: reviewInput,
obscured: false,
Expand All @@ -65,21 +67,16 @@ class _FormReviewComponentState extends State<FormReviewComponent> {
Spacer(),
Container(
child: CommonButton.normalButton(
disabled: isSubmitDisabled,
onPressed: () {
if (_formKey.currentState!.validate() == true) {
bloc.add(
AddReviewEvent(
id: widget.id,
name: nameInput.text,
review: reviewInput.text,
),
);
CommonSnackbar.showSuccessSnackbar(
context: context,
title: 'Success',
message: 'Successfully adding review',
);
context.pop();
if (formKey.currentState!.validate()) {
context.read<RestaurantBloc>().add(
AddReviewEvent(
id: id,
name: nameInput.text,
review: reviewInput.text,
),
);
}
},
text: 'Submit',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class _AllRestaurantScreenState extends State<AllRestaurantScreen> {
bloc: searchBloc,
listener: (context, state) {},
builder: (context, state) {
print('check $state');
return Column(
children: [
Padding(
Expand Down
23 changes: 15 additions & 8 deletions lib/src/restaurant/presentation/widgets/review_bubble_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ class ReviewBubbleWidget extends StatelessWidget {
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'By: $userReview',
overflow: TextOverflow.ellipsis,
style: StyleTypograph.body1.medium,
Expanded(
child: Text(
'By: $userReview',
overflow: TextOverflow.ellipsis,
style: StyleTypograph.body1.medium,
),
),
Text(
dateReview,
overflow: TextOverflow.ellipsis,
style: StyleTypograph.body1.regular,
Expanded(
child: Align(
alignment: Alignment.centerRight,
child: Text(
dateReview,
overflow: TextOverflow.ellipsis,
style: StyleTypograph.body1.regular,
),
),
),
],
),
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.1"
flutter_hooks:
dependency: "direct main"
description:
name: flutter_hooks
sha256: cde36b12f7188c85286fba9b38cc5a902e7279f36dd676967106c041dc9dde70
url: "https://pub.dev"
source: hosted
version: "0.20.5"
flutter_launcher_icons:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies:
sdk: flutter
flutter_bloc: ^8.1.3
flutter_flavorizr: ^2.2.1
flutter_hooks: ^0.20.5
flutter_launcher_icons: "^0.13.1"
flutter_rating_bar: ^4.0.1
flutter_svg: ^2.0.9
Expand Down

0 comments on commit 553176e

Please sign in to comment.