Skip to content

Commit

Permalink
WIP cleanup and use flutter 3
Browse files Browse the repository at this point in the history
  • Loading branch information
AhmedHanafy725 committed Dec 1, 2023
1 parent 22ef976 commit 05a0562
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 66 deletions.
81 changes: 43 additions & 38 deletions app/lib/screens/authentication_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ import 'package:threebotlogin/services/shared_preference_service.dart';
import 'package:threebotlogin/widgets/custom_dialog.dart';

class AuthenticationScreen extends StatefulWidget {
const AuthenticationScreen(
{super.key, this.correctPin, required this.userMessage, this.loginData});

final int pinLength = 4;
final String correctPin;
final String? correctPin;
final String userMessage;
final Login? loginData;

@override
AuthenticationScreen(
{required this.correctPin, required this.userMessage, this.loginData});

@override
AuthenticationScreenState createState() => AuthenticationScreenState();
State<AuthenticationScreen> createState() => AuthenticationScreenState();
}

class AuthenticationScreenState extends State<AuthenticationScreen> {
int timeout = 30000;
Globals globals = Globals();
late Timer timer;

void initState() {
@override
initState() {
super.initState();

Events().onEvent(CloseAuthEvent().runtimeType, (CloseAuthEvent event) {
Expand All @@ -40,10 +40,9 @@ class AuthenticationScreenState extends State<AuthenticationScreen> {
});

if (widget.loginData != null && widget.loginData!.isMobile == false) {
const oneSec = const Duration(seconds: 1);
const oneSec = Duration(seconds: 1);

print('Starting timer ... ');
timer = new Timer.periodic(oneSec, (Timer t) async {
timer = Timer.periodic(oneSec, (Timer t) async {
timeoutTimer();
});
}
Expand All @@ -58,7 +57,7 @@ class AuthenticationScreenState extends State<AuthenticationScreen> {
}

int? created = widget.loginData!.created;
int currentTimestamp = new DateTime.now().millisecondsSinceEpoch;
int currentTimestamp = DateTime.now().millisecondsSinceEpoch;

if (created != null &&
((currentTimestamp - created) / 1000) > Globals().loginTimeout) {
Expand All @@ -73,7 +72,7 @@ class AuthenticationScreenState extends State<AuthenticationScreen> {
'Your login attempt has expired, please request a new one in your browser.',
actions: <Widget>[
TextButton(
child: Text('Ok'),
child: const Text('Ok'),
onPressed: () {
Navigator.pop(context);
},
Expand Down Expand Up @@ -114,34 +113,37 @@ class AuthenticationScreenState extends State<AuthenticationScreen> {
margin: EdgeInsets.all(height / 120),
height: height / 50,
width: size,
decoration: BoxDecoration(color: Colors.black, shape: BoxShape.circle),
duration: Duration(milliseconds: 100),
decoration:
const BoxDecoration(color: Colors.black, shape: BoxShape.circle),
duration: const Duration(milliseconds: 100),
curve: Curves.bounceInOut,
);
}

Widget buildNumberPin(String buttonText, BuildContext context,
{Color backgroundColor: Colors.blueGrey}) {
{Color backgroundColor = Colors.blueGrey}) {
var onPressedMethod = () => handleInput(buttonText);
double height = MediaQuery.of(context).size.height;

if (buttonText == 'OK')
if (buttonText == 'OK') {
onPressedMethod =
(input.length >= widget.pinLength ? () => onOk() : () {});
if (buttonText == 'C')
onPressedMethod = (input.length >= 1 ? () => onClear() : () {});
}
if (buttonText == 'C') {
onPressedMethod = (input.isNotEmpty ? () => onClear() : () {});
}
return Container(
padding: EdgeInsets.only(top: height / 136, bottom: height / 136),
child: Center(
child: RawMaterialButton(
padding: EdgeInsets.all(12),
padding: const EdgeInsets.all(12),
onPressed: onPressedMethod,
fillColor: backgroundColor,
shape: const CircleBorder(),
child: Text(
buttonText,
style: TextStyle(color: Colors.white, fontSize: 20),
style: const TextStyle(color: Colors.white, fontSize: 20),
),
onPressed: onPressedMethod,
fillColor: backgroundColor,
shape: CircleBorder(),
)));
}

Expand All @@ -162,19 +164,20 @@ class AuthenticationScreenState extends State<AuthenticationScreen> {
];
List<Widget> pins = List.generate(possibleInput.length, (int i) {
String buttonText = possibleInput[i];
if (buttonText == 'C')
if (buttonText == 'C') {
return buildNumberPin(possibleInput[i], context,
backgroundColor: input.length >= 1
backgroundColor: input.isNotEmpty
? Colors.yellow.shade700
: Colors.yellow.shade200);
else if (buttonText == 'OK')
} else if (buttonText == 'OK') {
return buildNumberPin(possibleInput[i], context,
backgroundColor: input.length >= widget.pinLength
? Colors.green.shade600
: Colors.green.shade100);
else
} else {
return buildNumberPin(possibleInput[i], context,
backgroundColor: HexColor('#0a73b8'));
}
});
return Container(
width: double.infinity,
Expand Down Expand Up @@ -217,10 +220,10 @@ class AuthenticationScreenState extends State<AuthenticationScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
appBar: AppBar(
automaticallyImplyLeading: true,
backgroundColor: HexColor("#0a73b8"),
title: Text("Authentication"),
backgroundColor: HexColor('#0a73b8'),
title: const Text('Authentication'),
),
body: Container(
color: Colors.white,
Expand All @@ -229,16 +232,16 @@ class AuthenticationScreenState extends State<AuthenticationScreen> {
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Container(
child: Text(widget.userMessage),
padding: const EdgeInsets.only(bottom: 50),
child: Text(widget.userMessage),
),
Container(
alignment: Alignment.center,
color: Colors.white,
child: Column(
children: [
generateTextFields(context),
SizedBox(height: 25),
const SizedBox(height: 25),
generateNumbers(context),
],
),
Expand All @@ -252,8 +255,10 @@ class AuthenticationScreenState extends State<AuthenticationScreen> {
Future<void> onOk() async {
HapticFeedback.mediumImpact();

String pin = "";
input.forEach((char) => pin += char);
String pin = '';
for (var char in input) {
pin += char;
}

int currentTime = new DateTime.now().millisecondsSinceEpoch;

Expand Down Expand Up @@ -286,14 +291,14 @@ class AuthenticationScreenState extends State<AuthenticationScreen> {
}

dialog = CustomDialog(
title: "Too many attempts",
title: 'Too many attempts',
description:
"Too many incorrect attempts, please wait ${((globals.lockedUntill - currentTime) / 1000).toStringAsFixed(0)} seconds",
'Too many incorrect attempts, please wait ${((globals.lockedUntill - currentTime) / 1000).toStringAsFixed(0)} seconds',
);
} else {
dialog = CustomDialog(
title: "Incorrect pin",
description: "Your pin code is incorrect.",
title: 'Incorrect pin',
description: 'Your pin code is incorrect.',
);
}

Expand Down
53 changes: 27 additions & 26 deletions app/lib/screens/change_pin_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,56 @@ import 'package:threebotlogin/services/shared_preference_service.dart';
import 'package:threebotlogin/widgets/pin_field.dart';

class ChangePinScreen extends StatefulWidget {
final currentPin;
final bool? hideBackButton;
const ChangePinScreen({super.key, this.currentPin, this.hideBackButton});

ChangePinScreen({this.currentPin, this.hideBackButton});
final String? currentPin;
final bool? hideBackButton;

_ChangePinScreenState createState() => _ChangePinScreenState();
@override
State<ChangePinScreen> createState() => _ChangePinScreenState();
}

enum _State { CurrentPin, CurrentPinWrong, NewPinWrong, NewPin, Confirm, Done }
enum _State { newPinWrong, newPin, confirm, done }

class _ChangePinScreenState extends State<ChangePinScreen> {
String newPin = '';
_State? state;

_ChangePinScreenState() {
state = _State.NewPin;
state = _State.newPin;
}

getText() {
switch (state) {
case _State.NewPinWrong:
return "Confirmation incorrect, please enter your new PIN";
case _State.NewPin:
return "Please enter your new PIN";
case _State.Confirm:
return "Please confirm your new PIN";
case _State.newPinWrong:
return 'Confirmation incorrect, please enter your new PIN';
case _State.newPin:
return 'Please enter your new PIN';
case _State.confirm:
return 'Please confirm your new PIN';
default:
break;
}
return "";
return '';
}

@override
Widget build(BuildContext context) {
return WillPopScope(
child: Scaffold(
appBar: new AppBar(
backgroundColor: HexColor("#0a73b8"),
appBar: AppBar(
backgroundColor: HexColor('#0a73b8'),
title: widget.currentPin == null
? Text("Choose your pincode")
: Text("Change pincode"),
? const Text('Choose your pincode')
: const Text('Change pincode'),
elevation: 0.0,
automaticallyImplyLeading: widget.hideBackButton == false),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: <Widget>[
Container(
padding: EdgeInsets.only(top: 0.0, bottom: 32.0),
padding: const EdgeInsets.only(top: 0.0, bottom: 32.0),
child: Center(
child: Text(
getText(),
Expand All @@ -65,7 +66,7 @@ class _ChangePinScreenState extends State<ChangePinScreen> {
),
),
onWillPop: () {
if (state != _State.Done && widget.hideBackButton == true) {
if (state != _State.done && widget.hideBackButton == true) {
return Future(() => false);
}
return Future(() => true);
Expand All @@ -76,24 +77,24 @@ class _ChangePinScreenState extends State<ChangePinScreen> {
Future<void> changePin(String enteredPinCode) async {
setState(() {
switch (state) {
case _State.NewPinWrong:
case _State.NewPin:
case _State.newPinWrong:
case _State.newPin:
newPin = enteredPinCode;
state = _State.Confirm;
state = _State.confirm;
break;
case _State.Confirm:
case _State.confirm:
if (newPin == enteredPinCode) {
state = _State.Done;
state = _State.done;
} else {
state = _State.NewPinWrong;
state = _State.newPinWrong;
}
break;
default:
break;
}
});

if (state == _State.Done) {
if (state == _State.done) {
await savePin(enteredPinCode);
Navigator.pop(context, true);
}
Expand Down
2 changes: 1 addition & 1 deletion app/lib/widgets/pin_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class _PinFieldState extends State<PinField> {
}

Widget buildNumberPin(String buttonText, BuildContext context,
{Color backgroundColor: Colors.blueGrey}) {
{Color backgroundColor= Colors.blueGrey}) {
var onPressedMethod = () => handleInput(buttonText);
double height = MediaQuery.of(context).size.height;

Expand Down
2 changes: 1 addition & 1 deletion app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publish_to: "none"
version: 3.9.0+176

environment:
sdk: ">=2.12.0<3.0.0"
sdk: ">=3.0.0<3.13.7"

dependencies:
flutter:
Expand Down

0 comments on commit 05a0562

Please sign in to comment.