passo_mobile_app/lib/screens/unit2/login/qr_login.dart

252 lines
12 KiB
Dart
Raw Permalink Normal View History

2023-01-23 03:02:59 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
2023-01-23 08:23:20 +00:00
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
2023-06-06 06:54:51 +00:00
import 'package:flutter_spinkit/flutter_spinkit.dart';
2023-01-23 03:02:59 +00:00
import 'package:flutter_svg/svg.dart';
import 'package:fluttericon/font_awesome5_icons.dart';
2023-06-06 06:54:51 +00:00
import 'package:fluttertoast/fluttertoast.dart';
2023-01-23 03:02:59 +00:00
import 'package:unit2/theme-data.dart/btn-style.dart';
2023-06-06 06:54:51 +00:00
import 'package:unit2/utils/global_context.dart';
2023-01-23 03:02:59 +00:00
import 'package:unit2/widgets/wave.dart';
import '../../../bloc/user/user_bloc.dart';
2023-01-23 03:02:59 +00:00
import '../../../theme-data.dart/colors.dart';
import '../../../theme-data.dart/form-style.dart';
2023-06-06 06:54:51 +00:00
import '../../../utils/alerts.dart';
2023-01-23 03:02:59 +00:00
import '../../../utils/global.dart';
import '../../../utils/internet_time_out.dart';
2023-01-23 03:02:59 +00:00
import '../../../utils/text_container.dart';
import '../../../utils/validators.dart';
import '../../../widgets/error_state.dart';
2023-01-23 03:02:59 +00:00
class QRLogin extends StatefulWidget {
const QRLogin({super.key});
@override
State<QRLogin> createState() => _QRLoginState();
}
class _QRLoginState extends State<QRLogin> {
bool showSuffixIcon = false;
bool _showPassword = true;
final _formKey = GlobalKey<FormBuilderState>();
2023-06-06 06:54:51 +00:00
String? password;
2023-01-23 03:02:59 +00:00
@override
Widget build(BuildContext context) {
2023-01-23 08:23:20 +00:00
return WillPopScope(
onWillPop: ()async{
2023-06-06 06:54:51 +00:00
Navigator.pushReplacementNamed(context,"/");
2023-01-23 08:23:20 +00:00
return true;
},
child: SafeArea(
child: Scaffold(
resizeToAvoidBottomInset: true,
appBar: AppBar(
backgroundColor: primary,
elevation: 0,
title: const Text("Login via QR"),
centerTitle: true,
),
body: ProgressHUD(
2023-06-06 06:54:51 +00:00
backgroundColor: Colors.black87,
indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
2023-01-23 08:23:20 +00:00
child: BlocConsumer<UserBloc, UserState>(
listener: (context,state){
if (state is UserLoggedIn) {
2023-06-06 06:54:51 +00:00
if (state.success == true) {
if (!state.savedCredentials!) {
confirmAlertWithCancel(context, () async {
await CREDENTIALS?.put('saved', "saved");
await CREDENTIALS?.put('username',
state.userData!.user!.login!.user!.username!);
await CREDENTIALS?.put('password', password);
Fluttertoast.showToast(
msg: "Credentials Successfully saved",
toastLength: Toast.LENGTH_LONG,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.black,
);
Navigator.pushReplacementNamed(NavigationService.navigatorKey.currentContext!, '/module-screen');
},
() => Navigator.pushReplacementNamed(
context, '/module-screen'),
"Save credentials?",
"Do you want to save credentials so you don't have to login again next time?.");
} else {
Navigator.pushReplacementNamed(context, '/module-screen');
}
} else {
final progress = ProgressHUD.of(context);
progress!.dismiss();
errorAlert(context, "Error Login", state.message, () {
context
.read<UserBloc>()
.add(LoadUuid());
Navigator.of(context).pop();
});
}
2023-01-23 03:02:59 +00:00
}
2023-01-23 08:23:20 +00:00
},
builder: (context, state) {
if (state is UuidLoaded) {
return SingleChildScrollView(
child: Stack(
children: [
Positioned(
bottom: 0,
child: WaveReverse(height: blockSizeVertical * 8)),
Container(
height: screenHeight * .87,
2023-06-06 06:54:51 +00:00
padding: const EdgeInsets.symmetric(horizontal: 32),
2023-01-23 08:23:20 +00:00
child: FormBuilder(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
SvgPicture.asset(
'assets/svgs/logo.svg',
height: blockSizeVertical * 12,
allowDrawingOutsideViewBox: true,
color: primary,
2023-01-23 03:02:59 +00:00
),
2023-01-23 08:23:20 +00:00
const SizedBox(
height: 12,
),
Text(unitApp,
style: TextStyle(
fontSize: blockSizeVertical * 5,
fontWeight: FontWeight.w800,
letterSpacing: .2,
height: 1,
color: Colors.black87)),
const SizedBox(
height: 15,
),
Text(
"Enter your password",
style: TextStyle(
fontSize: blockSizeVertical * 1.5,
height: 1.5,
fontWeight: FontWeight.w600),
),
const SizedBox(
height: 15,
),
// Password
FormBuilderTextField(
name: 'password',
validator: registerPasswordValidator,
// initialValue: state.password,
onChanged: (value) {
value!.isEmpty
? setState(() {
showSuffixIcon = false;
})
: setState(() {
showSuffixIcon = true;
});
},
autofocus: false,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black87),
decoration: loginTextFieldStyle().copyWith(
suffixIcon: Visibility(
visible: showSuffixIcon,
child: _showPassword
? IconButton(
icon: Icon(FontAwesome5.eye_slash,
size: 24,
color: Theme.of(context)
.textTheme
.displayLarge
?.color),
onPressed: () {
setState(() {
_showPassword = false;
});
},
)
: IconButton(
onPressed: () {
setState(() {
_showPassword = true;
});
},
icon: Icon(FontAwesome5.eye,
size: 24,
color: Theme.of(context)
.textTheme
.displayLarge
?.color)),
),
prefixIcon: const Icon(Icons.lock),
labelText: "Password",
hintText: enterPassword),
obscureText: _showPassword ? true : false,
),
const SizedBox(
height: 15,
),
2023-01-23 08:43:02 +00:00
2023-01-23 08:23:20 +00:00
const SizedBox(
height: 15,
),
SizedBox(
width: double.infinity,
height: blockSizeVertical * 6,
child: ElevatedButton(
style: secondaryBtnStyle(
2023-06-06 06:54:51 +00:00
primary, Colors.transparent, primary),
2023-01-23 08:23:20 +00:00
onPressed: () {
if (_formKey.currentState!
.saveAndValidate()) {
2023-06-06 06:54:51 +00:00
password = _formKey.currentState!.value['password'];
2023-01-23 08:23:20 +00:00
final progress =
ProgressHUD.of(context);
progress?.showWithText(
'Logging in...',
);
context.read<UserBloc>().add(UuidLogin(uuid: state.uuid,password: _formKey.currentState!.value['password']));
2023-01-23 08:43:02 +00:00
2023-01-23 08:23:20 +00:00
}
},
2023-06-06 06:54:51 +00:00
child: const Text("LOGIN")),
2023-01-23 08:23:20 +00:00
)
],
),
2023-01-23 03:02:59 +00:00
),
2023-01-23 08:23:20 +00:00
),
],
2023-01-23 03:02:59 +00:00
),
2023-01-23 08:23:20 +00:00
);
} if (state is InternetTimeout) {
return SomethingWentWrong(message: state.message, onpressed: () {
context
.read<UserBloc>()
.add(LoadUuid());
Navigator.of(context).pop();
},);
}
if(state is LoginErrorState){
return SomethingWentWrong(message: state.message, onpressed: () {
context
.read<UserBloc>()
.add(LoadUuid());
Navigator.of(context).pop();
},);
}
2023-01-23 08:23:20 +00:00
return Container();
},
),
),
2023-01-23 03:02:59 +00:00
),
),
);
}
}