Skip to content

Commit d953ce3

Browse files
committed
create loading state for custom button
1 parent c8456fb commit d953ce3

File tree

6 files changed

+41
-21
lines changed

6 files changed

+41
-21
lines changed

lib/app/modules/JobDetails/views/widgets/submit_bottom_sheet.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class SubmitBottomSheet extends StatelessWidget {
4242
SizedBox(height: 50.h),
4343
CustomButton(
4444
title: "Back To Home",
45-
onTap: () => Get.close(2),
45+
onTap: () async => Get.close(2),
4646
),
4747
],
4848
);

lib/app/modules/auth/controllers/auth_controller.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,19 @@ class AuthController extends GetxController {
126126
_rxCountry.value = "+${country.dialCode}";
127127
}
128128

129-
void onLoginSubmit() {
129+
Future<void> onLoginSubmit() async {
130130
if (loginFormKey.currentState!.validate()) {
131-
_login();
131+
await _login();
132132
}
133133
}
134134

135-
void onRegisterSubmit() {
135+
Future<void> onRegisterSubmit() async {
136136
if (registerType == RegisterType.CUSTOMER &&
137137
customerFormKey.currentState!.validate()) {
138-
_registerCustomer();
139-
} else if (companyFormKey.currentState!.validate()) {
140-
_registerCompany();
138+
await _registerCustomer();
139+
} else if (registerType == RegisterType.COMPANY &&
140+
companyFormKey.currentState!.validate()) {
141+
await _registerCompany();
141142
}
142143
}
143144

@@ -146,7 +147,7 @@ class AuthController extends GetxController {
146147
result.whenOrNull(success: (data) => Get.offAllNamed(Routes.LOGIN));
147148
}
148149

149-
void _login() async {
150+
Future<void> _login() async {
150151
_rxLoginState.value = await _authRepository.login(
151152
dto: LoginInDto(
152153
emailOrPhone: loginEmailController.text,
@@ -171,7 +172,7 @@ class AuthController extends GetxController {
171172
);
172173
}
173174

174-
void _registerCustomer() async {
175+
Future<void> _registerCustomer() async {
175176
_rxRegisterCustomerState.value = await _authRepository.registerCustomer(
176177
dto: RegisterCustomerDto(
177178
name: customerFullNameController.text,
@@ -198,7 +199,7 @@ class AuthController extends GetxController {
198199
);
199200
}
200201

201-
void _registerCompany() async {
202+
Future<void> _registerCompany() async {
202203
_rxRegisterCompanyState.value = await _authRepository.registerCompany(
203204
dto: RegisterCompanyDto(
204205
name: companyNameController.text,

lib/app/modules/auth/views/login/widgets/body.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Body extends GetView<AuthController> {
2828
btnLabel: AppStrings.loginBtn,
2929
firstTextSpan: AppStrings.youDoNotHaveAnAccountYet,
3030
secondTextSpan: AppStrings.signup,
31-
onTap: () => controller.onLoginSubmit(),
31+
onTap: controller.onLoginSubmit,
3232
onTextTap: controller.onSignUp,
3333
),
3434
],

lib/app/modules/auth/views/register/widgets/body.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Body extends GetView<AuthController> {
3333
btnLabel: AppStrings.signup.toUpperCase(),
3434
firstTextSpan: AppStrings.alreadyHaveAnAccount,
3535
secondTextSpan: AppStrings.signIn,
36-
onTap: controller.onRegisterSubmit,
36+
onTap: controller.onRegisterSubmit,
3737
onTextTap: () => Get.offNamed(Routes.LOGIN),
3838
),
3939
SizedBox(height: 50.h),

lib/app/modules/auth/views/widgets/button_with_text.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ButtonWithText extends StatelessWidget {
1717
final String btnLabel;
1818
final String firstTextSpan;
1919
final String secondTextSpan;
20-
final void Function() onTap;
20+
final Future<void> Function() onTap;
2121
final void Function() onTextTap;
2222

2323
@override

lib/app/widgets/custom_button.dart

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_screenutil/flutter_screenutil.dart';
23
import 'package:get/get.dart';
34

45
class CustomButton extends StatelessWidget {
5-
const CustomButton({
6+
CustomButton({
67
Key? key,
78
required this.title,
89
required this.onTap,
910
}) : super(key: key);
1011
final String title;
11-
final void Function() onTap;
12+
final Future<void> Function() onTap;
13+
14+
final RxBool _isLoading = false.obs;
1215

1316
@override
1417
Widget build(BuildContext context) {
15-
return SizedBox(
16-
width: double.infinity,
17-
child: ElevatedButton(
18-
onPressed: onTap,
19-
style: Get.theme.elevatedButtonTheme.style,
20-
child: FittedBox(child: Text(title)),
18+
return Obx(
19+
() => SizedBox(
20+
width: double.infinity,
21+
child: ElevatedButton(
22+
onPressed: _isLoading.value
23+
? null
24+
: () async {
25+
_isLoading.value = true;
26+
await onTap();
27+
_isLoading.value = false;
28+
},
29+
style: Get.theme.elevatedButtonTheme.style,
30+
child: FittedBox(
31+
child: _isLoading.value
32+
? SizedBox(
33+
height: 20.h,
34+
width: 20.h,
35+
child: const CircularProgressIndicator(color: Colors.white),
36+
)
37+
: Text(title),
38+
),
39+
),
2140
),
2241
);
2342
}

0 commit comments

Comments
 (0)