Commit d5a3c225 authored by impfundev's avatar impfundev

fix: refactoring custom button with loading

parent 36d06822
...@@ -226,11 +226,11 @@ class AuthProvider with ChangeNotifier { ...@@ -226,11 +226,11 @@ class AuthProvider with ChangeNotifier {
String? lastName, String? lastName,
bool isStaff, bool isStaff,
) async { ) async {
setLoading(true);
final token = await getAccessToken(); final token = await getAccessToken();
if (token != null) { if (token != null) {
try { try {
setLoading(true);
final data = { final data = {
"username": username, "username": username,
"email": email, "email": email,
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:library_app/src/providers/auth_provider.dart'; import 'package:library_app/src/providers/auth_provider.dart';
import 'package:library_app/src/widgets/ui/button_custom.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
class LoanBookForm extends StatefulWidget { class LoanBookForm extends StatefulWidget {
...@@ -86,17 +87,16 @@ class _LoanBookForm extends State<LoanBookForm> { ...@@ -86,17 +87,16 @@ class _LoanBookForm extends State<LoanBookForm> {
}, },
child: const Text('Cancel'), child: const Text('Cancel'),
), ),
FilledButton( ButtonCustom(
onPressed: () { onPressed: () {
if (_formKey.currentState!.validate()) {} if (_formKey.currentState!.validate()) {}
authProvider authProvider.createMemberLoan(
.createMemberLoan(
authProvider.user!.id, authProvider.user!.id,
widget.bookId, widget.bookId,
int.parse(loanDayController.text), int.parse(loanDayController.text),
) );
.then((_) => Navigator.of(context).pop());
}, },
isLoading: authProvider.isLoading,
child: const Text('Submit'), child: const Text('Submit'),
), ),
], ],
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart'; import 'package:flutter_svg/svg.dart';
import 'package:library_app/src/providers/auth_provider.dart';
import 'package:library_app/src/widgets/loading.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:library_app/src/providers/auth_provider.dart';
import 'package:library_app/src/widgets/ui/button_custom.dart';
class ChangePasswordForm extends StatefulWidget { class ChangePasswordForm extends StatefulWidget {
const ChangePasswordForm({super.key}); const ChangePasswordForm({super.key});
...@@ -140,7 +141,7 @@ class _ChangePasswordForm extends State<ChangePasswordForm> { ...@@ -140,7 +141,7 @@ class _ChangePasswordForm extends State<ChangePasswordForm> {
), ),
SizedBox( SizedBox(
width: double.infinity, width: double.infinity,
child: FilledButton( child: ButtonCustom(
onPressed: () async { onPressed: () async {
if (_formKey.currentState!.validate()) {} if (_formKey.currentState!.validate()) {}
authProvider.changePassword( authProvider.changePassword(
...@@ -150,9 +151,8 @@ class _ChangePasswordForm extends State<ChangePasswordForm> { ...@@ -150,9 +151,8 @@ class _ChangePasswordForm extends State<ChangePasswordForm> {
newPasswordController2.text, newPasswordController2.text,
); );
}, },
child: authProvider.isLoading isLoading: authProvider.isLoading,
? const Loading() child: const Text("Submit"),
: const Text("Submit"),
), ),
), ),
], ],
......
import 'package:provider/provider.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:library_app/src/providers/auth_provider.dart'; import 'package:library_app/src/providers/auth_provider.dart';
import 'package:library_app/src/widgets/loading.dart'; import 'package:library_app/src/widgets/ui/button_custom.dart';
import 'package:provider/provider.dart';
class LoginForm extends StatefulWidget { class LoginForm extends StatefulWidget {
const LoginForm({super.key}); const LoginForm({super.key});
...@@ -120,7 +120,7 @@ class _LoginForm extends State<LoginForm> { ...@@ -120,7 +120,7 @@ class _LoginForm extends State<LoginForm> {
children: [ children: [
SizedBox( SizedBox(
width: double.infinity, width: double.infinity,
child: FilledButton( child: ButtonCustom(
onPressed: () async { onPressed: () async {
if (_formKey.currentState!.validate()) {} if (_formKey.currentState!.validate()) {}
authProvider.signIn( authProvider.signIn(
...@@ -129,9 +129,8 @@ class _LoginForm extends State<LoginForm> { ...@@ -129,9 +129,8 @@ class _LoginForm extends State<LoginForm> {
passwordController.text, passwordController.text,
); );
}, },
child: authProvider.isLoading isLoading: authProvider.isLoading,
? const Loading() child: const Text("Submit"),
: const Text("Submit"),
), ),
), ),
SizedBox( SizedBox(
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:library_app/src/providers/auth_provider.dart'; import 'package:library_app/src/widgets/ui/button_custom.dart';
import 'package:provider/provider.dart';
import 'package:library_app/src/providers/auth_provider.dart';
import 'package:library_app/src/models/user.dart'; import 'package:library_app/src/models/user.dart';
import 'package:provider/provider.dart';
class ProfileEditForm extends StatefulWidget { class ProfileEditForm extends StatefulWidget {
final User? user; final User? user;
...@@ -102,7 +103,7 @@ class _ProfileEditForm extends State<ProfileEditForm> { ...@@ -102,7 +103,7 @@ class _ProfileEditForm extends State<ProfileEditForm> {
padding: const EdgeInsets.only(top: 40.0), padding: const EdgeInsets.only(top: 40.0),
child: SizedBox( child: SizedBox(
width: double.infinity, width: double.infinity,
child: FilledButton( child: ButtonCustom(
onPressed: () { onPressed: () {
if (_formKey.currentState!.validate()) {} if (_formKey.currentState!.validate()) {}
authProvider.updateUserDetail( authProvider.updateUserDetail(
...@@ -115,6 +116,7 @@ class _ProfileEditForm extends State<ProfileEditForm> { ...@@ -115,6 +116,7 @@ class _ProfileEditForm extends State<ProfileEditForm> {
authProvider.user!.isStaff, authProvider.user!.isStaff,
); );
}, },
isLoading: authProvider.isLoading,
child: const Text("Submit"), child: const Text("Submit"),
), ),
), ),
......
...@@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; ...@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:library_app/src/providers/auth_provider.dart'; import 'package:library_app/src/providers/auth_provider.dart';
import 'package:library_app/src/widgets/loading.dart'; import 'package:library_app/src/widgets/ui/button_custom.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
class ResetPasswordForm extends StatefulWidget { class ResetPasswordForm extends StatefulWidget {
...@@ -85,15 +85,14 @@ class _ResetPasswordForm extends State<ResetPasswordForm> { ...@@ -85,15 +85,14 @@ class _ResetPasswordForm extends State<ResetPasswordForm> {
children: [ children: [
SizedBox( SizedBox(
width: double.infinity, width: double.infinity,
child: FilledButton( child: ButtonCustom(
onPressed: () { onPressed: () {
if (_formKey.currentState!.validate()) {} if (_formKey.currentState!.validate()) {}
authProvider.resetPassword( authProvider.resetPassword(
context, emailController.text); context, emailController.text);
}, },
child: authProvider.isLoading isLoading: authProvider.isLoading,
? const Loading() child: const Text("Submit"),
: const Text("Submit"),
), ),
), ),
], ],
...@@ -254,7 +253,7 @@ class _ConfirmResetPasswordForm extends State<ConfirmResetPasswordForm> { ...@@ -254,7 +253,7 @@ class _ConfirmResetPasswordForm extends State<ConfirmResetPasswordForm> {
children: [ children: [
SizedBox( SizedBox(
width: double.infinity, width: double.infinity,
child: FilledButton( child: ButtonCustom(
onPressed: () { onPressed: () {
if (_formKey.currentState!.validate()) {} if (_formKey.currentState!.validate()) {}
authProvider.confirmResetPassword( authProvider.confirmResetPassword(
...@@ -264,9 +263,8 @@ class _ConfirmResetPasswordForm extends State<ConfirmResetPasswordForm> { ...@@ -264,9 +263,8 @@ class _ConfirmResetPasswordForm extends State<ConfirmResetPasswordForm> {
password2Controller.text, password2Controller.text,
); );
}, },
child: authProvider.isLoading isLoading: authProvider.isLoading,
? const Loading() child: const Text("Submit"),
: const Text("Submit"),
), ),
), ),
], ],
......
...@@ -28,14 +28,9 @@ class _SearchForm extends State<SearchForm> { ...@@ -28,14 +28,9 @@ class _SearchForm extends State<SearchForm> {
elevation: WidgetStateProperty.all(0), elevation: WidgetStateProperty.all(0),
onChanged: (value) { onChanged: (value) {
if (value.length >= 3) { if (value.length >= 3) {
Future.delayed(
Duration.zero,
() {
Provider.of<BookProvider>(context, listen: false) Provider.of<BookProvider>(context, listen: false)
.setSearchKeyword(value); .setSearchKeyword(value);
Provider.of<BookProvider>(context, listen: false).getBooks(); Provider.of<BookProvider>(context, listen: false).getBooks();
},
);
} }
}, },
leading: const Icon(Icons.search), leading: const Icon(Icons.search),
......
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart'; import 'package:flutter_svg/flutter_svg.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:library_app/src/providers/auth_provider.dart'; import 'package:library_app/src/widgets/ui/button_custom.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:library_app/src/providers/auth_provider.dart';
class SignUpForm extends StatefulWidget { class SignUpForm extends StatefulWidget {
const SignUpForm({super.key}); const SignUpForm({super.key});
...@@ -131,7 +133,7 @@ class _SignUpForm extends State<SignUpForm> { ...@@ -131,7 +133,7 @@ class _SignUpForm extends State<SignUpForm> {
children: [ children: [
SizedBox( SizedBox(
width: double.infinity, width: double.infinity,
child: FilledButton( child: ButtonCustom(
onPressed: () { onPressed: () {
if (_formKey.currentState!.validate()) {} if (_formKey.currentState!.validate()) {}
authProvider.signUp( authProvider.signUp(
...@@ -141,6 +143,7 @@ class _SignUpForm extends State<SignUpForm> { ...@@ -141,6 +143,7 @@ class _SignUpForm extends State<SignUpForm> {
passwordController.text, passwordController.text,
); );
}, },
isLoading: authProvider.isLoading,
child: const Text("Submit"), child: const Text("Submit"),
), ),
), ),
......
import 'package:flutter/material.dart';
import 'package:library_app/src/widgets/loading.dart';
class ButtonCustom extends StatefulWidget {
final Widget? child;
final bool? isLoading;
final void Function()? onPressed;
const ButtonCustom({super.key, this.onPressed, this.child, this.isLoading});
@override
State<StatefulWidget> createState() => _ButtonCustom();
}
class _ButtonCustom extends State<ButtonCustom> {
get onPressed => widget.onPressed;
get child => widget.child;
get isLoading => widget.isLoading;
@override
Widget build(BuildContext context) {
return FilledButton(
onPressed: onPressed,
child: isLoading ? const Loading() : child,
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment