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

393 lines
20 KiB
Dart
Raw Normal View History

2023-01-25 05:13:21 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
2023-01-25 05:13:21 +00:00
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:fluttericon/font_awesome5_icons.dart';
import 'package:fluttertoast/fluttertoast.dart';
2023-01-25 05:13:21 +00:00
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:unit2/screens/unit2/login/components/update_required.dart';
2023-06-06 06:54:51 +00:00
import 'package:unit2/screens/unit2/login/qr_login.dart';
import 'package:unit2/utils/alerts.dart';
import 'package:unit2/utils/internet_time_out.dart';
2023-01-25 05:13:21 +00:00
import 'package:unit2/utils/text_container.dart';
import 'package:unit2/widgets/error_state.dart';
import '../../../bloc/user/user_bloc.dart';
2023-04-05 00:54:24 +00:00
import '../../../utils/global_context.dart';
2023-01-25 05:13:21 +00:00
import '../../../widgets/splash_screen.dart';
import '../../../widgets/wave.dart';
import '../../../utils/global.dart';
import '../../../theme-data.dart/colors.dart';
import '../../../theme-data.dart/form-style.dart';
import '../../../theme-data.dart/btn-style.dart';
import './components/login-via-qr-label.dart';
import './functions/press-again-to-exit.dart';
class UniT2Login extends StatefulWidget {
const UniT2Login({super.key});
@override
State<UniT2Login> createState() => _UniT2LoginState();
}
class _UniT2LoginState extends State<UniT2Login> {
final _formKey = GlobalKey<FormBuilderState>();
bool showSuffixIcon = false;
bool _showPassword = true;
2023-04-05 00:54:24 +00:00
String? password;
2023-04-11 01:27:53 +00:00
String? username;
2023-01-25 05:13:21 +00:00
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: pressAgainToExit,
child: Scaffold(
body: ProgressHUD(
backgroundColor: Colors.black87,
indicatorWidget: const SpinKitFadingCircle(color: Colors.white),
2023-01-25 05:13:21 +00:00
child: BlocConsumer<UserBloc, UserState>(listener: (context, state) {
2023-06-06 06:54:51 +00:00
print(state);
if (state is UserLoggedIn || state is UuidLoaded) {
2023-01-25 05:13:21 +00:00
final progress = ProgressHUD.of(context);
progress!.dismiss();
}
if (state is UserLoggedIn) {
if (state.success == true) {
2023-04-05 00:54:24 +00:00
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');
2023-04-05 00:54:24 +00:00
},
() => Navigator.pushReplacementNamed(
context, '/module-screen'),
"Save credentials?",
"Do you want to save credentials so you don't have to login again next time?.");
2023-06-06 06:54:51 +00:00
} else {
Navigator.pushReplacementNamed(context, '/module-screen');
2023-04-05 00:54:24 +00:00
}
} else {
final progress = ProgressHUD.of(context);
progress!.dismiss();
errorAlert(context, "Error Login", state.message, () {
2023-06-06 06:54:51 +00:00
context
.read<UserBloc>()
.add(LoadVersion(username: username, password: password));
Navigator.of(context).pop();
});
}
}
if (state is UuidLoaded) {
Navigator.push(context,
MaterialPageRoute(builder: (BuildContext context) {
2023-06-06 06:54:51 +00:00
return const QRLogin();
}));
}
2023-01-25 05:13:21 +00:00
}, builder: (context, state) {
if (state is VersionLoaded) {
return Builder(builder: (context) {
if (state.versionInfo!.version != state.apkVersion) {
return SizedBox(
child: SingleChildScrollView(
child: Stack(
alignment: Alignment.center,
children: [
Positioned(
bottom: 0,
right: 0,
child:
WaveReverse(height: blockSizeVertical * 7)),
SizedBox(
height: blockSizeVertical * 100,
child: FormBuilder(
key: _formKey,
child: Padding(
padding:
2023-06-06 06:54:51 +00:00
const EdgeInsets.symmetric(horizontal: 42),
2023-01-25 05:13:21 +00:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SvgPicture.asset(
'assets/svgs/logo.svg',
height: blockSizeVertical * 12,
allowDrawingOutsideViewBox: true,
color: primary,
),
Text(
welcome,
style: TextStyle(
fontSize: blockSizeVertical * 4,
fontWeight: FontWeight.w600),
),
Text(unitApp,
style: TextStyle(
fontSize: blockSizeVertical * 6,
fontWeight: FontWeight.w800,
letterSpacing: .2,
height: 1,
color: primary)),
// Text(
// loginToContinue,
// style: TextStyle(
// fontSize: blockSizeVertical * 1.7,
// height: 1.5,
// fontWeight: FontWeight.w600),
// ),
SizedBox(
height: blockSizeVertical * 3,
),
2023-04-13 08:45:19 +00:00
//// USERNAME
2023-01-25 05:13:21 +00:00
FormBuilderTextField(
name: 'username',
2023-04-13 08:45:19 +00:00
initialValue: state.username,
2023-01-25 05:13:21 +00:00
validator:
FormBuilderValidators.required(
errorText: usernameRequired),
autofocus: false,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black87),
decoration:
loginTextFieldStyle().copyWith()),
SizedBox(
height: blockSizeVertical * 1.5,
),
2023-04-13 08:45:19 +00:00
//// PASSWORD
2023-01-25 05:13:21 +00:00
FormBuilderTextField(
name: 'password',
2023-04-13 08:45:19 +00:00
initialValue: state.password,
2023-01-25 05:13:21 +00:00
validator: FormBuilderValidators.required(
errorText: passwordRequired),
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,
color: primary,
),
labelText: "Password",
hintText: enterPassword),
obscureText: _showPassword ? true : false,
),
SizedBox(
height: blockSizeVertical * 2,
),
SizedBox(
height: blockSizeVertical * 7,
2023-04-13 08:45:19 +00:00
//// Login Button
2023-01-25 05:13:21 +00:00
child: SizedBox(
width:
MediaQuery.of(context).size.width,
child: ElevatedButton(
style: mainBtnStyle(
second,
Colors.transparent,
Colors.white54),
child: const Text(
login,
style:
TextStyle(color: Colors.white),
),
onPressed: () {
final progress =
ProgressHUD.of(context);
FocusScope.of(context).unfocus();
if (_formKey.currentState!
.saveAndValidate()) {
2023-06-06 06:54:51 +00:00
password = _formKey.currentState!
.value['password'];
username = _formKey.currentState!
.value['username'];
2023-01-25 05:13:21 +00:00
progress?.showWithText(
'Logging in...',
);
BlocProvider.of<UserBloc>(context)
.add(UserLogin(
2023-06-06 06:54:51 +00:00
username: username,
password: password));
2023-01-25 05:13:21 +00:00
}
},
),
),
),
SizedBox(
height: blockSizeVertical * 1.5,
),
//// Login via Scan QR
2023-01-25 05:13:21 +00:00
SizedBox(
height: blockSizeVertical * 7,
child: SizedBox(
width:
MediaQuery.of(context).size.width,
child: ElevatedButton.icon(
style: mainBtnStyle(
Colors.white,
second,
primary.withOpacity(.4)),
icon: const Icon(
Icons.qr_code,
color: second,
),
label: const Text(
loginViaQr,
style: TextStyle(color: second),
),
onPressed: () {
context
.read<UserBloc>()
.add(GetUuid());
},
),
)),
SizedBox(
2023-04-13 08:45:19 +00:00
height: blockSizeVertical * 2,
2023-01-25 05:13:21 +00:00
),
const LoginViaQr(
text: emergencyReponseLabel),
SizedBox(
2023-04-13 08:45:19 +00:00
height: blockSizeVertical * 2,
2023-01-25 05:13:21 +00:00
),
// REQUEST SOS
SizedBox(
height: screenHeight * .07,
width: MediaQuery.of(context).size.width,
child: ElevatedButton.icon(
icon: const Icon(
FontAwesome5.life_ring,
color: Colors.white,
),
style: mainBtnStyle(
third,
Colors.transparent,
Colors.white38),
2023-04-05 00:54:24 +00:00
onPressed: () {
2023-06-06 06:54:51 +00:00
Navigator.pushNamed(
context, '/sos');
2023-04-05 00:54:24 +00:00
},
2023-01-25 05:13:21 +00:00
label: const Text(
requestSOS,
style:
TextStyle(color: Colors.white),
)),
)
],
),
),
),
),
],
),
),
);
} else {
//New update available
2023-01-25 05:13:21 +00:00
return Update(
apkVersion: state.apkVersion!,
currenVersion: state.versionInfo!.version!,
);
}
});
}
if (state is UserError) {
return SomethingWentWrong(
message: onError,
2023-04-05 00:54:24 +00:00
onpressed: () {
BlocProvider.of<UserBloc>(
NavigationService.navigatorKey.currentContext!)
.add(GetApkVersion());
return MaterialPageRoute(builder: (_) {
return const UniT2Login();
});
},
2023-01-25 05:13:21 +00:00
);
}
if (state is InternetTimeout) {
return const TimeOutError();
}
2023-01-25 05:13:21 +00:00
if (state is SplashScreen) {
return const UniTSplashScreen();
}
if (state is LoginErrorState) {
return SomethingWentWrong(
message: state.message,
onpressed: () {
BlocProvider.of<UserBloc>(
NavigationService.navigatorKey.currentContext!)
.add(GetApkVersion());
return MaterialPageRoute(builder: (_) {
return const UniT2Login();
});
},
);
}
2023-01-25 05:13:21 +00:00
return Container();
}),
),
),
);
}
}