From ea0c5d9eccee14dc5f62e9a18b356469a99a23d0 Mon Sep 17 00:00:00 2001 From: Francesco Bussolino Date: Mon, 23 Oct 2023 13:48:06 +0200 Subject: [PATCH 1/2] removed necessity for width and height --- example/lib/main.dart | 2 - lib/Components/ValidationBarWidget.dart | 6 +- lib/Components/ValidationTextWidget.dart | 23 ++-- lib/Utilities/SizeConfig.dart | 7 - lib/flutter_pw_validator.dart | 111 +++++++-------- pubspec.lock | 164 +++++++++++++++++++++++ 6 files changed, 225 insertions(+), 88 deletions(-) delete mode 100644 lib/Utilities/SizeConfig.dart create mode 100644 pubspec.lock diff --git a/example/lib/main.dart b/example/lib/main.dart index 42269aa..26b935b 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -65,8 +65,6 @@ class AppHome extends StatelessWidget { numericCharCount: 3, specialCharCount: 1, normalCharCount: 3, - width: 400, - height: 200, onSuccess: () { print("MATCHED"); ScaffoldMessenger.of(context).showSnackBar(new SnackBar( diff --git a/lib/Components/ValidationBarWidget.dart b/lib/Components/ValidationBarWidget.dart index e7020b9..8ea986b 100644 --- a/lib/Components/ValidationBarWidget.dart +++ b/lib/Components/ValidationBarWidget.dart @@ -1,5 +1,4 @@ import 'package:flutter/material.dart'; -import 'package:flutter_pw_validator/Utilities/SizeConfig.dart'; /// ValidationBarWidget that represent style of each one of them and shows under the TextField class ValidationBarComponent extends StatelessWidget { @@ -12,11 +11,10 @@ class ValidationBarComponent extends StatelessWidget { return Expanded( child: new Container( /// We Can set, width: double.maxFinite, - margin: EdgeInsets.symmetric(horizontal: SizeConfig.width! * 0.005), - height: SizeConfig.width! * 0.015, + margin: EdgeInsets.symmetric(horizontal: 2), decoration: new BoxDecoration( color: color, - borderRadius: BorderRadius.all(Radius.circular(SizeConfig.width!))), + borderRadius: BorderRadius.all(Radius.circular(100))), ), ); } diff --git a/lib/Components/ValidationTextWidget.dart b/lib/Components/ValidationTextWidget.dart index 629f329..9638b52 100644 --- a/lib/Components/ValidationTextWidget.dart +++ b/lib/Components/ValidationTextWidget.dart @@ -1,32 +1,31 @@ import 'package:flutter/material.dart'; -import 'package:flutter_pw_validator/Utilities/SizeConfig.dart'; /// ValidationTextWidget that represent style of each one of them and shows as list of condition that you want to the app user class ValidationTextWidget extends StatelessWidget { final Color color; final String text; final int? value; + final TextStyle? textStyle; - ValidationTextWidget( - {required this.color, required this.text, required this.value}); + ValidationTextWidget({ + required this.color, + required this.text, + required this.value, + this.textStyle + }); @override Widget build(BuildContext context) { return new Row( children: [ - new Container( - width: SizeConfig.width! * 0.03, - height: SizeConfig.width! * 0.03, - child: new CircleAvatar( - backgroundColor: color, - ), + new CircleAvatar( + backgroundColor: color, ), Padding( - padding: EdgeInsets.only(left: SizeConfig.width! * 0.03), + padding: EdgeInsets.only(left: 10), child: new Text( text.replaceFirst("-", value.toString()), - style: - new TextStyle(fontSize: SizeConfig.width! * 0.04, color: color), + style: (textStyle ?? Theme.of(context).textTheme.bodyMedium)?.copyWith(color: color) ), ) ], diff --git a/lib/Utilities/SizeConfig.dart b/lib/Utilities/SizeConfig.dart deleted file mode 100644 index b300ed6..0000000 --- a/lib/Utilities/SizeConfig.dart +++ /dev/null @@ -1,7 +0,0 @@ -/// SizeConfig hold user requested with and height for global use -class SizeConfig { - SizeConfig._(); - - static double? width; - static double? height; -} diff --git a/lib/flutter_pw_validator.dart b/lib/flutter_pw_validator.dart index b06045c..fccc906 100644 --- a/lib/flutter_pw_validator.dart +++ b/lib/flutter_pw_validator.dart @@ -3,12 +3,10 @@ library flutter_pw_validator; import 'package:flutter/material.dart'; import 'package:flutter_pw_validator/Utilities/ConditionsHelper.dart'; import 'package:flutter_pw_validator/Utilities/Validator.dart'; - import 'Components/ValidationBarWidget.dart'; import 'Components/ValidationTextWidget.dart'; import 'Resource/MyColors.dart'; import 'Resource/Strings.dart'; -import 'Utilities/SizeConfig.dart'; class FlutterPwValidator extends StatefulWidget { final int minLength, @@ -18,16 +16,15 @@ class FlutterPwValidator extends StatefulWidget { numericCharCount, specialCharCount; final Color defaultColor, successColor, failureColor; - final double width, height; final Function onSuccess; final Function? onFail; final TextEditingController controller; final FlutterPwValidatorStrings? strings; + final TextStyle? textStyle; final Key? key; FlutterPwValidator( - {required this.width, - required this.height, + { required this.minLength, required this.onSuccess, required this.controller, @@ -41,11 +38,8 @@ class FlutterPwValidator extends StatefulWidget { this.failureColor = MyColors.red, this.strings, this.onFail, - this.key}) { - //Initial entered size for global use - SizeConfig.width = width; - SizeConfig.height = height; - } + this.textStyle, + this.key}); @override State createState() => new FlutterPwValidatorState(); @@ -164,61 +158,52 @@ class FlutterPwValidatorState extends State { @override Widget build(BuildContext context) { - return new Container( - width: SizeConfig.width, - height: widget.height, - child: new Column( - mainAxisAlignment: MainAxisAlignment.spaceBetween, + return Container( + width: MediaQuery.of(context).size.width, + child: Column( children: [ - new Flexible( - flex: 3, - child: new Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - // Iterate through the conditions map values to check if there is any true values then create green ValidationBarComponent. - for (bool value in _conditionsHelper.getter()!.values) - if (value == true) - new ValidationBarComponent(color: widget.successColor), - - // Iterate through the conditions map values to check if there is any false values then create red ValidationBarComponent. - for (bool value in _conditionsHelper.getter()!.values) - if (value == false) - new ValidationBarComponent(color: widget.defaultColor) - ], - ), + Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + // Iterate through the conditions map values to check if there is any true values then create green ValidationBarComponent. + for (bool value in _conditionsHelper.getter()!.values) + if (value == true) + ValidationBarComponent(color: widget.successColor), + + // Iterate through the conditions map values to check if there is any false values then create red ValidationBarComponent. + for (bool value in _conditionsHelper.getter()!.values) + if (value == false) + ValidationBarComponent(color: widget.defaultColor) + ], ), - new Flexible( - flex: 7, - child: new Column( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - - //Iterate through the condition map entries and generate new ValidationTextWidget for each item in Green or Red Color - children: _conditionsHelper.getter()!.entries.map((entry) { - int? value; - if (entry.key == widget.translatedStrings.atLeast) - value = widget.minLength; - if (entry.key == widget.translatedStrings.normalLetters) - value = widget.normalCharCount; - if (entry.key == widget.translatedStrings.uppercaseLetters) - value = widget.uppercaseCharCount; - if (entry.key == widget.translatedStrings.lowercaseLetters) - value = widget.lowercaseCharCount; - if (entry.key == widget.translatedStrings.numericCharacters) - value = widget.numericCharCount; - if (entry.key == widget.translatedStrings.specialCharacters) - value = widget.specialCharCount; - return new ValidationTextWidget( - color: _isFirstRun - ? widget.defaultColor - : entry.value - ? widget.successColor - : widget.failureColor, - text: entry.key, - value: value, - ); - }).toList()), - ) + Column( + //Iterate through the condition map entries and generate new ValidationTextWidget for each item in Green or Red Color + children: _conditionsHelper.getter()!.entries.map((entry) { + int? value; + if (entry.key == widget.translatedStrings.atLeast) + value = widget.minLength; + if (entry.key == widget.translatedStrings.normalLetters) + value = widget.normalCharCount; + if (entry.key == widget.translatedStrings.uppercaseLetters) + value = widget.uppercaseCharCount; + if (entry.key == widget.translatedStrings.lowercaseLetters) + value = widget.lowercaseCharCount; + if (entry.key == widget.translatedStrings.numericCharacters) + value = widget.numericCharCount; + if (entry.key == widget.translatedStrings.specialCharacters) + value = widget.specialCharCount; + return new ValidationTextWidget( + color: _isFirstRun + ? widget.defaultColor + : entry.value + ? widget.successColor + : widget.failureColor, + text: entry.key, + textStyle: widget.textStyle, + value: value, + ); + }).toList()) ], ), ); diff --git a/pubspec.lock b/pubspec.lock new file mode 100644 index 0000000..a0798dc --- /dev/null +++ b/pubspec.lock @@ -0,0 +1,164 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: + async: + dependency: transitive + description: + name: async + sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c" + url: "https://pub.dev" + source: hosted + version: "2.11.0" + boolean_selector: + dependency: transitive + description: + name: boolean_selector + sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66" + url: "https://pub.dev" + source: hosted + version: "2.1.1" + characters: + dependency: transitive + description: + name: characters + sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605" + url: "https://pub.dev" + source: hosted + version: "1.3.0" + clock: + dependency: transitive + description: + name: clock + sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf + url: "https://pub.dev" + source: hosted + version: "1.1.1" + collection: + dependency: transitive + description: + name: collection + sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687 + url: "https://pub.dev" + source: hosted + version: "1.17.2" + fake_async: + dependency: transitive + description: + name: fake_async + sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78" + url: "https://pub.dev" + source: hosted + version: "1.3.1" + flutter: + dependency: "direct main" + description: flutter + source: sdk + version: "0.0.0" + flutter_test: + dependency: "direct dev" + description: flutter + source: sdk + version: "0.0.0" + matcher: + dependency: transitive + description: + name: matcher + sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e" + url: "https://pub.dev" + source: hosted + version: "0.12.16" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41" + url: "https://pub.dev" + source: hosted + version: "0.5.0" + meta: + dependency: transitive + description: + name: meta + sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3" + url: "https://pub.dev" + source: hosted + version: "1.9.1" + path: + dependency: transitive + description: + name: path + sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917" + url: "https://pub.dev" + source: hosted + version: "1.8.3" + sky_engine: + dependency: transitive + description: flutter + source: sdk + version: "0.0.99" + source_span: + dependency: transitive + description: + name: source_span + sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c" + url: "https://pub.dev" + source: hosted + version: "1.10.0" + stack_trace: + dependency: transitive + description: + name: stack_trace + sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5 + url: "https://pub.dev" + source: hosted + version: "1.11.0" + stream_channel: + dependency: transitive + description: + name: stream_channel + sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8" + url: "https://pub.dev" + source: hosted + version: "2.1.1" + string_scanner: + dependency: transitive + description: + name: string_scanner + sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde" + url: "https://pub.dev" + source: hosted + version: "1.2.0" + term_glyph: + dependency: transitive + description: + name: term_glyph + sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84 + url: "https://pub.dev" + source: hosted + version: "1.2.1" + test_api: + dependency: transitive + description: + name: test_api + sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8" + url: "https://pub.dev" + source: hosted + version: "0.6.0" + vector_math: + dependency: transitive + description: + name: vector_math + sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803" + url: "https://pub.dev" + source: hosted + version: "2.1.4" + web: + dependency: transitive + description: + name: web + sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10 + url: "https://pub.dev" + source: hosted + version: "0.1.4-beta" +sdks: + dart: ">=3.1.0-185.0.dev <4.0.0" From 91b2b040c3ffd7bc468037c7d8795804cd9404af Mon Sep 17 00:00:00 2001 From: Francesco Bussolino Date: Mon, 23 Oct 2023 14:19:50 +0200 Subject: [PATCH 2/2] added a few configuration options --- lib/Components/ValidationBarWidget.dart | 7 +- lib/Components/ValidationTextWidget.dart | 29 +++--- lib/flutter_pw_validator.dart | 110 +++++++++++++---------- 3 files changed, 87 insertions(+), 59 deletions(-) diff --git a/lib/Components/ValidationBarWidget.dart b/lib/Components/ValidationBarWidget.dart index 8ea986b..e398eb2 100644 --- a/lib/Components/ValidationBarWidget.dart +++ b/lib/Components/ValidationBarWidget.dart @@ -3,8 +3,12 @@ import 'package:flutter/material.dart'; /// ValidationBarWidget that represent style of each one of them and shows under the TextField class ValidationBarComponent extends StatelessWidget { final Color color; + final double? thickness; - ValidationBarComponent({required this.color}); + ValidationBarComponent({ + required this.color, + this.thickness + }); @override Widget build(BuildContext context) { @@ -12,6 +16,7 @@ class ValidationBarComponent extends StatelessWidget { child: new Container( /// We Can set, width: double.maxFinite, margin: EdgeInsets.symmetric(horizontal: 2), + height: thickness ?? 4, decoration: new BoxDecoration( color: color, borderRadius: BorderRadius.all(Radius.circular(100))), diff --git a/lib/Components/ValidationTextWidget.dart b/lib/Components/ValidationTextWidget.dart index 9638b52..0d886f0 100644 --- a/lib/Components/ValidationTextWidget.dart +++ b/lib/Components/ValidationTextWidget.dart @@ -2,31 +2,38 @@ import 'package:flutter/material.dart'; /// ValidationTextWidget that represent style of each one of them and shows as list of condition that you want to the app user class ValidationTextWidget extends StatelessWidget { - final Color color; + final bool? success; + final Color defaultColor; + final Color successColor; + final Color failureColor; final String text; final int? value; final TextStyle? textStyle; ValidationTextWidget({ - required this.color, + required this.success, required this.text, required this.value, + required this.defaultColor, + required this.successColor, + required this.failureColor, this.textStyle }); @override Widget build(BuildContext context) { - return new Row( + Color color = success == null ? defaultColor : success! ? successColor : failureColor; + return Row( children: [ - new CircleAvatar( - backgroundColor: color, + Icon( + success == null || success! ? Icons.check : Icons.close, + size: 20, + color: color, ), - Padding( - padding: EdgeInsets.only(left: 10), - child: new Text( - text.replaceFirst("-", value.toString()), - style: (textStyle ?? Theme.of(context).textTheme.bodyMedium)?.copyWith(color: color) - ), + const SizedBox(width: 10), + Text( + text.replaceFirst("-", value.toString()), + style: (textStyle ?? Theme.of(context).textTheme.bodyMedium)?.copyWith(color: color) ) ], ); diff --git a/lib/flutter_pw_validator.dart b/lib/flutter_pw_validator.dart index fccc906..c7d8536 100644 --- a/lib/flutter_pw_validator.dart +++ b/lib/flutter_pw_validator.dart @@ -21,6 +21,10 @@ class FlutterPwValidator extends StatefulWidget { final TextEditingController controller; final FlutterPwValidatorStrings? strings; final TextStyle? textStyle; + final EdgeInsets? padding; + final double? validationBarThickness; + final bool showValidationBar; + final bool showValidationText; final Key? key; FlutterPwValidator( @@ -39,7 +43,12 @@ class FlutterPwValidator extends StatefulWidget { this.strings, this.onFail, this.textStyle, - this.key}); + this.padding, + this.validationBarThickness, + this.showValidationBar = true, + this.showValidationText = true, + this.key + }); @override State createState() => new FlutterPwValidatorState(); @@ -158,53 +167,60 @@ class FlutterPwValidatorState extends State { @override Widget build(BuildContext context) { - return Container( + return SizedBox( width: MediaQuery.of(context).size.width, - child: Column( - children: [ - Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - // Iterate through the conditions map values to check if there is any true values then create green ValidationBarComponent. - for (bool value in _conditionsHelper.getter()!.values) - if (value == true) - ValidationBarComponent(color: widget.successColor), - - // Iterate through the conditions map values to check if there is any false values then create red ValidationBarComponent. - for (bool value in _conditionsHelper.getter()!.values) - if (value == false) - ValidationBarComponent(color: widget.defaultColor) - ], - ), - Column( - //Iterate through the condition map entries and generate new ValidationTextWidget for each item in Green or Red Color - children: _conditionsHelper.getter()!.entries.map((entry) { - int? value; - if (entry.key == widget.translatedStrings.atLeast) - value = widget.minLength; - if (entry.key == widget.translatedStrings.normalLetters) - value = widget.normalCharCount; - if (entry.key == widget.translatedStrings.uppercaseLetters) - value = widget.uppercaseCharCount; - if (entry.key == widget.translatedStrings.lowercaseLetters) - value = widget.lowercaseCharCount; - if (entry.key == widget.translatedStrings.numericCharacters) - value = widget.numericCharCount; - if (entry.key == widget.translatedStrings.specialCharacters) - value = widget.specialCharCount; - return new ValidationTextWidget( - color: _isFirstRun - ? widget.defaultColor - : entry.value - ? widget.successColor - : widget.failureColor, - text: entry.key, - textStyle: widget.textStyle, - value: value, - ); - }).toList()) - ], + child: Padding( + padding: widget.padding ?? EdgeInsets.zero, + child: Column( + children: [ + if(widget.showValidationBar || widget.showValidationText) const SizedBox(height: 2), + if(widget.showValidationBar) Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + // Iterate through the conditions map values to check if there is any true values then create green ValidationBarComponent. + for (bool value in _conditionsHelper.getter()!.values) + if (value == true) + ValidationBarComponent(color: widget.successColor, thickness: widget.validationBarThickness), + + // Iterate through the conditions map values to check if there is any false values then create red ValidationBarComponent. + for (bool value in _conditionsHelper.getter()!.values) + if (value == false) + ValidationBarComponent(color: widget.defaultColor) + ], + ), + if(widget.showValidationBar && widget.showValidationText) const SizedBox(height: 10), + if(widget.showValidationText) Padding( + padding: const EdgeInsets.symmetric(horizontal: 10), + child: Column( + //Iterate through the condition map entries and generate new ValidationTextWidget for each item in Green or Red Color + children: _conditionsHelper.getter()!.entries.map((entry) { + int? value; + if (entry.key == widget.translatedStrings.atLeast) + value = widget.minLength; + if (entry.key == widget.translatedStrings.normalLetters) + value = widget.normalCharCount; + if (entry.key == widget.translatedStrings.uppercaseLetters) + value = widget.uppercaseCharCount; + if (entry.key == widget.translatedStrings.lowercaseLetters) + value = widget.lowercaseCharCount; + if (entry.key == widget.translatedStrings.numericCharacters) + value = widget.numericCharCount; + if (entry.key == widget.translatedStrings.specialCharacters) + value = widget.specialCharCount; + return new ValidationTextWidget( + success: _isFirstRun ? null : entry.value, + defaultColor: widget.defaultColor, + successColor: widget.successColor, + failureColor: widget.failureColor, + text: entry.key, + textStyle: widget.textStyle, + value: value, + ); + }).toList()), + ) + ], + ), ), ); }