text validation and AppTextField

merge-requests/14/head
unknown 6 years ago
parent 93d2ec15aa
commit 4b55176b9d

@ -28,7 +28,7 @@ const String MEDICINE_SEARCH = 'medicine-search';
const String SETTINGS = 'settings'; const String SETTINGS = 'settings';
var routes = { var routes = {
HOME: (_) => DashboardScreen(), HOME: (_) => PatientSearchScreen(),
INIT_ROUTE: (_) => Loginsreen(), INIT_ROUTE: (_) => Loginsreen(),
MY_SCHEDULE: (_) => MyScheduleScreen(), MY_SCHEDULE: (_) => MyScheduleScreen(),
PATIENT_SEARCH: (_) => PatientSearchScreen(), PATIENT_SEARCH: (_) => PatientSearchScreen(),

@ -1,18 +1,21 @@
import '../../config/size_config.dart';
import 'package:doctor_app_flutter/config/size_config.dart'; import '../../util/text_validator.dart';
import 'package:doctor_app_flutter/widgets/shared/app_buttons_widget.dart'; import '../../widgets/shared/app_buttons_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_scaffold_widget.dart'; import '../../widgets/shared/app_scaffold_widget.dart';
import 'package:doctor_app_flutter/widgets/shared/app_texts_widget.dart'; import '../../widgets/shared/app_text_form_field.dart';
import 'package:doctor_app_flutter/widgets/shared/rounded_container_widget.dart'; import '../../widgets/shared/app_texts_widget.dart';
import '../../widgets/shared/rounded_container_widget.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import '../../lookups/patient_lookup.dart'; import '../../lookups/patient_lookup.dart';
import '../../models/patient_model.dart'; import '../../models/patient_model.dart';
import '../../routes.dart';
import '../../widgets/patients/dynamic_elements.dart'; import '../../widgets/patients/dynamic_elements.dart';
// OWNER : Ibrahim albitar
// DATE : 19-04-2020
// DESCRIPTION : Patient Search Screen.
class PatientSearchScreen extends StatefulWidget { class PatientSearchScreen extends StatefulWidget {
@override @override
_PatientSearchScreenState createState() => _PatientSearchScreenState(); _PatientSearchScreenState createState() => _PatientSearchScreenState();
@ -22,7 +25,9 @@ class _PatientSearchScreenState extends State<PatientSearchScreen> {
String _selectedType = '1'; String _selectedType = '1';
String _selectedLocation = '1'; String _selectedLocation = '1';
final _patientSearchForm = GlobalKey<FormState>(); final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
bool _autoValidate = false;
var _patientSearchFormValues = PatientModel( var _patientSearchFormValues = PatientModel(
ProjectID: 12, ProjectID: 12,
ClinicID: 17, ClinicID: 17,
@ -45,18 +50,18 @@ class _PatientSearchScreenState extends State<PatientSearchScreen> {
IsLoginForDoctorApp: true, IsLoginForDoctorApp: true,
PatientOutSA: false); PatientOutSA: false);
void _saveForm() {
_patientSearchForm.currentState.save();
// Navigator.pushNamed(context,routeName) void _validateInputs() {
print(_patientSearchFormValues.From); if (_formKey.currentState.validate()) {
print(_patientSearchFormValues.To); // If all data are correct then save data to out variables
_formKey.currentState.save();
Navigator.of(context).pushNamed(PATIENTS, arguments: { } else {
'patientSearchForm': _patientSearchFormValues, // If all data are not valid then start auto validation.
'selectedType': _selectedType setState(() {
_autoValidate = true;
}); });
} }
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -75,7 +80,8 @@ class _PatientSearchScreenState extends State<PatientSearchScreen> {
padding: EdgeInsets.all(15), padding: EdgeInsets.all(15),
width: SizeConfig.screenWidth * 1, width: SizeConfig.screenWidth * 1,
child: Form( child: Form(
key: _patientSearchForm, key: _formKey,
autovalidate: _autoValidate,
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[
@ -143,127 +149,47 @@ class _PatientSearchScreenState extends State<PatientSearchScreen> {
SizedBox( SizedBox(
height: 10, height: 10,
), ),
TextFormField( AppTextFormField(
decoration: InputDecoration( hintText: 'First Name',
hintText: 'First Name',
hintStyle: TextStyle(
fontSize: SizeConfig.textMultiplier * 2),
enabledBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30)),
borderSide:
BorderSide(color: Color(0xff707070)),
),
focusedBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30)),
borderSide: BorderSide(
color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
onSaved: (value) { onSaved: (value) {
_patientSearchFormValues.FirstName = value; _patientSearchFormValues.FirstName = value;
}, },
validator: (value) { validator: (value) {
if (value.isEmpty) { return TextValidator().validateName(value);
return 'Please enter some text';
}
return null;
}, },
), ),
SizedBox( SizedBox(
height: 10, height: 10,
), ),
TextFormField( AppTextFormField(
decoration: InputDecoration( hintText: 'Middle Name',
hintText: 'Middle Name',
hintStyle: TextStyle(
fontSize: SizeConfig.textMultiplier * 2),
enabledBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30)),
borderSide:
BorderSide(color: Color(0xff707070)),
),
focusedBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30)),
borderSide: BorderSide(
color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
onSaved: (value) { onSaved: (value) {
_patientSearchFormValues.MiddleName = value; _patientSearchFormValues.MiddleName = value;
}, },
validator: (value) {
return TextValidator().validateName(value);
},
), ),
SizedBox( SizedBox(
height: 10, height: 10,
), ),
TextFormField( AppTextFormField(
decoration: InputDecoration( hintText: 'Last Name',
hintText: 'Last Name',
hintStyle: TextStyle(
fontSize: SizeConfig.textMultiplier * 2),
enabledBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30)),
borderSide:
BorderSide(color: Color(0xff707070)),
),
focusedBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30.0)),
borderSide: BorderSide(
color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
onSaved: (value) { onSaved: (value) {
_patientSearchFormValues.LastName = value; _patientSearchFormValues.LastName = value;
}, },
validator: (value) {
return TextValidator().validateName(value);
},
), ),
SizedBox( SizedBox(
height: 10, height: 10,
), ),
TextFormField( AppTextFormField(
keyboardType: TextInputType.number, textInputType: TextInputType.number,
decoration: InputDecoration( hintText: 'Phone Number',
hintText: 'Phone Number',
hintStyle: TextStyle(
fontSize: SizeConfig.textMultiplier * 2),
enabledBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30)),
borderSide:
BorderSide(color: Color(0xff707070)),
),
focusedBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30.0)),
borderSide: BorderSide(
color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
validator: (value) { validator: (value) {
if (value.isEmpty) { return TextValidator().validateMobile(value);
return 'Please enter some text';
}
return null;
}, },
onSaved: (value) { onSaved: (value) {
_patientSearchFormValues.PatientMobileNumber = _patientSearchFormValues.PatientMobileNumber =
@ -273,67 +199,27 @@ class _PatientSearchScreenState extends State<PatientSearchScreen> {
SizedBox( SizedBox(
height: 10, height: 10,
), ),
TextFormField( AppTextFormField(
keyboardType: TextInputType.number, textInputType: TextInputType.number,
decoration: InputDecoration( hintText: 'Patiant ID',
hintText: 'Patiant ID',
hintStyle: TextStyle(
fontSize: SizeConfig.textMultiplier * 2),
enabledBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30)),
borderSide:
BorderSide(color: Color(0xff707070)),
),
focusedBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30.0)),
borderSide: BorderSide(
color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
validator: (value) { validator: (value) {
if (value.isEmpty) { return TextValidator().validateIdNumber(value);
return 'Please enter some text';
}
return null;
}, },
onSaved: (value) { onSaved: (value) {
_patientSearchFormValues.PatientID = _patientSearchFormValues.PatientID =
int.parse(value); 89000;
}), }),
SizedBox( SizedBox(
height: 10, height: 10,
), ),
TextFormField( AppTextFormField(
keyboardType: TextInputType.number, textInputType: TextInputType.number,
decoration: InputDecoration( hintText: 'Patiant File',
hintText: 'Patiant File',
hintStyle: TextStyle(
fontSize: SizeConfig.textMultiplier * 2),
enabledBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30)),
borderSide:
BorderSide(color: Color(0xff707070)),
),
focusedBorder: OutlineInputBorder(
borderRadius:
BorderRadius.all(Radius.circular(30.0)),
borderSide: BorderSide(
color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
validator: (value) { validator: (value) {
if (value.isEmpty) { return TextValidator().validateIdNumber(value);
return 'Please enter some text';
}
return null;
}, },
onSaved: (value) { onSaved: (value) {
// _patientSearchFormValues. = value; //_patientSearchFormValues. = value;
}, },
), ),
(!(_selectedType == '3' || _selectedType == '5')) (!(_selectedType == '3' || _selectedType == '5'))
@ -432,7 +318,7 @@ class _PatientSearchScreenState extends State<PatientSearchScreen> {
title: "Search", title: "Search",
color: Color(0xff58434F), color: Color(0xff58434F),
onPressed: () { onPressed: () {
print("Tapped Me"); _validateInputs();
}, },
), ),
], ],

@ -1,3 +1,7 @@
// OWNER : Ibrahim albitar
// DATE : 19-04-2020
// DESCRIPTION : Extension for all classes objects.
extension Extension on Object { extension Extension on Object {
bool isNullOrEmpty() => this == null || this == ''; bool isNullOrEmpty() => this == null || this == '';

@ -0,0 +1,37 @@
import '../util/extenstions.dart';
class TextValidator{
// OWNER : Ibrahim albitar
// DATE : 19-04-2020
// DESCRIPTION : Text Validator.
String validateName(String value) {
if (value.isNullOrEmpty()||value.length < 3)
return 'Name must be more than 2 charater';
else
return null;
}
String validateMobile(String value) {
if (value.isNullOrEmpty()||value.length != 10)
return 'Mobile Number must be of 10 digit';
else
return null;
}
String validateIdNumber(String value) {
if (value.isNullOrEmpty())
return 'Please input valid number';
else
return null;
}
String validateDate(String value) {
if (value.isNullOrEmpty())
return 'Please input valid date';
else
return null;
}
}

@ -1,10 +1,11 @@
import 'package:doctor_app_flutter/models/patient_model.dart'; import 'package:doctor_app_flutter/models/patient_model.dart';
import 'package:doctor_app_flutter/widgets/shared/app_text_form_field.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart'; import '../../util/text_validator.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
class DynamicElements extends StatefulWidget { class DynamicElements extends StatefulWidget {
PatientModel _patientSearchFormValues; final PatientModel _patientSearchFormValues;
DynamicElements(this._patientSearchFormValues); DynamicElements(this._patientSearchFormValues);
@override @override
_DynamicElementsState createState() => _DynamicElementsState(); _DynamicElementsState createState() => _DynamicElementsState();
@ -40,39 +41,21 @@ class _DynamicElementsState extends State<DynamicElements> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return LayoutBuilder( return LayoutBuilder(
builder: (ctx, constraints) { builder: (ctx, constraints) {
var smallScreenSize = 660;
bool isSmallScreen = constraints.maxWidth <= smallScreenSize;
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[
SizedBox( SizedBox(
height: 10, height: 10,
), ),
TextFormField( AppTextFormField(
keyboardType: TextInputType.number, textInputType: TextInputType.number,
hintText: 'From',
controller: _fromDateController, controller: _fromDateController,
decoration: InputDecoration( onChanged: (value){
hintText: 'From',
hintStyle: TextStyle( },
fontSize:
isSmallScreen ? 14 : constraints.maxWidth * 0.024),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30)),
borderSide: BorderSide(color: Color(0xff707070)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30.0)),
borderSide:
BorderSide(color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
validator: (value) { validator: (value) {
if (value.isEmpty) { return TextValidator().validateDate(value);
return 'Please enter some text';
}
return null;
}, },
onTap: () { onTap: () {
_presentDatePicker('_selectedFromDate'); _presentDatePicker('_selectedFromDate');
@ -85,39 +68,20 @@ class _DynamicElementsState extends State<DynamicElements> {
SizedBox( SizedBox(
height: 10, height: 10,
), ),
TextFormField( AppTextFormField(
keyboardType: TextInputType.number, textInputType: TextInputType.number,
hintText: 'TO',
controller: _toDateController, controller: _toDateController,
decoration: InputDecoration(
hintText: 'TO',
hintStyle: TextStyle(
fontSize:
isSmallScreen ? 14 : constraints.maxWidth * 0.024),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30)),
borderSide: BorderSide(color: Color(0xff707070)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30.0)),
borderSide:
BorderSide(color: Theme.of(context).primaryColor),
)
//BorderRadius.all(Radius.circular(20));
),
onTap: () { onTap: () {
_presentDatePicker('_selectedToDate'); _presentDatePicker('_selectedToDate');
}, },
validator: (value) { validator: (value) {
if (value.isEmpty) { return TextValidator().validateDate(value);
return 'Please enter some text';
}
return null;
}, },
onSaved: (value) { onSaved: (value) {
widget._patientSearchFormValues.To = value; widget._patientSearchFormValues.To = value;
}, },
), ),
], ],
); );
}, },

@ -0,0 +1,60 @@
import 'package:doctor_app_flutter/config/size_config.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
// OWNER : Ibrahim albitar
// DATE : 19-04-2020
// DESCRIPTION : Custom Text Form Field for app.
class AppTextFormField extends FormField<String> {
AppTextFormField({
FormFieldSetter<String> onSaved,
FormFieldValidator<String> validator,
ValueChanged<String> onChanged,
GestureTapCallback onTap,
TextEditingController controller,
bool autovalidate = true,
TextInputType textInputType,
String hintText
}) : super(
onSaved: onSaved,
validator: validator,
autovalidate: autovalidate,
builder: (FormFieldState<String> state) {
return Column(
children: <Widget>[
TextFormField(
keyboardType: textInputType,
onChanged: onChanged?? (value){
state.didChange(value);
},
decoration: InputDecoration(
hintText: hintText,
hintStyle: TextStyle(fontSize: SizeConfig.textMultiplier * 2),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30)),
borderSide: BorderSide(color: Color(0xff707070)),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(30)),
)
//BorderRadius.all(Radius.circular(20));
),
onTap: onTap,
controller: controller,
),
state.hasError?
Text(
state.errorText,
style: TextStyle(
color: Colors.red
),
) :
Container()
],
);
}
);
}
Loading…
Cancel
Save