introduction
fancypasswordfield is a widget that acts as a password validation field. you can have all the features of a commom password field, and also gets cool features like setting validation rules and password strength.
basic usage
the most simple usage is just using fancypasswordfield withou any properties.
class myapp extends statelesswidget {
@override
widget build(buildcontext context) {
return materialapp(
title: 'flutter demo',
home: scaffold(
body: fancypasswordfield(),
),
);
}
}
this will create a password field with some default configurations. the default behavior is to show the strength indicator
above the form field. you can disabled this behavior passing false to the hasstrengthindicator
property.
we can go a bit further and pass some validations rules to our widget.
class myapp extends statelesswidget {
@override
widget build(buildcontext context) {
return materialapp(
title: 'flutter demo',
home: scaffold(
body: fancypasswordfield(
validationrules: {
digitvalidationrule(),
uppercasevalidationrule(),
lowercasevalidationrule(),
specialcharactervalidationrule(),
mincharactersvalidationrule(6),
maxcharactersvalidationrule(12),
},
),
),
);
}
}
these package comes if some pre defined commom rules so you can just use out of the box. feel free to implement your own [validationrule] and adds normaly to the password field.
custom rules
as described earlier, the package comes with built in commom used rules, but you can create your own custom rules. to do this, just extends the base [validationrule] abstract class. you’ll have to provide a name and a validation function for your rule.
class containstripleavalidationrule extends validationrule {
@override
string get name => 'contains aaa';
@override
bool validate(string value) {
return value.contains('aaa');
}
}
just doing that is already enough for your rule just work. pass your rule to the [validationrules] set of the [fancypasswordfield] and the showing and validation of the rules will occur.
password controller
the [fancypasswordfield] has a controller that you can pass. the purpose of this controller is almost the same as any controller on the flutter world. the controller gives you access to underline info of the field. notably, it gives you information about the rules and what rules are beeing ofended at any point. the controller has also one getter that indicates whether all rules are ok or not.
one use case of this is if you want to validate the status of the form field to enable or disable the saving of a form.
final fancypasswordcontroller _passwordcontroller = fancypasswordcontroller();
@override
widget build(buildcontext context) {
return scaffold(
body: center(
child: form(
key: _formkey,
child: column(
mainaxisalignment: mainaxisalignment.center,
children: <widget>[
sizedbox(
width: 400,
child: fancypasswordfield(
passwordcontroller: _passwordcontroller,
validationrules: {
digitvalidationrule(),
uppercasevalidationrule(),
},
validator: (value) {
return _passwordcontroller.areallrulesvalidated
? null
: 'not validated';
},
),
),
],
),
),
),
);
}
customization
the [fancypasswordfield] comes with some pre defined widget that show some okish good looking components for the strenght indicator
and for the rules. but the widget was designed to be fully customizable. so you can pass your own builers to both the
strength password widget and the rules widget.
on the samples section you can grab a lot of customizations samples.
above we can see an example of some possible customization for these fields.
class myapp extends statelesswidget {
@override
widget build(buildcontext context) {
return materialapp(
title: 'flutter demo',
home: scaffold(
body: sizedbox(
width: 400,
child: fancypasswordfield(
validationrules: {
digitvalidationrule(),
uppercasevalidationrule(),
lowercasevalidationrule(),
specialcharactervalidationrule(),
mincharactersvalidationrule(6),
maxcharactersvalidationrule(12),
},
strengthindicatorbuilder: (strength) => text(
strength.tostring(),
),
validationrulebuilder: (rules, value) {
if (value.isempty) {
return const sizedbox.shrink();
}
return listview(
shrinkwrap: true,
children: rules
.map(
(rule) => rule.validate(value)
? row(
children: [
const icon(
icons.check,
color: colors.green,
),
const sizedbox(width: 12),
text(
rule.name,
style: const textstyle(
color: colors.green,
),
),
],
)
: row(
children: [
const icon(
icons.close,
color: colors.red,
),
const sizedbox(width: 12),
text(
rule.name,
style: const textstyle(
color: colors.red,
),
),
],
),
)
.tolist(),
);
},
),
),
),
);
}
}
samples
suggestions & bugs
for any suggestions or bug report please head to issue tracker.
Comments are closed.