From a65081ef7fce9c8adfeba19b48dd290909f1153f Mon Sep 17 00:00:00 2001 From: yaso-meth Date: Fri, 19 Jul 2024 13:34:34 +0200 Subject: [PATCH] SignIn Page to detect enter press to submit --- .../patient_manager/lib/pages/signin.dart | 247 ++++++++++-------- 1 file changed, 139 insertions(+), 108 deletions(-) diff --git a/Frontend/patient_manager/lib/pages/signin.dart b/Frontend/patient_manager/lib/pages/signin.dart index 98931e8b..49209429 100644 --- a/Frontend/patient_manager/lib/pages/signin.dart +++ b/Frontend/patient_manager/lib/pages/signin.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:patient_manager/components/myErrorMessage.dart'; import 'package:patient_manager/components/myPassInput.dart'; import 'package:patient_manager/components/myTextInput.dart'; @@ -19,6 +20,8 @@ class _SignInState extends State { final passwordController = TextEditingController(); //bool _obscureText = true; bool successfulSignIn = false; + // focus node to capture keyboard events + final FocusNode _focusNode = FocusNode(); //sign user in Future signUserIn() async { @@ -51,124 +54,152 @@ class _SignInState extends State { @override Widget build(BuildContext context) { - return Scaffold( - //backgroundColor: Colors.white, - body: SafeArea( - child: Center( - child: SingleChildScrollView( - physics: const BouncingScrollPhysics(), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - //logo - Icon( - Icons.lock, - size: 100, - color: - MzanziInnovationHub.of(context)!.theme.secondaryColor(), - ), - //spacer - const SizedBox(height: 10), - //Heading - Text( - 'Sign In', - style: TextStyle( - fontSize: 25, - fontWeight: FontWeight.bold, + return KeyboardListener( + focusNode: _focusNode, + autofocus: true, + onKeyEvent: (event) { + if (event is KeyDownEvent && + event.logicalKey == LogicalKeyboardKey.enter) { + if (emailController.text.isEmpty || passwordController.text.isEmpty) { + showDialog( + context: context, + builder: (context) { + return const MyErrorMessage(errorType: "Input Error"); + }, + ); + } else { + signUserIn(); + } + + if (successfulSignIn) { + Navigator.of(context).pushNamed('/homme'); + } + } + }, + child: Scaffold( + //backgroundColor: Colors.white, + body: SafeArea( + child: Center( + child: SingleChildScrollView( + physics: const BouncingScrollPhysics(), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + //logo + Icon( + Icons.lock, + size: 100, color: MzanziInnovationHub.of(context)!.theme.secondaryColor(), ), - ), - //spacer - const SizedBox(height: 25), - //email input - SizedBox( - width: 500.0, - child: MyTextField( - controller: emailController, - hintText: 'Email', - editable: true, - required: true, + //spacer + const SizedBox(height: 10), + //Heading + Text( + 'Sign In', + style: TextStyle( + fontSize: 25, + fontWeight: FontWeight.bold, + color: MzanziInnovationHub.of(context)! + .theme + .secondaryColor(), + ), ), - ), - //spacer - const SizedBox(height: 25), - //password input - SizedBox( - width: 500.0, - child: MyPassField( - controller: passwordController, - hintText: 'Password', - required: true, + //spacer + const SizedBox(height: 25), + //email input + SizedBox( + width: 500.0, + child: MyTextField( + controller: emailController, + hintText: 'Email', + editable: true, + required: true, + ), ), - ), - //spacer - const SizedBox(height: 10), - // sign in button - SizedBox( - width: 500.0, - height: 100.0, - child: MyButton( - buttonText: "Sign In", - buttonColor: - MzanziInnovationHub.of(context)!.theme.secondaryColor(), - textColor: - MzanziInnovationHub.of(context)!.theme.primaryColor(), - onTap: () { - if (emailController.text.isEmpty || - passwordController.text.isEmpty) { - showDialog( - context: context, - builder: (context) { - return const MyErrorMessage( - errorType: "Input Error"); - }, - ); - } else { - signUserIn(); - } + //spacer + const SizedBox(height: 25), + //password input + SizedBox( + width: 500.0, + child: MyPassField( + controller: passwordController, + hintText: 'Password', + required: true, + ), + ), + //spacer + const SizedBox(height: 10), + // sign in button + SizedBox( + width: 500.0, + height: 100.0, + child: MyButton( + buttonText: "Sign In", + buttonColor: MzanziInnovationHub.of(context)! + .theme + .secondaryColor(), + textColor: + MzanziInnovationHub.of(context)!.theme.primaryColor(), + onTap: () { + if (emailController.text.isEmpty || + passwordController.text.isEmpty) { + showDialog( + context: context, + builder: (context) { + return const MyErrorMessage( + errorType: "Input Error"); + }, + ); + } else { + signUserIn(); + } - if (successfulSignIn) { - Navigator.of(context).pushNamed('/homme'); - } - }, + if (successfulSignIn) { + Navigator.of(context).pushNamed('/homme'); + } + }, + ), ), - ), - //spacer - //const SizedBox(height: 30), - //register text - SizedBox( - width: 450.0, - height: 100.0, - child: Row( - mainAxisAlignment: MainAxisAlignment.end, - children: [ - const Text( - 'New User?', - style: TextStyle( - fontSize: 18, - color: Color.fromARGB(255, 201, 200, 200)), - ), - const SizedBox( - width: 6, - ), - GestureDetector( - onTap: widget.onTap, - child: Text( - 'Register Now', + //spacer + //const SizedBox(height: 30), + //register text + SizedBox( + width: 450.0, + height: 100.0, + child: Row( + mainAxisAlignment: MainAxisAlignment.end, + children: [ + const Text( + 'New User?', style: TextStyle( - fontSize: 18, - color: MzanziInnovationHub.of(context)! - .theme - .secondaryColor(), - fontWeight: FontWeight.bold, + fontSize: 18, + color: Color.fromARGB(255, 201, 200, 200)), + ), + const SizedBox( + width: 6, + ), + GestureDetector( + onTap: widget.onTap, + child: Text( + 'Register Now', + style: TextStyle( + fontSize: 18, + color: MzanziInnovationHub.of(context)! + .theme + .secondaryColor(), + fontWeight: FontWeight.bold, + ), ), ), - ) - ], - ), - ) - ], + const SizedBox( + width: 15, + ), + ], + ), + ) + ], + ), ), ), ),